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