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