3 %### Main output for Trivial Gallery.
5 %### (c) 2021 Mark Wooding
8 %###----- Licensing notice --------------------------------------------------
10 %### This file is part of Trivial Gallery.
12 %### Trivial Gallery is free software: you can redistribute it and/or modify
13 %### it under the terms of the GNU Affero General Public License as
14 %### published by the Free Software Foundation; either version 3 of the
15 %### License, or (at your option) any later version.
17 %### Trivial Gallery is distributed in the hope that it will be useful, but
18 %### WITHOUT ANY WARRANTY; without even the implied warranty of
19 %### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 %### Affero General Public License for more details.
22 %### You should have received a copy of the GNU Affero General Public
23 %### License along with Trivial Gallery. If not, see
24 %### <https://www.gnu.org/licenses/>.
26 %###-------------------------------------------------------------------------
28 % $r->content_type("text/html; charset=\"utf-8\"");
31 Trivial Gallery, copyright © 2021 Mark Wooding.
32 Free software: you can redistribute it and/or modify it under the terms
33 of the GNU Affero General Public License.
37 <meta name=viewport content="width=device-width initial-scale=1.0">
38 <script type="text/javascript" src="<% "$STATICURL/tgal.js" |hu %>" defer></script>
39 <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |hu %>">
41 <title><% $title %></title>
54 %###-------------------------------------------------------------------------
56 <&| .html, title => "Not found" &>
58 Failed to find ‘<% $path |h %>’.
67 %###-------------------------------------------------------------------------
70 unless ($r->path_info =~ m!/$!)
71 { $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
73 my $real = join_paths $IMGROOT, $path;
74 my $url = join_paths $SCRIPTURL, $path;
75 my ($dd, $ff, $ii) = listdir $real;
78 if ($path eq "" || $path eq "/") { $uplink = undef; }
80 ($uplink = $path) =~ s![^/]*/$!!;
81 $links .= sprintf " <link rel=up href=\"%s\">\n",
82 urlencode "$SCRIPTURL/$uplink";
84 (my $nosl = $path) =~ s!/$!!;
86 my @size = ("smallthumb", "medthumb", "bigthumb");
90 my $img = TrivGal::Image->new($path . $f->name);
91 for my $size (@size) { $tn{$f}{$size} = $img->scale($size); }
94 my $p = join_paths $path, $d->name;
95 my ($ddd, $fff, $iii);
96 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
99 $count .= scalar(@$ddd) . "/" if @$ddd;
100 $count .= scalar(@$fff) if @$fff;
105 my $index = join_paths $p, $iii->name;
106 my $img = TrivGal::Image->new($index);
107 for my $size (@size) { $tn{$d}{$size} = $img->scale($size); }
110 if (!@$ddd) { $tn{$d} = undef; last DIR; }
111 $p = join_paths $p, $ddd->[0]->name;
112 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
118 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
120 <&| .breadcrumbs, what => "Folder", path => $path &>
122 <a href="<% "$SCRIPTURL/" . substr($path, 0, -1) . ".zip" |hu %>">[zip]</a>
126 % my $note = contents "$IMGROOT/$path/.tgal-note.html";
127 % if (defined $note) {
135 % for my $size (@size) {
136 <div class="gallery <% $size %>">
138 <& .thumbnail, target => $d->name, comment => $d->comment,
139 tn => $tn{$d}{$size}, size => $size,
141 $m->interp->apply_escapes($d->name, "h") .
150 % for my $size (@size) {
151 <div class="gallery <% $size %>">
153 <& .thumbnail, target => $f->name, comment => $f->comment,
154 tn => $tn{$f}{$size}, size => $size,
155 caption => $m->interp->apply_escapes($f->name, "h") &>\
161 <div class=fill></div>
162 <& .footer, path => $path &>
170 %###-------------------------------------------------------------------------
173 my $st = stat "$IMGROOT/$path";
174 if (!$st) { $m->comp(".not-found", path => $path); return; }
175 my $zip = "$TMP/t$$-download.zip";
176 my $err = "$TMP/t$$-download.stderr";
179 untie *STDIN; open STDIN, "</dev/null";
180 untie *STDOUT; open STDOUT, ">/dev/null";
181 untie *STDERR; open STDERR, ">", $err;
182 chdir "$IMGROOT/$path";
183 exec "zip", "-qr", $zip, ".";
190 <&| .html, title => "Zip failed (rc = $?)" &>
193 open my $f, "<", $err;
195 while (read $f, $buf, 16384) { $m->print($buf); }
201 $r->content_type("application/zip");
202 open my $f, "<", $zip; binmode $f;
204 while (read $f, $buf, 16384) { $m->print($buf); }
209 eval { unlink $zip; };
210 eval { unlink $err; };
219 %###-------------------------------------------------------------------------
222 my ($dir, $base, $ext) = split_path $path;
224 if (defined $scale) {
225 my $img = TrivGal::Image->new($path);
226 $m->redirect($img->scale($scale, 1));
229 my $real = join_paths $IMGROOT, $path;
230 my $url = join_paths $IMGURL, $path;
231 my $realdir = join_paths $IMGROOT, $dir;
232 my $urldir = join_paths $SCRIPTURL, $dir;
233 my ($dd, $ff, $ii) = listdir $realdir;
234 my @thumbsz = qw{smallthumb medthumb bigthumb};
235 my @imgsz = sort { $SIZE{$a} <=> $SIZE{$b} } keys %SIZE;
241 FILE: for (my $i = 0; $i < @$ff; $i++) {
243 my $img = TrivGal::Image->new(join_paths $dir, $f->name);
244 for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
245 if ($ff->[$i]->name eq "$base$ext") {
247 ($wd, $ht) = ($img->wd, $img->ht);
249 SIZE: for my $sc (@imgsz) {
251 last SIZE if $max < $sz;
252 $vw{$sc} = $img->scale($sc);
256 defined $fi or die "image not found in its folder?";
257 my $this = $ff->[$fi];
262 $link{first} = $ff->[0]->name;
263 $link{prev} = $ff->[$fi - 1]->name;
265 if ($fi != @$ff - 1) {
266 $link{last} = $ff->[-1]->name;
267 $link{next} = $ff->[$fi + 1]->name;
271 my $pre = urlencode join_paths $SCRIPTURL, $dir;
272 for my $rel (qw{up first prev next last}) {
273 $links .= sprintf " <link rel=%s href=\"%s\">\n", $rel,
274 urlencode "$pre/$link{$rel}"
275 if exists $link{$rel};
279 <&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
281 <& .breadcrumbs, what => "Image", path => $path &>
282 % if ($this->comment) {
284 <p><% $this->comment %>
291 <a class=nav title="Previous image" href="<% "$pre/$link{prev}" |hu %>">
292 <svg width="50" height="80" viewBox="-25 -40 50 80">
293 <path class="fg" stroke="none"
294 d="m+1,0 +6,-11 -2,-1 -12,+12 +12,+12 +2,-1 z"/>
299 <a class=view href="<% $url |h %>">
301 % my ($hoff, $voff) = (60, 480);
302 % SIZE: for (my $i = 0; $i < @imgsz; $i++) {
303 % my $scale = $imgsz[$i];
304 % last SIZE unless exists $vw{$scale};
305 % my $scsz = $SIZE{$scale};
306 % my $f = $scsz/$max;
307 % my ($thiswd, $thisht) = map int, ($f*$wd + $hoff, $f*$ht + $voff);
308 <source srcset="<% $vw{$scale} |h %>"
309 media="(max-width: <% $thiswd %>px) or (max-height: <% $thisht %>px)">
311 <img src="<% "$IMGURL/$path" |hu %>">
316 <a class=nav title="Next image" href="<% "$pre/$link{next}" |hu %>">
317 <svg width="50" height="80" viewBox="-25 -40 50 80">
318 <path class="fg" stroke="none"
319 d="m-1,0 -6,-11 +2,-1 +12,+12 -12,+12 -2,-1 z"/>
326 % for my $size (qw{smallthumb medthumb bigthumb}) {
327 <div class="thumbstrip <% $size %>">
329 <& .thumbnail, target => $f->name,
330 tn => $tn{$f->name}{$size}, size => $size,
331 caption => $m->interp->apply_escapes($f->name, "h"),
332 focus => $f eq $this &>\
336 <& .footer, path => $dir &>
345 %###-------------------------------------------------------------------------
348 % my @p = split m!/!, $path;
355 <a href="<% $SCRIPTURL |hu %>/">[top]</a> / \
356 % STEP: for my $p (@p) {
357 % if (defined $prev) {
359 <a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
360 <% $prev %></a> / \
366 % if ($m->has_content) {
377 %###-------------------------------------------------------------------------
379 % $tn //= "$STATICURL/folder.svg";
381 <figure class="thumb focusthumb <% $size %>">
382 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
383 <figcaption><span class=name><% $caption %></span></figcaption>
385 <figure class="thumb <% $size %>">
386 <a class=thumb href="<% $target |hu %>">
387 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
389 <span class=name><% $caption %></span>
390 % if (defined $comment) {
391 <span class=comment><% $comment %></span>
408 %###-------------------------------------------------------------------------
414 <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |hu %>"></a>
415 Trivial Gallery, copyright © 2021 Mark Wooding.
416 Free software: you can modify it and/or redistribute it under the
418 <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
419 General Public License version 3</a>.
420 Browse or download the <a href="<% $SRCURL %>">source code</a>.
423 % find_covering_file $IMGROOT, $path, ".tgal-footer.html";
424 % if (defined $user) {
435 %###-------------------------------------------------------------------------
439 require autodie; autodie->import;
440 require File::stat; File::stat->import;
442 require TrivGal; TrivGal->import;
444 if ($@) { print "$@"; exit 2; }
452 my $path = $m->dhandler_arg;
453 my $st = stat "$IMGROOT/$path";
456 $comp = ".not-found";
457 if ($path =~ /^ (.*) (\.(?: zip)) $/x) {
458 $st = stat "$IMGROOT/$1";
459 if ($st) { $path = $1; $comp = $2; }
462 elsif (-d $st) { $comp = ".contact"; }
463 elsif (-f $st) { $comp = ".image"; }
464 else { $comp = ".not-found"; }
465 $r->header_out("X-AGPL-Source" => $SRCURL);
466 $m->comp($comp, path => $path, %ARGS);
469 %###----- That's all, folks -------------------------------------------------