chiark / gitweb /
mason/dhandler (.contact), static/tgal.css: Adaptive thumbnail sizes.
[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><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
291 % }
292   <a class=view href="<% $url |h %>">
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>
306   </a>
307 % if ($link{next}) {
308   <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>
309 % }
310 </div>
311 %
312 % for my $size (qw{smallthumb medthumb bigthumb}) {
313 <div class="thumbstrip <% $size %>">
314 %   for my $f (@$ff) {
315   <& .thumbnail, target => $f->name,
316                  tn => $tn{$f->name}{$size}, size => $size,
317                  caption => $m->interp->apply_escapes($f->name, "h"),
318                  focus => $f eq $this &>\
319 %   }
320 </div>
321 % }
322 <& .footer, path => $dir &>
323 </&>
324 %
325 <%args>
326         $path
327         $scale => undef
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 {
341 <a href="<% $SCRIPTURL |hu %>/">[top]</a>&thinsp;/&thinsp;\
342 %   STEP: for my $p (@p) {
343 %     if (defined $prev) {
344 %       $pp .= "$prev/";
345 <a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
346 <% $prev %></a>&thinsp;/&thinsp;\
347 %     }
348 %     $prev = $p;
349 %   }
350 <% $prev %>\
351 % }
352 % if ($m->has_content) {
353
354 <% $m->content %>\
355 % }
356 </h1>
357 <%args>
358         $what
359         $path
360 </%args>
361 </%def>
362 %
363 %###-------------------------------------------------------------------------
364 <%def .thumbnail>\
365 % $tn //= "$STATICURL/folder.svg";
366 % if ($focus) {
367   <figure class="thumb focusthumb <% $size %>">
368     <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
369     <figcaption><span class=name><% $caption %></span></figcaption>
370 % } else {
371   <figure class="thumb <% $size %>">
372     <a class=thumb href="<% $target |hu %>">
373       <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
374       <figcaption>
375         <span class=name><% $caption %></span>
376 % if (defined $comment) {
377         <span class=comment><% $comment %></span>
378 % }
379       </figcaption>
380     </a>
381 % }
382   </figure>
383 %
384 <%args>
385         $target
386         $tn
387         $size
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>
400     <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |hu %>"></a>
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
404     <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
405     General Public License version 3</a>.
406     Browse or download the <a href="<% $SRCURL %>">source code</a>.
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;
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;
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         }
442         elsif (-d $st) { $comp = ".contact"; }
443         elsif (-f $st) { $comp = ".image"; }
444         else { $comp = ".not-found"; }
445         $r->header_out("X-AGPL-Source" => $SRCURL);
446         $m->comp($comp, path => $path, %ARGS);
447 </%init>
448 %
449 %###----- That's all, folks -------------------------------------------------