summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/filecheck.pm
blob: 1855a8b48720baece395eeb06b4f2e1f84d701e4 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::filecheck;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my %units=( #{{{ # size in bytes
  7. B => 1,
  8. byte => 1,
  9. KB => 2 ** 10,
  10. kilobyte => 2 ** 10,
  11. K => 2 ** 10,
  12. KB => 2 ** 10,
  13. kilobyte => 2 ** 10,
  14. M => 2 ** 20,
  15. MB => 2 ** 20,
  16. megabyte => 2 ** 20,
  17. G => 2 ** 30,
  18. GB => 2 ** 30,
  19. gigabyte => 2 ** 30,
  20. T => 2 ** 40,
  21. TB => 2 ** 40,
  22. terabyte => 2 ** 40,
  23. P => 2 ** 50,
  24. PB => 2 ** 50,
  25. petabyte => 2 ** 50,
  26. E => 2 ** 60,
  27. EB => 2 ** 60,
  28. exabyte => 2 ** 60,
  29. Z => 2 ** 70,
  30. ZB => 2 ** 70,
  31. zettabyte => 2 ** 70,
  32. Y => 2 ** 80,
  33. YB => 2 ** 80,
  34. yottabyte => 2 ** 80,
  35. # ikiwiki, if you find you need larger data quantities, either modify
  36. # yourself to add them, or travel back in time to 2008 and kill me.
  37. # -- Joey
  38. ); #}}}
  39. sub parsesize ($) { #{{{
  40. my $size=shift;
  41. no warnings;
  42. my $base=$size+0; # force to number
  43. use warnings;
  44. foreach my $unit (sort keys %units) {
  45. if ($size=~/[0-9\s]\Q$unit\E$/i) {
  46. return $base * $units{$unit};
  47. }
  48. }
  49. return $base;
  50. } #}}}
  51. # This is provided for other plugins that want to convert back the other way.
  52. sub humansize ($) { #{{{
  53. my $size=shift;
  54. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  55. if ($size / $units{$unit} > 0.25) {
  56. return (int($size / $units{$unit} * 10)/10).$unit;
  57. }
  58. }
  59. return $size; # near zero, or negative
  60. } #}}}
  61. package IkiWiki::PageSpec;
  62. sub match_maxsize ($$;@) { #{{{
  63. my $page=shift;
  64. my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  65. if ($@) {
  66. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  67. }
  68. my %params=@_;
  69. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  70. if (! defined $file) {
  71. return IkiWiki::FailReason->new("no file specified");
  72. }
  73. if (-s $file > $maxsize) {
  74. return IkiWiki::FailReason->new("file too large (".(-s $file)." > $maxsize)");
  75. }
  76. else {
  77. return IkiWiki::SuccessReason->new("file not too large");
  78. }
  79. } #}}}
  80. sub match_minsize ($$;@) { #{{{
  81. my $page=shift;
  82. my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  83. if ($@) {
  84. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  85. }
  86. my %params=@_;
  87. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  88. if (! defined $file) {
  89. return IkiWiki::FailReason->new("no file specified");
  90. }
  91. if (-s $file < $minsize) {
  92. return IkiWiki::FailReason->new("file too small");
  93. }
  94. else {
  95. return IkiWiki::SuccessReason->new("file not too small");
  96. }
  97. } #}}}
  98. sub match_mimetype ($$;@) { #{{{
  99. my $page=shift;
  100. my $wanted=shift;
  101. my %params=@_;
  102. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  103. if (! defined $file) {
  104. return IkiWiki::FailReason->new("no file specified");
  105. }
  106. # Use ::magic to get the mime type, the idea is to only trust
  107. # data obtained by examining the actual file contents.
  108. eval q{use File::MimeInfo::Magic};
  109. if ($@) {
  110. return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
  111. }
  112. my $mimetype=File::MimeInfo::Magic::magic($file);
  113. if (! defined $mimetype) {
  114. $mimetype="unknown";
  115. }
  116. my $regexp=IkiWiki::glob2re($wanted);
  117. if ($mimetype!~/^$regexp$/i) {
  118. return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
  119. }
  120. else {
  121. return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
  122. }
  123. } #}}}
  124. sub match_virusfree ($$;@) { #{{{
  125. my $page=shift;
  126. my $wanted=shift;
  127. my %params=@_;
  128. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  129. if (! defined $file) {
  130. return IkiWiki::FailReason->new("no file specified");
  131. }
  132. if (! exists $IkiWiki::config{virus_checker} ||
  133. ! length $IkiWiki::config{virus_checker}) {
  134. return IkiWiki::FailReason->new("no virus_checker configured");
  135. }
  136. # The file needs to be fed into the virus checker on stdin,
  137. # because the file is not world-readable, and if clamdscan is
  138. # used, clamd would fail to read it.
  139. eval q{use IPC::Open2};
  140. error($@) if $@;
  141. open (IN, "<", $file) || return IkiWiki::FailReason->new("failed to read file");
  142. binmode(IN);
  143. my $sigpipe=0;
  144. $SIG{PIPE} = sub { $sigpipe=1 };
  145. my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
  146. my $reason=<CHECKER_OUT>;
  147. chomp $reason;
  148. 1 while (<CHECKER_OUT>);
  149. close(CHECKER_OUT);
  150. waitpid $pid, 0;
  151. $SIG{PIPE}="DEFAULT";
  152. if ($sigpipe || $?) {
  153. if (! length $reason) {
  154. $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
  155. }
  156. return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
  157. }
  158. else {
  159. return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
  160. }
  161. } #}}}
  162. sub match_ispage ($$;@) { #{{{
  163. my $filename=shift;
  164. if (defined IkiWiki::pagetype($filename)) {
  165. return IkiWiki::SuccessReason->new("file is a wiki page");
  166. }
  167. else {
  168. return IkiWiki::FailReason->new("file is not a wiki page");
  169. }
  170. } #}}}