2 package IkiWiki::Plugin::attachment;
8 # TODO move to admin prefs
9 $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
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);
17 sub checkconfig () { #{{{
18 $config{cgi_disable_uploads}=0;
21 sub formbuilder_setup (@) { #{{{
23 my $form=$params{form};
25 return if $form->field("do") ne "edit";
27 $form->field(name => 'attachment', type => 'file');
30 sub formbuilder (@) { #{{{
32 my $form=$params{form};
34 return if $form->field("do") ne "edit";
36 if ($form->submitted eq "Upload") {
38 my $filename=$q->param('attachment');
39 if (! defined $filename || ! length $filename) {
40 # no file, so do nothing
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);
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);
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"));
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);
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},
71 error(gettext("attachment rejected")." ($result)");
75 # Move the attachment into place.
76 # Try to use a fast rename; fall back to copying.
77 IkiWiki::prep_writefile($filename, $config{srcdir});
78 unlink($config{srcdir}."/".$filename);
79 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
80 my $fh=$q->upload('attachment');
81 if (! defined $fh || ! ref $fh) {
82 error("failed to get filehandle");
85 writefile($filename, $config{srcdir}, undef, 1, sub {
86 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
92 # TODO trigger a wiki build if there's no vcs
96 package IkiWiki::PageSpec;
101 my $base=$size+0; # force to number
104 if ($size=~/kb?$/i) {
107 elsif ($size=~/mb?$/i) {
110 elsif ($size=~/gb?$/i) {
113 elsif ($size=~/tb?$/i) {
116 return $base * $multiple;
119 sub match_maxsize ($$;@) { #{{{
121 my $maxsize=eval{parsesize(shift)};
123 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
127 if (! exists $params{file}) {
128 return IkiWiki::FailReason->new("no file specified");
131 if (-s $params{file} > $maxsize) {
132 return IkiWiki::FailReason->new("file too large");
135 return IkiWiki::SuccessReason->new("file not too large");
139 sub match_minsize ($$;@) { #{{{
141 my $minsize=eval{parsesize(shift)};
143 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
147 if (! exists $params{file}) {
148 return IkiWiki::FailReason->new("no file specified");
151 if (-s $params{file} < $minsize) {
152 return IkiWiki::FailReason->new("file too small");
155 return IkiWiki::SuccessReason->new("file not too small");
159 sub match_ispage ($$;@) { #{{{
162 if (defined IkiWiki::pagetype($filename)) {
163 return IkiWiki::SuccessReason->new("file is a wiki page");
166 return IkiWiki::FailReason->new("file is not a wiki page");