summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin
diff options
context:
space:
mode:
authorJoey Hess <joey@kodama.kitenet.net>2008-07-02 17:30:00 -0400
committerJoey Hess <joey@kodama.kitenet.net>2008-07-02 18:22:49 -0400
commit84e6c4fa665703133b5778e92c045897df496e97 (patch)
treeeb717fb3856204cd03dce54dca4c7716cc06590c /IkiWiki/Plugin
parent190d328e402102f04ee0a287f9b173c52bffa341 (diff)
add mimetype checking
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r--IkiWiki/Plugin/attachment.pm33
1 files changed, 33 insertions, 0 deletions
diff --git a/IkiWiki/Plugin/attachment.pm b/IkiWiki/Plugin/attachment.pm
index a5c42d638..5d918c43f 100644
--- a/IkiWiki/Plugin/attachment.pm
+++ b/IkiWiki/Plugin/attachment.pm
@@ -298,6 +298,39 @@ sub match_minsize ($$;@) { #{{{
}
} #}}}
+sub match_mimetype ($$;@) { #{{{
+ shift;
+ my $wanted=shift;
+
+ my %params=@_;
+ if (! exists $params{file}) {
+ return IkiWiki::FailReason->new("no file specified");
+ }
+
+ # Use ::magic to get the mime type, the idea is to only trust
+ # data obtained by examining the actual file contents.
+ eval q{use File::MimeInfo::Magic};
+ if ($@) {
+ return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
+ }
+ my $mimetype=File::MimeInfo::Magic::magic($params{file});
+ if (! defined $mimetype) {
+ $mimetype="unknown";
+ }
+
+ # turn glob into a safe regexp
+ my $regexp=quotemeta($wanted);
+ $regexp=~s/\\\*/.*/g;
+ $regexp=~s/\\\?/./g;
+
+ if ($mimetype!~/^$regexp$/i) {
+ return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
+ }
+ else {
+ return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
+ }
+} #}}}
+
sub match_ispage ($$;@) { #{{{
my $filename=shift;