]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/attachment.pm
fix expensive move code path
[git.ikiwiki.info.git] / IkiWiki / Plugin / attachment.pm
index f9d7f14f6c00a882aa2a2fbad83701dfec5be17a..a474f6e75f58b178b2b049fd1c4e590740a4d6f2 100644 (file)
@@ -4,18 +4,21 @@ package IkiWiki::Plugin::attachment;
 use warnings;
 use strict;
 use IkiWiki 2.00;
-use CGI;
-$CGI::DISABLE_UPLOADS=0;
 
 # TODO move to admin prefs
 $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
 
 sub import { #{{{
+       hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
        hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
        hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
 } # }}}
 
-sub formbuilder_setup { #{{{
+sub checkconfig () { #{{{
+       $config{cgi_disable_uploads}=0;
+} #}}}
+
+sub formbuilder_setup (@) { #{{{
        my %params=@_;
        my $form=$params{form};
 
@@ -32,7 +35,7 @@ sub formbuilder (@) { #{{{
 
        if ($form->submitted eq "Upload") {
                my $q=$params{cgi};
-               my $filename=IkiWiki::basename($q->param('attachment'));
+               my $filename=$q->param('attachment');
                if (! defined $filename || ! length $filename) {
                        # no file, so do nothing
                        return;
@@ -46,11 +49,11 @@ sub formbuilder (@) { #{{{
                # to, unless that page is an "index" page.
                my $page=$form->field('page');
                $page=~s/(^|\/)index//;
-               $filename="$page/$filename";
+               $filename=$page."/".IkiWiki::basename($filename);
                
                # To untaint the filename, escape any hazardous characters,
                # and make sure it isn't pruned.
-               $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename);
+               $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename));
                if (IkiWiki::file_pruned($filename, $config{srcdir})) {
                        error(gettext("bad attachment filename"));
                }
@@ -69,9 +72,12 @@ sub formbuilder (@) { #{{{
                        }
                }
 
+               # Needed for fast_file_copy.
+               require IkiWiki::Render;
+
                # Move the attachment into place.
                # Try to use a fast rename; fall back to copying.
-               prep_writefile($filename, $config{srcdir});
+               IkiWiki::prep_writefile($filename, $config{srcdir});
                unlink($config{srcdir}."/".$filename);
                if (! rename($tempfile, $config{srcdir}."/".$filename)) {
                        my $fh=$q->upload('attachment');