mason/dhandler (.view), static/tgal.css: Use SVG arrows.
[tgal] / mason / dhandler
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 <!--
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.
34 -->
35 <html>
36 <head>
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 %>">
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>
58 Failed 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>
70 unless ($r->path_info =~ m!/$!)
71 { $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
72
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!/$!!;
85
86 my @size = ("smallthumb", "medthumb", "bigthumb");
87 my %tn;
88 my %count;
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 }
93 for my $d (@$dd) {
94 my $p = join_paths $path, $d->name;
95 my ($ddd, $fff, $iii);
96 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
97
98 my $count = "";
99 $count .= scalar(@$ddd) . "/" if @$ddd;
100 $count .= scalar(@$fff) if @$fff;
101 $count{$d} = $count;
102
103 DIR: for (;;) {
104 if (defined $iii) {
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); }
108 last DIR;
109 }
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;
113 }
114 }
115 </%perl>
116 %
117 <&| .html, title =>
118 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
119 head => $links &>
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 </&>
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>
135 % for my $size (@size) {
136 <div class="gallery <% $size %>">
137 % for my $d (@$dd) {
138 <& .thumbnail, target => $d->name, comment => $d->comment,
139 tn => $tn{$d}{$size}, size => $size,
140 caption =>
141 $m->interp->apply_escapes($d->name, "h") .
142 " [$count{$d}]" &>\
143 % }
144 </div>
145 % }
146 % }
147 %
148 % if (@$ff) {
149 <h2>Images</h2>
150 % for my $size (@size) {
151 <div class="gallery <% $size %>">
152 % for my $f (@$ff) {
153 <& .thumbnail, target => $f->name, comment => $f->comment,
154 tn => $tn{$f}{$size}, size => $size,
155 caption => $m->interp->apply_escapes($f->name, "h") &>\
156 % }
157 </div>
158 % }
159 % }
160 %
161 <div class=fill></div>
162 <& .footer, path => $path &>
163 </&>
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
213 %
214 <%args>
215 $path
216 </%args>
217 </%def>
218 %
219 %###-------------------------------------------------------------------------
220 <%def .image>\
221 <%perl>
222 my ($dir, $base, $ext) = split_path $path;
223
224 if (defined $scale) {
225 my $img = TrivGal::Image->new($path);
226 $m->redirect($img->scale($scale, 1));
227 }
228
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;
236 my ($wd, $ht, $max);
237 my %tn;
238 my %vw;
239
240 my $fi = undef;
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); }
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 }
255 }
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 = "";
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};
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}) {
290 <div class=prev>
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"/>
295 </svg>
296 </a>
297 </div>
298 % }
299 <a class=view href="<% $url |h %>">
300 <picture>
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)">
310 % }
311 <img src="<% "$IMGURL/$path" |hu %>">
312 </picture>
313 </a>
314 % if ($link{next}) {
315 <div class=next>
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"/>
320 </svg>
321 </a>
322 </div>
323 % }
324 </div>
325 %
326 % for my $size (qw{smallthumb medthumb bigthumb}) {
327 <div class="thumbstrip <% $size %>">
328 % for my $f (@$ff) {
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 &>\
333 % }
334 </div>
335 % }
336 <& .footer, path => $dir &>
337 </&>
338 %
339 <%args>
340 $path
341 $scale => undef
342 </%args>
343 </%def>
344 %
345 %###-------------------------------------------------------------------------
346 <%def .breadcrumbs>\
347 % $path =~ s!/$!!;
348 % my @p = split m!/!, $path;
349 % my $pp = "";
350 % my $prev = undef;
351 <h1><% $what %> \
352 % if (!@p) {
353 [top]
354 % } else {
355 <a href="<% $SCRIPTURL |hu %>/">[top]</a>&thinsp;/&thinsp;\
356 % STEP: for my $p (@p) {
357 % if (defined $prev) {
358 % $pp .= "$prev/";
359 <a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
360 <% $prev %></a>&thinsp;/&thinsp;\
361 % }
362 % $prev = $p;
363 % }
364 <% $prev %>\
365 % }
366 % if ($m->has_content) {
367
368 <% $m->content %>\
369 % }
370 </h1>
371 <%args>
372 $what
373 $path
374 </%args>
375 </%def>
376 %
377 %###-------------------------------------------------------------------------
378 <%def .thumbnail>\
379 % $tn //= "$STATICURL/folder.svg";
380 % if ($focus) {
381 <figure class="thumb focusthumb <% $size %>">
382 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
383 <figcaption><span class=name><% $caption %></span></figcaption>
384 % } else {
385 <figure class="thumb <% $size %>">
386 <a class=thumb href="<% $target |hu %>">
387 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
388 <figcaption>
389 <span class=name><% $caption %></span>
390 % if (defined $comment) {
391 <span class=comment><% $comment %></span>
392 % }
393 </figcaption>
394 </a>
395 % }
396 </figure>
397 %
398 <%args>
399 $target
400 $tn
401 $size
402 $caption
403 $comment => undef
404 $focus => 0
405 </%args>
406 </%def>
407 %
408 %###-------------------------------------------------------------------------
409 <%def .footer>\
410 <%perl>
411 </%perl>
412 <div class=footer>
413 <div class=footitem>
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 &copy; 2021 Mark Wooding.
416 Free software: you can modify it and/or redistribute it under the
417 terms of 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>.
421 </div>
422 % my $user =
423 % find_covering_file $IMGROOT, $path, ".tgal-footer.html";
424 % if (defined $user) {
425 <div class=footitem>
426 <% $user %>
427 </div>
428 % }
429 </div>
430 <%args>
431 $path
432 </%args>
433 </%def>
434 %
435 %###-------------------------------------------------------------------------
436 <%once>
437 use autodie;
438 use File::stat;
439
440 use TrivGal;
441 </%once>
442 %
443 <%init>
444 TrivGal->init;
445
446 my $path = $m->dhandler_arg;
447 my $st = stat "$IMGROOT/$path";
448 my $comp;
449 if (!$st) {
450 $comp = ".not-found";
451 if ($path =~ /^ (.*) (\.(?: zip)) $/x) {
452 $st = stat "$IMGROOT/$1";
453 if ($st) { $path = $1; $comp = $2; }
454 }
455 }
456 elsif (-d $st) { $comp = ".contact"; }
457 elsif (-f $st) { $comp = ".image"; }
458 else { $comp = ".not-found"; }
459 $r->header_out("X-AGPL-Source" => $SRCURL);
460 $m->comp($comp, path => $path, %ARGS);
461 </%init>
462 %
463 %###----- That's all, folks -------------------------------------------------