mason/dhandler (.contact): Report the number of subfolders and images.
[tgal] / mason / dhandler
index 2383d6b..44ab7ed 100755 (executable)
@@ -67,9 +67,9 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %###-------------------------------------------------------------------------
 <%def .contact>\
 <%perl>
-       unless ($r->path_info =~ m!/$!) {
-         $m->redirect(join_paths($SCRIPTURL, $path) . "/");
-       }
+       unless ($r->path_info =~ m!/$!)
+         { $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
+
        my $real = join_paths $IMGROOT, $path;
        my $url = join_paths $SCRIPTURL, $path;
        my ($dd, $ff, $ii) = listdir $real;
@@ -83,18 +83,31 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        }
        (my $nosl = $path) =~ s!/$!!;
 
+       my $size = "medthumb";
        my %tn;
+       my (%nd, %nf);
        for my $f (@$ff)
-         { $tn{$f} = TrivGal::Image->new($path . $f->name); }
+         { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
        for my $d (@$dd) {
-         my ($ddd, $fff, $iii) = listdir $real . "/" . $d->name;
-         $tn{$d} = $iii ?
-           TrivGal::Image->new(join_paths $path, $d->name, $iii->name) :
-           undef;
+         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;
+         DIR: for (;;) {
+           if (defined $iii) {
+             my $index = join_paths $p, $iii->name;
+             $tn{$d} = TrivGal::Image->new($index)->scale($size);
+             last DIR;
+           }
+           if (!@$ddd) { $tn{$d} = undef; last DIR; }
+           $p = join_paths $p, $ddd->[0]->name;
+           ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
+         }
        }
 </%perl>
 %
-<&| .html, title => "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
+<&| .html, title =>
+            "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
           head => $links &>
 <& .breadcrumbs, what => "Folder", path => $path &>
 %
@@ -107,28 +120,28 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %
 % if (@$dd) {
 <h2>Subfolders</h2>
-%   for my $size (qw{medthumb}) {
 <div class="gallery <% $size %>">
-%     for my $d (@$dd) {
-  <& .thumbnail, target => $d->name . "/", comment => $d->comment,
-                img => $tn{$d}, size => $size,
-                caption => $m->interp->apply_escapes($d->name, "h") &>\
-%     }
-</div>
+%   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]" &>\
 %   }
+</div>
 % }
 %
 % if (@$ff) {
 <h2>Images</h2>
-%   for my $size (qw{medthumb}) {
 <div class="gallery <% $size %>">
-%     for my $f (@$ff) {
+%   for my $f (@$ff) {
   <& .thumbnail, target => $f->name, comment => $f->comment,
-                img => $tn{$f}, size => "bigthumb",
+                tn => $tn{$f}, size => $size,
                 caption => $m->interp->apply_escapes($f->name, "h") &>\
-%     }
-</div>
 %   }
+</div>
 % }
 %
 <div class=fill></div>
@@ -144,16 +157,29 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 <%def .image>\
 <%perl>
        my ($dir, $base, $ext) = split_path $path;
+
+       if (defined $scale) {
+         my $img = TrivGal::Image->new($path);
+         $m->redirect($img->scale($scale, 1));
+       }
+
        my $real = join_paths $IMGROOT, $path;
        my $url = join_paths $IMGURL, $path;
        my $realdir = join_paths $IMGROOT, $dir;
        my $urldir = join_paths $SCRIPTURL, $dir;
        my ($dd, $ff, $ii) = listdir $realdir;
-       my $vw = TrivGal::Image->new($path)->scale("view");
+       my @thumbsz = qw{smallthumb medthumb bigthumb};
+       my %tn;
+       my $vw;
 
        my $fi = undef;
-       FILE: for (my $i = 0; $i < @$ff; $i++)
-         { if ($ff->[$i]->name eq "$base$ext") { $fi = $i; last FILE; } }
+       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"); }
+       }
        defined $fi or die "image not found in its folder?";
        my $this = $ff->[$fi];
 
@@ -199,13 +225,13 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 % }
 </div>
 %
-% my %img = map { $_ => TrivGal::Image->new($dir . "/" . $_->name) } @$ff;
 % for my $size (qw{smallthumb medthumb bigthumb}) {
 <div class="thumbstrip <% $size %>">
 %   for my $f (@$ff) {
-  <& .thumbnail, target => $f->name, img => $img{$f}, size => $size,
+  <& .thumbnail, target => $f->name,
+                tn => $tn{$f->name}{$size}, size => $size,
                 caption => $m->interp->apply_escapes($f->name, "h"),
-                focus => $f->name eq "$base$ext" &>\
+                focus => $f eq $this &>\
 %   }
 </div>
 % }
@@ -214,6 +240,7 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %
 <%args>
        $path
+       $scale => undef
 </%args>
 </%def>
 %
@@ -247,17 +274,15 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %
 %###-------------------------------------------------------------------------
 <%def .thumbnail>\
-% my $tn;
-% if (defined $img) { $tn = $img->scale($size); }
-% else { $tn = "$STATICURL/folder.svg"; }
+% $tn //= "$STATICURL/folder.svg";
 % if ($focus) {
   <figure class="thumb focusthumb <% $size %>">
-    <img class="thumb <% $size %>" load=lazy src="<% $tn |u %>">
+    <img class="thumb <% $size %>" load=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 |u %>">
+      <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
       <figcaption>
        <span class=name><% $caption %></span>
 % if (defined $comment) {
@@ -270,7 +295,7 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %
 <%args>
        $target
-       $img
+       $tn
        $size
        $caption
        $comment => undef
@@ -308,7 +333,6 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 %###-------------------------------------------------------------------------
 <%once>
        use autodie;
-use Data::Dumper;
        use File::stat;
 
        use TrivGal;
@@ -325,7 +349,7 @@ use Data::Dumper;
        elsif (-f $st) { $comp = ".image"; }
        else { $comp = ".not-found"; }
         $r->header_out("X-AGPL-Source" => $SRCURL);
-       $m->comp($comp, path => $path);
+       $m->comp($comp, path => $path, %ARGS);
 </%init>
 %
 %###----- That's all, folks -------------------------------------------------