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 # Needed for fast_file_copy.
76 require IkiWiki::Render;
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");
88 writefile($filename, $config{srcdir}, undef, 1, sub {
89 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
95 # TODO trigger a wiki build if there's no vcs
99 package IkiWiki::PageSpec;
104 my $base=$size+0; # force to number
107 if ($size=~/kb?$/i) {
110 elsif ($size=~/mb?$/i) {
113 elsif ($size=~/gb?$/i) {
116 elsif ($size=~/tb?$/i) {
119 return $base * $multiple;
122 sub match_maxsize ($$;@) { #{{{
124 my $maxsize=eval{parsesize(shift)};
126 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
130 if (! exists $params{file}) {
131 return IkiWiki::FailReason->new("no file specified");
134 if (-s $params{file} > $maxsize) {
135 return IkiWiki::FailReason->new("file too large");
138 return IkiWiki::SuccessReason->new("file not too large");
142 sub match_minsize ($$;@) { #{{{
144 my $minsize=eval{parsesize(shift)};
146 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
150 if (! exists $params{file}) {
151 return IkiWiki::FailReason->new("no file specified");
154 if (-s $params{file} < $minsize) {
155 return IkiWiki::FailReason->new("file too small");
158 return IkiWiki::SuccessReason->new("file not too small");
162 sub match_ispage ($$;@) { #{{{
165 if (defined IkiWiki::pagetype($filename)) {
166 return IkiWiki::SuccessReason->new("file is a wiki page");
169 return IkiWiki::FailReason->new("file is not a wiki page");