2 package IkiWiki::Plugin::filecheck;
8 my %units=( #{{{ # size in bytes
37 # ikiwiki, if you find you need larger data quantities, either modify
38 # yourself to add them, or travel back in time to 2008 and kill me.
42 sub parsesize ($) { #{{{
46 my $base=$size+0; # force to number
48 foreach my $unit (sort keys %units) {
49 if ($size=~/[0-9\s]\Q$unit\E$/i) {
50 return $base * $units{$unit};
56 sub humansize ($) { #{{{
59 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
60 if ($size / $units{$unit} > 0.25) {
61 return (int($size / $units{$unit} * 10)/10).$unit;
64 return $size; # near zero, or negative
67 package IkiWiki::PageSpec;
69 sub match_maxsize ($$;@) { #{{{
71 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
73 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
77 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
78 if (! defined $file) {
79 return IkiWiki::FailReason->new("no file specified");
82 if (-s $file > $maxsize) {
83 return IkiWiki::FailReason->new("file too large (".(-s $file)." > $maxsize)");
86 return IkiWiki::SuccessReason->new("file not too large");
90 sub match_minsize ($$;@) { #{{{
92 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
94 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
98 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
99 if (! defined $file) {
100 return IkiWiki::FailReason->new("no file specified");
103 if (-s $file < $minsize) {
104 return IkiWiki::FailReason->new("file too small");
107 return IkiWiki::SuccessReason->new("file not too small");
111 sub match_mimetype ($$;@) { #{{{
116 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
117 if (! defined $file) {
118 return IkiWiki::FailReason->new("no file specified");
121 # Use ::magic to get the mime type, the idea is to only trust
122 # data obtained by examining the actual file contents.
123 eval q{use File::MimeInfo::Magic};
125 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
127 my $mimetype=File::MimeInfo::Magic::magic($file);
128 if (! defined $mimetype) {
132 my $regexp=IkiWiki::glob2re($wanted);
133 if ($mimetype!~/^$regexp$/i) {
134 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
137 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
141 sub match_virusfree ($$;@) { #{{{
146 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
147 if (! defined $file) {
148 return IkiWiki::FailReason->new("no file specified");
151 if (! exists $IkiWiki::config{virus_checker} ||
152 ! length $IkiWiki::config{virus_checker}) {
153 return IkiWiki::FailReason->new("no virus_checker configured");
156 # The file needs to be fed into the virus checker on stdin,
157 # because the file is not world-readable, and if clamdscan is
158 # used, clamd would fail to read it.
159 eval q{use IPC::Open2};
161 open (IN, "<", $file) || return IkiWiki::FailReason->new("failed to read file");
164 $SIG{PIPE} = sub { $sigpipe=1 };
165 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
166 my $reason=<CHECKER_OUT>;
168 1 while (<CHECKER_OUT>);
171 $SIG{PIPE}="DEFAULT";
172 if ($sigpipe || $?) {
173 if (! length $reason) {
174 $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
176 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
179 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
183 sub match_ispage ($$;@) { #{{{
186 if (defined IkiWiki::pagetype($filename)) {
187 return IkiWiki::SuccessReason->new("file is a wiki page");
190 return IkiWiki::FailReason->new("file is not a wiki page");