-sub match_maxsize ($$;@) { #{{{
- shift;
- my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
- if ($@) {
- return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
- }
-
- my %params=@_;
- if (! exists $params{file}) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (-s $params{file} > $maxsize) {
- return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
- }
- else {
- return IkiWiki::SuccessReason->new("file not too large");
- }
-} #}}}
-
-sub match_minsize ($$;@) { #{{{
- shift;
- my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
- if ($@) {
- return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
- }
-
- my %params=@_;
- if (! exists $params{file}) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (-s $params{file} < $minsize) {
- return IkiWiki::FailReason->new("file too small");
- }
- else {
- return IkiWiki::SuccessReason->new("file not too small");
- }
-} #}}}
-
-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";
- }
-
- my $regexp=IkiWiki::glob2re($wanted);
- 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_virusfree ($$;@) { #{{{
- shift;
- my $wanted=shift;
-
- my %params=@_;
- if (! exists $params{file}) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (! exists $IkiWiki::config{virus_checker} ||
- ! length $IkiWiki::config{virus_checker}) {
- return IkiWiki::FailReason->new("no virus_checker configured");
- }
-
- # The file needs to be fed into the virus checker on stdin,
- # because the file is not world-readable, and if clamdscan is
- # used, clamd would fail to read it.
- eval q{use IPC::Open2};
- error($@) if $@;
- open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
- binmode(IN);
- my $sigpipe=0;
- $SIG{PIPE} = sub { $sigpipe=1 };
- my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
- my $reason=<CHECKER_OUT>;
- chomp $reason;
- 1 while (<CHECKER_OUT>);
- close(CHECKER_OUT);
- waitpid $pid, 0;
- $SIG{PIPE}="DEFAULT";
- if ($sigpipe || $?) {
- return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
- }
- else {
- return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
- }
-} #}}}
-
-sub match_ispage ($$;@) { #{{{
- my $filename=shift;
-
- if (defined IkiWiki::pagetype($filename)) {
- return IkiWiki::SuccessReason->new("file is a wiki page");
- }
- else {
- return IkiWiki::FailReason->new("file is not a wiki page");
- }
-} #}}}
-