chiark / gitweb /
mason/dhandler, static/tgal.css: Add option to download zipfiles.
[tgal] / mason / dhandler
index f523432544c7347d722ebf41464597c83800d26b..49e5f847b23c0371e3252763aebd3fcc6ab72728 100755 (executable)
@@ -109,7 +109,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) {
@@ -147,6 +151,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
@@ -348,7 +401,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"; }