+sub attachment_location ($) {
+ my $page=shift;
+
+ # Put the attachment in a subdir of the page it's attached
+ # to, unless that page is an "index" page.
+ $page=~s/(^|\/)index//;
+ $page.="/" if length $page;
+
+ return $page;
+}
+
+sub attachment_list ($) {
+ my $page=shift;
+ my $loc=attachment_location($page);
+
+ my @ret;
+ foreach my $f (values %pagesources) {
+ if (! defined IkiWiki::pagetype($f) &&
+ $f=~m/^\Q$loc\E[^\/]+$/ &&
+ -e "$config{srcdir}/$f") {
+ push @ret, {
+ "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
+ link => htmllink($page, $page, $f, noimageinline => 1),
+ size => humansize((stat(_))[7]),
+ mtime => displaytime($IkiWiki::pagemtime{$f}),
+ mtime_raw => $IkiWiki::pagemtime{$f},
+ };
+ }
+ }
+
+ # Sort newer attachments to the top of the list, so a newly-added
+ # attachment appears just before the form used to add it.
+ return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
+}
+
+my %units=( # size in bytes
+ B => 1,
+ byte => 1,
+ KB => 2 ** 10,
+ kilobyte => 2 ** 10,
+ K => 2 ** 10,
+ KB => 2 ** 10,
+ kilobyte => 2 ** 10,
+ M => 2 ** 20,
+ MB => 2 ** 20,
+ megabyte => 2 ** 20,
+ G => 2 ** 30,
+ GB => 2 ** 30,
+ gigabyte => 2 ** 30,
+ T => 2 ** 40,
+ TB => 2 ** 40,
+ terabyte => 2 ** 40,
+ P => 2 ** 50,
+ PB => 2 ** 50,
+ petabyte => 2 ** 50,
+ E => 2 ** 60,
+ EB => 2 ** 60,
+ exabyte => 2 ** 60,
+ Z => 2 ** 70,
+ ZB => 2 ** 70,
+ zettabyte => 2 ** 70,
+ Y => 2 ** 80,
+ YB => 2 ** 80,
+ yottabyte => 2 ** 80,
+ # ikiwiki, if you find you need larger data quantities, either modify
+ # yourself to add them, or travel back in time to 2008 and kill me.
+ # -- Joey
+);