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