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