]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/attachment.pm
willu's teximg changes
[git.ikiwiki.info.git] / IkiWiki / Plugin / attachment.pm
index 404ca07fa4fca244c9a34d6d7af50750e62337de..c78a1c177ee6cbccb1085c7f456f7d62afbe7db5 100644 (file)
@@ -18,13 +18,6 @@ sub getsetup () { #{{{
                        safe => 1,
                        rebuild => 0,
                },
                        safe => 1,
                        rebuild => 0,
                },
-               virus_checker => {
-                       type => "string",
-                       example => "clamdscan -",
-                       description => "virus checker program (reads STDIN, returns nonzero if virus found)",
-                       safe => 0, # executed
-                       rebuild => 0,
-               },
                allowed_attachments => {
                        type => "pagespec",
                        example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
                allowed_attachments => {
                        type => "pagespec",
                        example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
@@ -33,6 +26,13 @@ sub getsetup () { #{{{
                        safe => 1,
                        rebuild => 0,
                },
                        safe => 1,
                        rebuild => 0,
                },
+               virus_checker => {
+                       type => "string",
+                       example => "clamdscan -",
+                       description => "virus checker program (reads STDIN, returns nonzero if virus found)",
+                       safe => 0, # executed
+                       rebuild => 0,
+               },
 } #}}}
 
 sub check_canattach ($$;$) { #{{{
 } #}}}
 
 sub check_canattach ($$;$) { #{{{
@@ -42,7 +42,8 @@ sub check_canattach ($$;$) { #{{{
 
        # Don't allow an attachment to be uploaded with the same name as an
        # existing page.
 
        # Don't allow an attachment to be uploaded with the same name as an
        # existing page.
-       if (exists $pagesources{$dest} && $pagesources{$dest} ne $dest) {
+       if (exists $IkiWiki::pagesources{$dest} &&
+           $IkiWiki::pagesources{$dest} ne $dest) {
                error(sprintf(gettext("there is already a page named %s"), $dest));
        }
 
                error(sprintf(gettext("there is already a page named %s"), $dest));
        }
 
@@ -143,7 +144,7 @@ sub formbuilder_setup (@) { #{{{
                                IkiWiki::userinfo_set($user_name, "allowed_attachments",
                                $form->field("allowed_attachments")) ||
                                        error("failed to set allowed_attachments");
                                IkiWiki::userinfo_set($user_name, "allowed_attachments",
                                $form->field("allowed_attachments")) ||
                                        error("failed to set allowed_attachments");
-                               if (length $form->field("allowed_attachments")) {
+                               if (length $form->field("allowed_attachments")) {
                                        $form->field(name => "allowed_attachments", type => "hidden");
                                }
                        }
                                        $form->field(name => "allowed_attachments", type => "hidden");
                                }
                        }
@@ -349,19 +350,20 @@ sub humansize ($) { #{{{
 package IkiWiki::PageSpec;
 
 sub match_maxsize ($$;@) { #{{{
 package IkiWiki::PageSpec;
 
 sub match_maxsize ($$;@) { #{{{
-       shift;
+       my $page=shift;
        my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
        if ($@) {
                return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
        }
 
        my %params=@_;
        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}) {
+       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       if (! defined $file) {
                return IkiWiki::FailReason->new("no file specified");
        }
 
                return IkiWiki::FailReason->new("no file specified");
        }
 
-       if (-s $params{file} > $maxsize) {
-               return IkiWiki::FailReason->new("file too large (".(-s $params{file})." >  $maxsize)");
+       if (-s $file > $maxsize) {
+               return IkiWiki::FailReason->new("file too large (".(-s $file)." >  $maxsize)");
        }
        else {
                return IkiWiki::SuccessReason->new("file not too large");
        }
        else {
                return IkiWiki::SuccessReason->new("file not too large");
@@ -369,18 +371,19 @@ sub match_maxsize ($$;@) { #{{{
 } #}}}
 
 sub match_minsize ($$;@) { #{{{
 } #}}}
 
 sub match_minsize ($$;@) { #{{{
-       shift;
+       my $page=shift;
        my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
        if ($@) {
                return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
        }
 
        my %params=@_;
        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}) {
+       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       if (! defined $file) {
                return IkiWiki::FailReason->new("no file specified");
        }
 
                return IkiWiki::FailReason->new("no file specified");
        }
 
-       if (-s $params{file} < $minsize) {
+       if (-s $file < $minsize) {
                return IkiWiki::FailReason->new("file too small");
        }
        else {
                return IkiWiki::FailReason->new("file too small");
        }
        else {
@@ -389,11 +392,12 @@ sub match_minsize ($$;@) { #{{{
 } #}}}
 
 sub match_mimetype ($$;@) { #{{{
 } #}}}
 
 sub match_mimetype ($$;@) { #{{{
-       shift;
+       my $page=shift;
        my $wanted=shift;
 
        my %params=@_;
        my $wanted=shift;
 
        my %params=@_;
-       if (! exists $params{file}) {
+       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       if (! defined $file) {
                return IkiWiki::FailReason->new("no file specified");
        }
 
                return IkiWiki::FailReason->new("no file specified");
        }
 
@@ -403,7 +407,7 @@ sub match_mimetype ($$;@) { #{{{
        if ($@) {
                return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
        }
        if ($@) {
                return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
        }
-       my $mimetype=File::MimeInfo::Magic::magic($params{file});
+       my $mimetype=File::MimeInfo::Magic::magic($file);
        if (! defined $mimetype) {
                $mimetype="unknown";
        }
        if (! defined $mimetype) {
                $mimetype="unknown";
        }
@@ -418,11 +422,12 @@ sub match_mimetype ($$;@) { #{{{
 } #}}}
 
 sub match_virusfree ($$;@) { #{{{
 } #}}}
 
 sub match_virusfree ($$;@) { #{{{
-       shift;
+       my $page=shift;
        my $wanted=shift;
 
        my %params=@_;
        my $wanted=shift;
 
        my %params=@_;
-       if (! exists $params{file}) {
+       my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
+       if (! defined $file) {
                return IkiWiki::FailReason->new("no file specified");
        }
 
                return IkiWiki::FailReason->new("no file specified");
        }
 
@@ -436,7 +441,7 @@ sub match_virusfree ($$;@) { #{{{
        # used, clamd would fail to read it.
        eval q{use IPC::Open2};
        error($@) if $@;
        # 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");
+       open (IN, "<", $file) || return IkiWiki::FailReason->new("failed to read file");
        binmode(IN);
        my $sigpipe=0;
        $SIG{PIPE} = sub { $sigpipe=1 };
        binmode(IN);
        my $sigpipe=0;
        $SIG{PIPE} = sub { $sigpipe=1 };