chiark / gitweb /
mason/dhandler (.contact): Determine the `count' strings in advance.
[tgal] / mason / dhandler
index 6a81e78cd486928b22b972837e01f76ffe0384a7..78f0f951fe2aa02e44d02b7a1eb8ec30a54b731d 100755 (executable)
@@ -85,14 +85,19 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 
        my $size = "medthumb";
        my %tn;
-       my (%nd, %nf);
+       my %count;
        for my $f (@$ff)
          { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
        for my $d (@$dd) {
          my $p = join_paths $path, $d->name;
          my ($ddd, $fff, $iii);
          ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
-         $nd{$d} = @$ddd; $nf{$d} = @$fff;
+
+         my $count = "";
+         $count .= scalar(@$ddd) . "/" if @$ddd;
+         $count .= scalar(@$fff) if @$fff;
+         $count{$d} = $count;
+
          DIR: for (;;) {
            if (defined $iii) {
              my $index = join_paths $p, $iii->name;
@@ -109,7 +114,11 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 <&| .html, title =>
             "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
           head => $links &>
-<& .breadcrumbs, what => "Folder", path => $path &>
+<&| .breadcrumbs, what => "Folder", path => $path &>
+  <div class="menu">
+    <a href="<% "$SCRIPTURL/" . substr($path, 0, -1) . ".zip" |hu %>">[zip]</a>
+  </div>
+</&>
 %
 % my $note = contents "$IMGROOT/$path/.tgal-note.html";
 % if (defined $note) {
@@ -121,14 +130,11 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 % if (@$dd) {
 <h2>Subfolders</h2>
 <div class="gallery <% $size %>">
-%   for my $d (@$dd) {
-%     my $count = "";
-%     $count .= "$nd{$d}/" if $nd{$d};
-%     $count .= "$nf{$d}" if $nf{$d};
   <& .thumbnail, target => $d->name, comment => $d->comment,
                 tn => $tn{$d}, size => $size,
                 caption =>
-                  $m->interp->apply_escapes($d->name, "h") . " [$count]" &>\
+                  $m->interp->apply_escapes($d->name, "h") .
+                  " [$count{$d}]" &>\
 %   }
 </div>
 % }
@@ -147,6 +153,55 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 <div class=fill></div>
 <& .footer, path => $path &>
 </&>
+%
+<%args>
+       $path
+</%args>
+</%def>
+%
+%###-------------------------------------------------------------------------
+<%def .zip>\
+<%perl>
+       my $st = stat "$IMGROOT/$path";
+       if (!$st) { $m->comp(".not-found", path => $path); return; }
+       my $zip = "$TMP/t$$-download.zip";
+       my $err = "$TMP/t$$-download.stderr";
+       my $kid = fork;
+       if (!$kid) {
+         untie *STDIN; open STDIN, "</dev/null";
+         untie *STDOUT; open STDOUT, ">/dev/null";
+         untie *STDERR; open STDERR, ">", $err;
+         chdir "$IMGROOT/$path";
+         exec "zip", "-qr", $zip, ".";
+         exit 127;
+       }
+       waitpid $kid, 0;
+</%perl>
+%
+% if ($?) {
+<&| .html, title => "Zip failed (rc = $?)" &>
+<pre>
+<%perl>
+       open my $f, "<", $err;
+       my $buf;
+       while (read $f, $buf, 16384) { $m->print($buf); }
+</%perl>
+</pre>
+</&>
+% } else {
+<%perl>
+       $r->content_type("application/zip");
+       open my $f, "<", $zip; binmode $f;
+       my $buf;
+       while (read $f, $buf, 16384) { $m->print($buf); }
+</%perl>
+% }
+%
+<%perl>
+       eval { unlink $zip; };
+       eval { unlink $err; };
+</%perl>
+
 %
 <%args>
        $path
@@ -169,16 +224,26 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        my $urldir = join_paths $SCRIPTURL, $dir;
        my ($dd, $ff, $ii) = listdir $realdir;
        my @thumbsz = qw{smallthumb medthumb bigthumb};
+       my @imgsz = sort { $SIZE{$a} <=> $SIZE{$b} } keys %SIZE;
+       my ($wd, $ht, $max);
        my %tn;
-       my $vw;
+       my %vw;
 
        my $fi = undef;
        FILE: for (my $i = 0; $i < @$ff; $i++) {
          my $f = $ff->[$i];
          my $img = TrivGal::Image->new(join_paths $dir, $f->name);
          for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
-         if ($ff->[$i]->name eq "$base$ext")
-           { $fi = $i; $vw = $img->scale("view"); }
+         if ($ff->[$i]->name eq "$base$ext") {
+           $fi = $i;
+           ($wd, $ht) = ($img->wd, $img->ht);
+           $max = $img->sz;
+           SIZE: for my $sc (@imgsz) {
+             my $sz = $SIZE{$sc};
+             last SIZE if $max < $sz;
+             $vw{$sc} = $img->scale($sc);
+           }
+         }
        }
        defined $fi or die "image not found in its folder?";
        my $this = $ff->[$fi];
@@ -195,12 +260,11 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        }
 
        my $links = "";
-       my $pre =
-         urlencode join_paths $SCRIPTURL, $dir;
+       my $pre = urlencode join_paths $SCRIPTURL, $dir;
        for my $rel (qw{up first prev next last}) {
-         exists $link{$rel} and 
-           $links .= sprintf "  <link rel=%s href=\"%s\">\n",
-             $rel, urlencode "$pre/$link{$rel}";
+         $links .= sprintf "  <link rel=%s href=\"%s\">\n", $rel,
+                           urlencode "$pre/$link{$rel}"
+           if exists $link{$rel};
        }
 </%perl>
 %
@@ -218,7 +282,19 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
   <div class=prev><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
 % }
   <a class=view href="<% $url |h %>">
-    <img src="<% $vw |h %>">
+    <picture>
+% my ($hoff, $voff) = (60, 480);
+% SIZE: for (my $i = 0; $i < @imgsz; $i++) {
+%   my $scale = $imgsz[$i];
+%   last SIZE unless exists $vw{$scale};
+%   my $scsz = $SIZE{$scale};
+%   my $f = $scsz/$max;
+%   my ($thiswd, $thisht) = map int, ($f*$wd + $hoff, $f*$ht + $voff);
+      <source srcset="<% $vw{$scale} |h %>"
+             media="(max-width: <% $thiswd %>px) or (max-height: <% $thisht %>px)">
+% }
+      <img src="<% "$IMGURL/$path" |hu %>">
+    </picture>
   </a>
 % if ($link{next}) {
   <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>
@@ -265,6 +341,10 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %   }
 <% $prev %>\
 % }
+% if ($m->has_content) {
+
+<% $m->content %>\
+% }
 </h1>
 <%args>
        $what
@@ -344,7 +424,13 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        my $path = $m->dhandler_arg;
        my $st = stat "$IMGROOT/$path";
        my $comp;
-       if (!$st) { $comp = ".not-found"; }
+       if (!$st) {
+         $comp = ".not-found";
+         if ($path =~ /^ (.*) (\.(?: zip)) $/x) {
+           $st = stat "$IMGROOT/$1";
+           if ($st) { $path = $1; $comp = $2; }
+         }
+       }
        elsif (-d $st) { $comp = ".contact"; }
        elsif (-f $st) { $comp = ".image"; }
        else { $comp = ".not-found"; }