mason/dhandler (.contact): Determine the `count' strings in advance.
[tgal] / mason / dhandler
CommitLineData
6ac5dde2
MW
1%### -*-html-*-
2%###
3%### Main output for Trivial Gallery.
4%###
5%### (c) 2021 Mark Wooding
6%###
7%
8%###----- Licensing notice --------------------------------------------------
9%###
10%### This file is part of Trivial Gallery.
11%###
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.
16%###
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.
21%###
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/>.
25%
26%###-------------------------------------------------------------------------
27<%def .html>\
28% $r->content_type("text/html; charset=\"utf-8\"");
29<!DOCTYPE html>
30<!--
31Trivial Gallery, copyright © 2021 Mark Wooding.
32Free software: you can redistribute it and/or modify it under the terms
33of the GNU Affero General Public License.
34-->
35<html>
36<head>
37 <meta name=viewport content="width=device-width initial-scale=1.0">
988a1d43
MW
38 <script type="text/javascript" src="<% "$STATICURL/tgal.js" |hu %>" defer></script>
39 <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |hu %>">
6ac5dde2
MW
40<% $head %>\
41 <title><% $title %></title>
42</head>
43<body>
44<% $m->content %>
45</body>
46</html>\
47%
48<%args>
49 $title
50 $head => ""
51</%args>
52</%def>
53%
54%###-------------------------------------------------------------------------
55<%def .not-found>\
56<&| .html, title => "Not found" &>
57<h1>Not found</h1>
58Failed to find &lsquo;<% $path |h %>&rsquo;.
59</&>
60% return 404;
61%
62<%args>
63 $path
64</%args>
65</%def>
66%
67%###-------------------------------------------------------------------------
68<%def .contact>\
69<%perl>
458a80b2
MW
70 unless ($r->path_info =~ m!/$!)
71 { $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
72
6ac5dde2
MW
73 my $real = join_paths $IMGROOT, $path;
74 my $url = join_paths $SCRIPTURL, $path;
75 my ($dd, $ff, $ii) = listdir $real;
76 my $links = "";
77 my $uplink;
78 if ($path eq "" || $path eq "/") { $uplink = undef; }
79 else {
80 ($uplink = $path) =~ s![^/]*/$!!;
81 $links .= sprintf " <link rel=up href=\"%s\">\n",
82 urlencode "$SCRIPTURL/$uplink";
83 }
84 (my $nosl = $path) =~ s!/$!!;
6e749fab 85
f4b90914 86 my $size = "medthumb";
6e749fab 87 my %tn;
f6ac1767 88 my %count;
6e749fab 89 for my $f (@$ff)
ccaab4e8 90 { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
6e749fab 91 for my $d (@$dd) {
2fe5383f 92 my $p = join_paths $path, $d->name;
b0e94d8a
MW
93 my ($ddd, $fff, $iii);
94 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
f6ac1767
MW
95
96 my $count = "";
97 $count .= scalar(@$ddd) . "/" if @$ddd;
98 $count .= scalar(@$fff) if @$fff;
99 $count{$d} = $count;
100
2fe5383f 101 DIR: for (;;) {
2fe5383f
MW
102 if (defined $iii) {
103 my $index = join_paths $p, $iii->name;
ccaab4e8 104 $tn{$d} = TrivGal::Image->new($index)->scale($size);
2fe5383f
MW
105 last DIR;
106 }
107 if (!@$ddd) { $tn{$d} = undef; last DIR; }
108 $p = join_paths $p, $ddd->[0]->name;
b0e94d8a 109 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
2fe5383f 110 }
6e749fab 111 }
6ac5dde2
MW
112</%perl>
113%
3d7d20f8
MW
114<&| .html, title =>
115 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
6ac5dde2 116 head => $links &>
aa5c3a51
MW
117<&| .breadcrumbs, what => "Folder", path => $path &>
118 <div class="menu">
119 <a href="<% "$SCRIPTURL/" . substr($path, 0, -1) . ".zip" |hu %>">[zip]</a>
120 </div>
121</&>
6ac5dde2
MW
122%
123% my $note = contents "$IMGROOT/$path/.tgal-note.html";
124% if (defined $note) {
125<div class=note>
126<% $note %>
127</div>
128% }
129%
130% if (@$dd) {
131<h2>Subfolders</h2>
6e749fab 132<div class="gallery <% $size %>">
24f4eac3 133 <& .thumbnail, target => $d->name, comment => $d->comment,
ccaab4e8 134 tn => $tn{$d}, size => $size,
27ad11a2 135 caption =>
f6ac1767
MW
136 $m->interp->apply_escapes($d->name, "h") .
137 " [$count{$d}]" &>\
6e749fab 138% }
f4b90914 139</div>
6ac5dde2
MW
140% }
141%
142% if (@$ff) {
143<h2>Images</h2>
6e749fab 144<div class="gallery <% $size %>">
f4b90914 145% for my $f (@$ff) {
dfdd1964 146 <& .thumbnail, target => $f->name, comment => $f->comment,
ccaab4e8 147 tn => $tn{$f}, size => $size,
dfdd1964 148 caption => $m->interp->apply_escapes($f->name, "h") &>\
6e749fab 149% }
f4b90914 150</div>
6ac5dde2
MW
151% }
152%
153<div class=fill></div>
154<& .footer, path => $path &>
155</&>
156%
157<%args>
158 $path
159</%args>
160</%def>
161%
162%###-------------------------------------------------------------------------
aa5c3a51
MW
163<%def .zip>\
164<%perl>
165 my $st = stat "$IMGROOT/$path";
166 if (!$st) { $m->comp(".not-found", path => $path); return; }
167 my $zip = "$TMP/t$$-download.zip";
168 my $err = "$TMP/t$$-download.stderr";
169 my $kid = fork;
170 if (!$kid) {
171 untie *STDIN; open STDIN, "</dev/null";
172 untie *STDOUT; open STDOUT, ">/dev/null";
173 untie *STDERR; open STDERR, ">", $err;
174 chdir "$IMGROOT/$path";
175 exec "zip", "-qr", $zip, ".";
176 exit 127;
177 }
178 waitpid $kid, 0;
179</%perl>
180%
181% if ($?) {
182<&| .html, title => "Zip failed (rc = $?)" &>
183<pre>
184<%perl>
185 open my $f, "<", $err;
186 my $buf;
187 while (read $f, $buf, 16384) { $m->print($buf); }
188</%perl>
189</pre>
190</&>
191% } else {
192<%perl>
193 $r->content_type("application/zip");
194 open my $f, "<", $zip; binmode $f;
195 my $buf;
196 while (read $f, $buf, 16384) { $m->print($buf); }
197</%perl>
198% }
199%
200<%perl>
201 eval { unlink $zip; };
202 eval { unlink $err; };
203</%perl>
204
205%
206<%args>
207 $path
208</%args>
209</%def>
210%
211%###-------------------------------------------------------------------------
6ac5dde2
MW
212<%def .image>\
213<%perl>
214 my ($dir, $base, $ext) = split_path $path;
9601c4bb 215
bfc5bfe6
MW
216 if (defined $scale) {
217 my $img = TrivGal::Image->new($path);
784bdf8f 218 $m->redirect($img->scale($scale, 1));
bfc5bfe6 219 }
9601c4bb 220
bfc5bfe6 221 my $real = join_paths $IMGROOT, $path;
6ac5dde2
MW
222 my $url = join_paths $IMGURL, $path;
223 my $realdir = join_paths $IMGROOT, $dir;
224 my $urldir = join_paths $SCRIPTURL, $dir;
225 my ($dd, $ff, $ii) = listdir $realdir;
bfc5bfe6 226 my @thumbsz = qw{smallthumb medthumb bigthumb};
7cd805d1
MW
227 my @imgsz = sort { $SIZE{$a} <=> $SIZE{$b} } keys %SIZE;
228 my ($wd, $ht, $max);
bfc5bfe6 229 my %tn;
7cd805d1 230 my %vw;
6ac5dde2
MW
231
232 my $fi = undef;
bfc5bfe6
MW
233 FILE: for (my $i = 0; $i < @$ff; $i++) {
234 my $f = $ff->[$i];
235 my $img = TrivGal::Image->new(join_paths $dir, $f->name);
236 for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
7cd805d1
MW
237 if ($ff->[$i]->name eq "$base$ext") {
238 $fi = $i;
239 ($wd, $ht) = ($img->wd, $img->ht);
240 $max = $img->sz;
241 SIZE: for my $sc (@imgsz) {
242 my $sz = $SIZE{$sc};
243 last SIZE if $max < $sz;
244 $vw{$sc} = $img->scale($sc);
245 }
246 }
bfc5bfe6 247 }
6ac5dde2
MW
248 defined $fi or die "image not found in its folder?";
249 my $this = $ff->[$fi];
250
251 my %link;
252 $link{up} = "";
253 if ($fi != 0) {
254 $link{first} = $ff->[0]->name;
255 $link{prev} = $ff->[$fi - 1]->name;
256 }
257 if ($fi != @$ff - 1) {
258 $link{last} = $ff->[-1]->name;
259 $link{next} = $ff->[$fi + 1]->name;
260 }
261
262 my $links = "";
fd972456 263 my $pre = urlencode join_paths $SCRIPTURL, $dir;
6ac5dde2 264 for my $rel (qw{up first prev next last}) {
fd972456
MW
265 $links .= sprintf " <link rel=%s href=\"%s\">\n", $rel,
266 urlencode "$pre/$link{$rel}"
267 if exists $link{$rel};
6ac5dde2
MW
268 }
269</%perl>
270%
271<&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
272 head => $links &>
273<& .breadcrumbs, what => "Image", path => $path &>
274% if ($this->comment) {
275 <div class=comment>
276 <p><% $this->comment %>
277 </div>
278% }
279%
280<div class=viewnav>
281% if ($link{prev}) {
988a1d43 282 <div class=prev><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
6ac5dde2
MW
283% }
284 <a class=view href="<% $url |h %>">
7cd805d1
MW
285 <picture>
286% my ($hoff, $voff) = (60, 480);
287% SIZE: for (my $i = 0; $i < @imgsz; $i++) {
288% my $scale = $imgsz[$i];
289% last SIZE unless exists $vw{$scale};
290% my $scsz = $SIZE{$scale};
291% my $f = $scsz/$max;
292% my ($thiswd, $thisht) = map int, ($f*$wd + $hoff, $f*$ht + $voff);
293 <source srcset="<% $vw{$scale} |h %>"
294 media="(max-width: <% $thiswd %>px) or (max-height: <% $thisht %>px)">
295% }
296 <img src="<% "$IMGURL/$path" |hu %>">
297 </picture>
6ac5dde2
MW
298 </a>
299% if ($link{next}) {
988a1d43 300 <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>
6ac5dde2
MW
301% }
302</div>
303%
1408b7a2 304% for my $size (qw{smallthumb medthumb bigthumb}) {
6e749fab
MW
305<div class="thumbstrip <% $size %>">
306% for my $f (@$ff) {
ccaab4e8 307 <& .thumbnail, target => $f->name,
bfc5bfe6 308 tn => $tn{$f->name}{$size}, size => $size,
6ac5dde2 309 caption => $m->interp->apply_escapes($f->name, "h"),
62ee1c09 310 focus => $f eq $this &>\
6e749fab 311% }
6ac5dde2 312</div>
6e749fab 313% }
6ac5dde2
MW
314<& .footer, path => $dir &>
315</&>
316%
317<%args>
318 $path
9601c4bb 319 $scale => undef
6ac5dde2
MW
320</%args>
321</%def>
322%
323%###-------------------------------------------------------------------------
324<%def .breadcrumbs>\
325% $path =~ s!/$!!;
326% my @p = split m!/!, $path;
327% my $pp = "";
328% my $prev = undef;
329<h1><% $what %> \
330% if (!@p) {
331[top]
332% } else {
988a1d43 333<a href="<% $SCRIPTURL |hu %>/">[top]</a>&thinsp;/&thinsp;\
6ac5dde2
MW
334% STEP: for my $p (@p) {
335% if (defined $prev) {
336% $pp .= "$prev/";
988a1d43 337<a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
6ac5dde2
MW
338<% $prev %></a>&thinsp;/&thinsp;\
339% }
340% $prev = $p;
341% }
342<% $prev %>\
343% }
f3663b65
MW
344% if ($m->has_content) {
345
346<% $m->content %>\
347% }
6ac5dde2
MW
348</h1>
349<%args>
350 $what
351 $path
352</%args>
353</%def>
354%
355%###-------------------------------------------------------------------------
356<%def .thumbnail>\
ccaab4e8 357% $tn //= "$STATICURL/folder.svg";
6ac5dde2 358% if ($focus) {
1408b7a2 359 <figure class="thumb focusthumb <% $size %>">
1126ec40 360 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
83bfcbc7 361 <figcaption><span class=name><% $caption %></span></figcaption>
6ac5dde2 362% } else {
dfdd1964 363 <figure class="thumb <% $size %>">
1126ec40
MW
364 <a class=thumb href="<% $target |hu %>">
365 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
83bfcbc7 366 <figcaption>
6ac5dde2
MW
367 <span class=name><% $caption %></span>
368% if (defined $comment) {
369 <span class=comment><% $comment %></span>
370% }
83bfcbc7 371 </figcaption>
6ac5dde2
MW
372 </a>
373% }
83bfcbc7 374 </figure>
6ac5dde2
MW
375%
376<%args>
377 $target
ccaab4e8 378 $tn
dfdd1964 379 $size
6ac5dde2
MW
380 $caption
381 $comment => undef
382 $focus => 0
383</%args>
384</%def>
385%
386%###-------------------------------------------------------------------------
387<%def .footer>\
388<%perl>
389</%perl>
390<div class=footer>
391 <div class=footitem>
988a1d43 392 <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |hu %>"></a>
6ac5dde2
MW
393 Trivial Gallery, copyright &copy; 2021 Mark Wooding.
394 Free software: you can modify it and/or redistribute it under the
395 terms of the
a54108de 396 <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
6ac5dde2 397 General Public License version 3</a>.
a38a867d 398 Browse or download the <a href="<% $SRCURL %>">source code</a>.
6ac5dde2
MW
399 </div>
400% my $user =
401% find_covering_file $IMGROOT, $path, ".tgal-footer.html";
402% if (defined $user) {
403 <div class=footitem>
404<% $user %>
405 </div>
406% }
407</div>
408<%args>
409 $path
410</%args>
411</%def>
412%
413%###-------------------------------------------------------------------------
414<%once>
415 use autodie;
6ac5dde2
MW
416 use File::stat;
417
418 use TrivGal;
419</%once>
420%
421<%init>
422 TrivGal->init;
423
424 my $path = $m->dhandler_arg;
425 my $st = stat "$IMGROOT/$path";
426 my $comp;
aa5c3a51
MW
427 if (!$st) {
428 $comp = ".not-found";
429 if ($path =~ /^ (.*) (\.(?: zip)) $/x) {
430 $st = stat "$IMGROOT/$1";
431 if ($st) { $path = $1; $comp = $2; }
432 }
433 }
6ac5dde2
MW
434 elsif (-d $st) { $comp = ".contact"; }
435 elsif (-f $st) { $comp = ".image"; }
436 else { $comp = ".not-found"; }
ecf3c7a8 437 $r->header_out("X-AGPL-Source" => $SRCURL);
6abe8e59 438 $m->comp($comp, path => $path, %ARGS);
6ac5dde2
MW
439</%init>
440%
441%###----- That's all, folks -------------------------------------------------