mason/.perl-lib/TrivGal.pm (%SIZE): Modify and extend the available sizes.
[tgal] / mason / dhandler
index 44ab7ed..611567a 100755 (executable)
@@ -35,8 +35,8 @@ of the GNU Affero General Public License.
 <html>
 <head>
   <meta name=viewport content="width=device-width initial-scale=1.0">
-  <script type="text/javascript" src="<% "$STATICURL/tgal.js" |u %>" defer></script>
-  <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |u %>">
+  <script type="text/javascript" src="<% "$STATICURL/tgal.js" |hu %>" defer></script>
+  <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |hu %>">
 <% $head %>\
   <title><% $title %></title>
 </head>
@@ -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) {
@@ -154,6 +158,55 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 </%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
+</%args>
+</%def>
+%
+%###-------------------------------------------------------------------------
 <%def .image>\
 <%perl>
        my ($dir, $base, $ext) = split_path $path;
@@ -178,7 +231,7 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
          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"); }
+           { $fi = $i; $vw = $img->scale("medium"); }
        }
        defined $fi or die "image not found in its folder?";
        my $this = $ff->[$fi];
@@ -215,13 +268,13 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %
 <div class=viewnav>
 % if ($link{prev}) {
-  <div class=prev><a class=prev href="<% "$pre/$link{prev}" |u %>">&lsaquo;</a></div>
+  <div class=prev><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
 % }
   <a class=view href="<% $url |h %>">
     <img src="<% $vw |h %>">
   </a>
 % if ($link{next}) {
-  <div class=next><a class=next href="<% "$pre/$link{next}" |u %>">&rsaquo;</a></div>
+  <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>
 % }
 </div>
 %
@@ -254,17 +307,21 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 % if (!@p) {
 [top]
 % } else {
-<a href="<% $SCRIPTURL |u %>/">[top]</a>&thinsp;/&thinsp;\
+<a href="<% $SCRIPTURL |hu %>/">[top]</a>&thinsp;/&thinsp;\
 %   STEP: for my $p (@p) {
 %     if (defined $prev) {
 %       $pp .= "$prev/";
-<a href="<% join_paths($SCRIPTURL, $pp) |u %>/">\
+<a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
 <% $prev %></a>&thinsp;/&thinsp;\
 %     }
 %     $prev = $p;
 %   }
 <% $prev %>\
 % }
+% if ($m->has_content) {
+
+<% $m->content %>\
+% }
 </h1>
 <%args>
        $what
@@ -277,12 +334,12 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 % $tn //= "$STATICURL/folder.svg";
 % if ($focus) {
   <figure class="thumb focusthumb <% $size %>">
-    <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
+    <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
     <figcaption><span class=name><% $caption %></span></figcaption>
 % } else {
   <figure class="thumb <% $size %>">
-    <a class=thumb href="<% $target |u %>">
-      <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
+    <a class=thumb href="<% $target |hu %>">
+      <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
       <figcaption>
        <span class=name><% $caption %></span>
 % if (defined $comment) {
@@ -309,7 +366,7 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 </%perl>
 <div class=footer>
   <div class=footitem>
-    <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |u %>"></a>
+    <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |hu %>"></a>
     Trivial Gallery, copyright &copy; 2021 Mark Wooding.
     Free software: you can modify it and/or redistribute it under the
     terms of the
@@ -344,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"; }