fix expensive move code path
[ikiwiki] / IkiWiki / Plugin / attachment.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::attachment;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 # TODO move to admin prefs
9 $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
10
11 sub import { #{{{
12         hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
13         hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
14         hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
15 } # }}}
16
17 sub checkconfig () { #{{{
18         $config{cgi_disable_uploads}=0;
19 } #}}}
20
21 sub formbuilder_setup (@) { #{{{
22         my %params=@_;
23         my $form=$params{form};
24
25         return if $form->field("do") ne "edit";
26
27         $form->field(name => 'attachment', type => 'file');
28 } #}}}
29
30 sub formbuilder (@) { #{{{
31         my %params=@_;
32         my $form=$params{form};
33
34         return if $form->field("do") ne "edit";
35
36         if ($form->submitted eq "Upload") {
37                 my $q=$params{cgi};
38                 my $filename=$q->param('attachment');
39                 if (! defined $filename || ! length $filename) {
40                         # no file, so do nothing
41                         return;
42                 }
43                 
44                 # This is an (apparently undocumented) way to get the name
45                 # of the temp file that CGI writes the upload to.
46                 my $tempfile=$q->tmpFileName($filename);
47                 
48                 # Put the attachment in a subdir of the page it's attached
49                 # to, unless that page is an "index" page.
50                 my $page=$form->field('page');
51                 $page=~s/(^|\/)index//;
52                 $filename=$page."/".IkiWiki::basename($filename);
53                 
54                 # To untaint the filename, escape any hazardous characters,
55                 # and make sure it isn't pruned.
56                 $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename));
57                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
58                         error(gettext("bad attachment filename"));
59                 }
60                 
61                 # Check that the user is allowed to edit a page with the
62                 # name of the attachment.
63                 IkiWiki::check_canedit($filename, $q, $params{session}, 1);
64                 
65                 # Use a pagespec to test that the attachment is valid.
66                 if (exists $config{valid_attachments} &&
67                     length $config{valid_attachments}) {
68                         my $result=pagespec_match($filename, $config{valid_attachments},
69                                 file => $tempfile);
70                         if (! $result) {
71                                 error(gettext("attachment rejected")." ($result)");
72                         }
73                 }
74
75                 # Needed for fast_file_copy.
76                 require IkiWiki::Render;
77
78                 # Move the attachment into place.
79                 # Try to use a fast rename; fall back to copying.
80                 IkiWiki::prep_writefile($filename, $config{srcdir});
81                 unlink($config{srcdir}."/".$filename);
82                 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
83                         my $fh=$q->upload('attachment');
84                         if (! defined $fh || ! ref $fh) {
85                                 error("failed to get filehandle");
86                         }
87                         binmode($fh);
88                         writefile($filename, $config{srcdir}, undef, 1, sub {
89                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
90                         });
91                 }
92
93                 # TODO add to vcs
94                 
95                 # TODO trigger a wiki build if there's no vcs
96         }
97 } # }}}
98
99 package IkiWiki::PageSpec;
100
101 sub parsesize { #{{{
102         my $size=shift;
103         no warnings;
104         my $base=$size+0; # force to number
105         use warnings;
106         my $multiple=1;
107         if ($size=~/kb?$/i) {
108                 $multiple=2**10;
109         }
110         elsif ($size=~/mb?$/i) {
111                 $multiple=2**20;
112         }
113         elsif ($size=~/gb?$/i) {
114                 $multiple=2**30;
115         }
116         elsif ($size=~/tb?$/i) {
117                 $multiple=2**40;
118         }
119         return $base * $multiple;
120 } #}}}
121
122 sub match_maxsize ($$;@) { #{{{
123         shift;
124         my $maxsize=eval{parsesize(shift)};
125         if ($@) {
126                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
127         }
128
129         my %params=@_;
130         if (! exists $params{file}) {
131                 return IkiWiki::FailReason->new("no file specified");
132         }
133
134         if (-s $params{file} > $maxsize) {
135                 return IkiWiki::FailReason->new("file too large");
136         }
137         else {
138                 return IkiWiki::SuccessReason->new("file not too large");
139         }
140 } #}}}
141
142 sub match_minsize ($$;@) { #{{{
143         shift;
144         my $minsize=eval{parsesize(shift)};
145         if ($@) {
146                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
147         }
148
149         my %params=@_;
150         if (! exists $params{file}) {
151                 return IkiWiki::FailReason->new("no file specified");
152         }
153
154         if (-s $params{file} < $minsize) {
155                 return IkiWiki::FailReason->new("file too small");
156         }
157         else {
158                 return IkiWiki::SuccessReason->new("file not too small");
159         }
160 } #}}}
161
162 sub match_ispage ($$;@) { #{{{
163         my $filename=shift;
164
165         if (defined IkiWiki::pagetype($filename)) {
166                 return IkiWiki::SuccessReason->new("file is a wiki page");
167         }
168         else {
169                 return IkiWiki::FailReason->new("file is not a wiki page");
170         }
171 } #}}}
172
173 1