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