add mimetype checking
parent
f8e33430d8
commit
b941827420
|
@ -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 ($$;@) { #{{{
|
sub match_ispage ($$;@) { #{{{
|
||||||
my $filename=shift;
|
my $filename=shift;
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ For example, to limit arbitrary files to 50 kilobytes, but allow
|
||||||
larger mp3 files to be uploaded by joey, a test like this could be
|
larger mp3 files to be uploaded by joey, a test like this could be
|
||||||
used:
|
used:
|
||||||
|
|
||||||
(user(joey) and *.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))
|
(user(joey) and *.mp3 and mimetype(audio/mpeg) and maxsize(15mb)) or (!ispage() and maxsize(50kb))
|
||||||
|
|
||||||
The following additional tests are available:
|
The following additional tests are available:
|
||||||
|
|
||||||
|
@ -62,3 +62,9 @@ The following additional tests are available:
|
||||||
|
|
||||||
Tests whether the attacment is being uploaded from the specified IP
|
Tests whether the attacment is being uploaded from the specified IP
|
||||||
address.
|
address.
|
||||||
|
|
||||||
|
* mimetype(foo/bar)
|
||||||
|
|
||||||
|
If the [[cpan File::MimeInfo::Magic]] perl module is installed, this
|
||||||
|
allows checking the mime type of the attachment. You can include a glob
|
||||||
|
in the type, for example `mimetype(image/*)`.
|
||||||
|
|
Loading…
Reference in New Issue