]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/filecheck.pm
filecheck: Fix bug that prevented the pagespecs from matching when not called by...
[ikiwiki.git] / IkiWiki / Plugin / filecheck.pm
index 1855a8b48720baece395eeb06b4f2e1f84d701e4..1549b82db75a9d5164c04b166a3d6d149bdb01dc 100644 (file)
@@ -3,9 +3,9 @@ package IkiWiki::Plugin::filecheck;
 
 use warnings;
 use strict;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 
-my %units=( #{{{       # size in bytes
+my %units=(            # size in bytes
        B               => 1,
        byte            => 1,
        KB              => 2 ** 10,
@@ -37,9 +37,9 @@ my %units=( #{{{      # size in bytes
        # ikiwiki, if you find you need larger data quantities, either modify
        # yourself to add them, or travel back in time to 2008 and kill me.
        #   -- Joey
-); #}}}
+);
 
-sub parsesize ($) { #{{{
+sub parsesize ($) {
        my $size=shift;
 
        no warnings;
@@ -51,10 +51,10 @@ sub parsesize ($) { #{{{
                }
        }
        return $base;
-} #}}}
+}
 
 # This is provided for other plugins that want to convert back the other way.
-sub humansize ($) { #{{{
+sub humansize ($) {
        my $size=shift;
 
        foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
@@ -63,21 +63,21 @@ sub humansize ($) { #{{{
                }
        }
        return $size; # near zero, or negative
-} #}}}
+}
 
 package IkiWiki::PageSpec;
 
-sub match_maxsize ($$;@) { #{{{
+sub match_maxsize ($$;@) {
        my $page=shift;
-       my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
+       my $maxsize=eval{IkiWiki::Plugin::filecheck::parsesize(shift)};
        if ($@) {
-               return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
+               return IkiWiki::ErrorReason->new("unable to parse maxsize (or number too large)");
        }
 
        my %params=@_;
-       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       my $file=exists $params{file} ? $params{file} : IkiWiki::srcfile($IkiWiki::pagesources{$page});
        if (! defined $file) {
-               return IkiWiki::FailReason->new("no file specified");
+               return IkiWiki::ErrorReason->new("file does not exist");
        }
 
        if (-s $file > $maxsize) {
@@ -86,19 +86,19 @@ sub match_maxsize ($$;@) { #{{{
        else {
                return IkiWiki::SuccessReason->new("file not too large");
        }
-} #}}}
+}
 
-sub match_minsize ($$;@) { #{{{
+sub match_minsize ($$;@) {
        my $page=shift;
-       my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
+       my $minsize=eval{IkiWiki::Plugin::filecheck::parsesize(shift)};
        if ($@) {
-               return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
+               return IkiWiki::ErrorReason->new("unable to parse minsize (or number too large)");
        }
 
        my %params=@_;
-       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       my $file=exists $params{file} ? $params{file} : IkiWiki::srcfile($IkiWiki::pagesources{$page});
        if (! defined $file) {
-               return IkiWiki::FailReason->new("no file specified");
+               return IkiWiki::ErrorReason->new("file does not exist");
        }
 
        if (-s $file < $minsize) {
@@ -107,27 +107,30 @@ sub match_minsize ($$;@) { #{{{
        else {
                return IkiWiki::SuccessReason->new("file not too small");
        }
-} #}}}
+}
 
-sub match_mimetype ($$;@) { #{{{
+sub match_mimetype ($$;@) {
        my $page=shift;
        my $wanted=shift;
 
        my %params=@_;
-       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       my $file=exists $params{file} ? $params{file} : IkiWiki::srcfile($IkiWiki::pagesources{$page});
        if (! defined $file) {
-               return IkiWiki::FailReason->new("no file specified");
+               return IkiWiki::ErrorReason->new("file does not exist");
        }
 
        # 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");
+               return IkiWiki::ErrorReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
        }
        my $mimetype=File::MimeInfo::Magic::magic($file);
        if (! defined $mimetype) {
-               $mimetype="unknown";
+               $mimetype=File::MimeInfo::Magic::default($file);
+               if (! defined $mimetype) {
+                       $mimetype="unknown";
+               }
        }
 
        my $regexp=IkiWiki::glob2re($wanted);
@@ -137,21 +140,21 @@ sub match_mimetype ($$;@) { #{{{
        else {
                return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
        }
-} #}}}
+}
 
-sub match_virusfree ($$;@) { #{{{
+sub match_virusfree ($$;@) {
        my $page=shift;
        my $wanted=shift;
 
        my %params=@_;
-       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       my $file=exists $params{file} ? $params{file} : IkiWiki::srcfile($IkiWiki::pagesources{$page});
        if (! defined $file) {
-               return IkiWiki::FailReason->new("no file specified");
+               return IkiWiki::ErrorReason->new("file does not exist");
        }
 
        if (! exists $IkiWiki::config{virus_checker} ||
            ! length $IkiWiki::config{virus_checker}) {
-               return IkiWiki::FailReason->new("no virus_checker configured");
+               return IkiWiki::ErrorReason->new("no virus_checker configured");
        }
 
        # The file needs to be fed into the virus checker on stdin,
@@ -159,7 +162,7 @@ sub match_virusfree ($$;@) { #{{{
        # used, clamd would fail to read it.
        eval q{use IPC::Open2};
        error($@) if $@;
-       open (IN, "<", $file) || return IkiWiki::FailReason->new("failed to read file");
+       open (IN, "<", $file) || return IkiWiki::ErrorReason->new("failed to read file");
        binmode(IN);
        my $sigpipe=0;
        $SIG{PIPE} = sub { $sigpipe=1 };
@@ -179,9 +182,9 @@ sub match_virusfree ($$;@) { #{{{
        else {
                return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
        }
-} #}}}
+}
 
-sub match_ispage ($$;@) { #{{{
+sub match_ispage ($$;@) {
        my $filename=shift;
 
        if (defined IkiWiki::pagetype($filename)) {
@@ -190,4 +193,4 @@ sub match_ispage ($$;@) { #{{{
        else {
                return IkiWiki::FailReason->new("file is not a wiki page");
        }
-} #}}}
+}