mason/dhandler (.image): Generate thumbnails more memory-efficiently.
[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">
38 <script type="text/javascript" src="<% "$STATICURL/tgal.js" |u %>" defer></script>
39 <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |u %>">
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
MW
87 my %tn;
88 for my $f (@$ff)
ccaab4e8 89 { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
6e749fab 90 for my $d (@$dd) {
2fe5383f
MW
91 my $p = join_paths $path, $d->name;
92 DIR: for (;;) {
93 my ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
94 if (defined $iii) {
95 my $index = join_paths $p, $iii->name;
ccaab4e8 96 $tn{$d} = TrivGal::Image->new($index)->scale($size);
2fe5383f
MW
97 last DIR;
98 }
99 if (!@$ddd) { $tn{$d} = undef; last DIR; }
100 $p = join_paths $p, $ddd->[0]->name;
101 }
6e749fab 102 }
6ac5dde2
MW
103</%perl>
104%
3d7d20f8
MW
105<&| .html, title =>
106 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
6ac5dde2
MW
107 head => $links &>
108<& .breadcrumbs, what => "Folder", path => $path &>
109%
110% my $note = contents "$IMGROOT/$path/.tgal-note.html";
111% if (defined $note) {
112<div class=note>
113<% $note %>
114</div>
115% }
116%
117% if (@$dd) {
118<h2>Subfolders</h2>
6e749fab 119<div class="gallery <% $size %>">
f4b90914 120% for my $d (@$dd) {
24f4eac3 121 <& .thumbnail, target => $d->name, comment => $d->comment,
ccaab4e8 122 tn => $tn{$d}, size => $size,
dfdd1964 123 caption => $m->interp->apply_escapes($d->name, "h") &>\
6e749fab 124% }
f4b90914 125</div>
6ac5dde2
MW
126% }
127%
128% if (@$ff) {
129<h2>Images</h2>
6e749fab 130<div class="gallery <% $size %>">
f4b90914 131% for my $f (@$ff) {
dfdd1964 132 <& .thumbnail, target => $f->name, comment => $f->comment,
ccaab4e8 133 tn => $tn{$f}, size => $size,
dfdd1964 134 caption => $m->interp->apply_escapes($f->name, "h") &>\
6e749fab 135% }
f4b90914 136</div>
6ac5dde2
MW
137% }
138%
139<div class=fill></div>
140<& .footer, path => $path &>
141</&>
142%
143<%args>
144 $path
145</%args>
146</%def>
147%
148%###-------------------------------------------------------------------------
149<%def .image>\
150<%perl>
151 my ($dir, $base, $ext) = split_path $path;
9601c4bb 152
bfc5bfe6
MW
153 if (defined $scale) {
154 my $img = TrivGal::Image->new($path);
155 $m->redirect($img->scale($scale));
156 }
9601c4bb 157
bfc5bfe6 158 my $real = join_paths $IMGROOT, $path;
6ac5dde2
MW
159 my $url = join_paths $IMGURL, $path;
160 my $realdir = join_paths $IMGROOT, $dir;
161 my $urldir = join_paths $SCRIPTURL, $dir;
162 my ($dd, $ff, $ii) = listdir $realdir;
bfc5bfe6
MW
163 my @thumbsz = qw{smallthumb medthumb bigthumb};
164 my %tn;
165 my $vw;
6ac5dde2
MW
166
167 my $fi = undef;
bfc5bfe6
MW
168 FILE: for (my $i = 0; $i < @$ff; $i++) {
169 my $f = $ff->[$i];
170 my $img = TrivGal::Image->new(join_paths $dir, $f->name);
171 for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
172 if ($ff->[$i]->name eq "$base$ext")
173 { $fi = $i; $vw = $img->scale("view"); }
174 }
6ac5dde2
MW
175 defined $fi or die "image not found in its folder?";
176 my $this = $ff->[$fi];
177
178 my %link;
179 $link{up} = "";
180 if ($fi != 0) {
181 $link{first} = $ff->[0]->name;
182 $link{prev} = $ff->[$fi - 1]->name;
183 }
184 if ($fi != @$ff - 1) {
185 $link{last} = $ff->[-1]->name;
186 $link{next} = $ff->[$fi + 1]->name;
187 }
188
189 my $links = "";
190 my $pre =
191 urlencode join_paths $SCRIPTURL, $dir;
192 for my $rel (qw{up first prev next last}) {
193 exists $link{$rel} and
194 $links .= sprintf " <link rel=%s href=\"%s\">\n",
195 $rel, urlencode "$pre/$link{$rel}";
196 }
197</%perl>
198%
199<&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
200 head => $links &>
201<& .breadcrumbs, what => "Image", path => $path &>
202% if ($this->comment) {
203 <div class=comment>
204 <p><% $this->comment %>
205 </div>
206% }
207%
208<div class=viewnav>
209% if ($link{prev}) {
210 <div class=prev><a class=prev href="<% "$pre/$link{prev}" |u %>">&lsaquo;</a></div>
211% }
212 <a class=view href="<% $url |h %>">
213 <img src="<% $vw |h %>">
214 </a>
215% if ($link{next}) {
216 <div class=next><a class=next href="<% "$pre/$link{next}" |u %>">&rsaquo;</a></div>
217% }
218</div>
219%
1408b7a2 220% for my $size (qw{smallthumb medthumb bigthumb}) {
6e749fab
MW
221<div class="thumbstrip <% $size %>">
222% for my $f (@$ff) {
ccaab4e8 223 <& .thumbnail, target => $f->name,
bfc5bfe6 224 tn => $tn{$f->name}{$size}, size => $size,
6ac5dde2 225 caption => $m->interp->apply_escapes($f->name, "h"),
62ee1c09 226 focus => $f eq $this &>\
6e749fab 227% }
6ac5dde2 228</div>
6e749fab 229% }
6ac5dde2
MW
230<& .footer, path => $dir &>
231</&>
232%
233<%args>
234 $path
9601c4bb 235 $scale => undef
6ac5dde2
MW
236</%args>
237</%def>
238%
239%###-------------------------------------------------------------------------
240<%def .breadcrumbs>\
241% $path =~ s!/$!!;
242% my @p = split m!/!, $path;
243% my $pp = "";
244% my $prev = undef;
245<h1><% $what %> \
246% if (!@p) {
247[top]
248% } else {
249<a href="<% $SCRIPTURL |u %>/">[top]</a>&thinsp;/&thinsp;\
250% STEP: for my $p (@p) {
251% if (defined $prev) {
252% $pp .= "$prev/";
253<a href="<% join_paths($SCRIPTURL, $pp) |u %>/">\
254<% $prev %></a>&thinsp;/&thinsp;\
255% }
256% $prev = $p;
257% }
258<% $prev %>\
259% }
260</h1>
261<%args>
262 $what
263 $path
264</%args>
265</%def>
266%
267%###-------------------------------------------------------------------------
268<%def .thumbnail>\
ccaab4e8 269% $tn //= "$STATICURL/folder.svg";
6ac5dde2 270% if ($focus) {
1408b7a2 271 <figure class="thumb focusthumb <% $size %>">
1b3d6791 272 <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
83bfcbc7 273 <figcaption><span class=name><% $caption %></span></figcaption>
6ac5dde2 274% } else {
dfdd1964 275 <figure class="thumb <% $size %>">
56340849 276 <a class=thumb href="<% $target |u %>">
1b3d6791 277 <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
83bfcbc7 278 <figcaption>
6ac5dde2
MW
279 <span class=name><% $caption %></span>
280% if (defined $comment) {
281 <span class=comment><% $comment %></span>
282% }
83bfcbc7 283 </figcaption>
6ac5dde2
MW
284 </a>
285% }
83bfcbc7 286 </figure>
6ac5dde2
MW
287%
288<%args>
289 $target
ccaab4e8 290 $tn
dfdd1964 291 $size
6ac5dde2
MW
292 $caption
293 $comment => undef
294 $focus => 0
295</%args>
296</%def>
297%
298%###-------------------------------------------------------------------------
299<%def .footer>\
300<%perl>
301</%perl>
302<div class=footer>
303 <div class=footitem>
304 <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |u %>"></a>
305 Trivial Gallery, copyright &copy; 2021 Mark Wooding.
306 Free software: you can modify it and/or redistribute it under the
307 terms of the
a54108de 308 <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
6ac5dde2 309 General Public License version 3</a>.
a38a867d 310 Browse or download the <a href="<% $SRCURL %>">source code</a>.
6ac5dde2
MW
311 </div>
312% my $user =
313% find_covering_file $IMGROOT, $path, ".tgal-footer.html";
314% if (defined $user) {
315 <div class=footitem>
316<% $user %>
317 </div>
318% }
319</div>
320<%args>
321 $path
322</%args>
323</%def>
324%
325%###-------------------------------------------------------------------------
326<%once>
327 use autodie;
6ac5dde2
MW
328 use File::stat;
329
330 use TrivGal;
331</%once>
332%
333<%init>
334 TrivGal->init;
335
336 my $path = $m->dhandler_arg;
337 my $st = stat "$IMGROOT/$path";
338 my $comp;
339 if (!$st) { $comp = ".not-found"; }
340 elsif (-d $st) { $comp = ".contact"; }
341 elsif (-f $st) { $comp = ".image"; }
342 else { $comp = ".not-found"; }
ecf3c7a8 343 $r->header_out("X-AGPL-Source" => $SRCURL);
6abe8e59 344 $m->comp($comp, path => $path, %ARGS);
6ac5dde2
MW
345</%init>
346%
347%###----- That's all, folks -------------------------------------------------