3 ### Main output for Trivial Gallery.
5 ### (c) 2021 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Trivial Gallery.
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.
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.
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/>.
31 use Exporter qw{import};
34 use Image::ExifTool qw{};
40 sub export (@) { push @EXPORT, @_; }
42 ###--------------------------------------------------------------------------
43 ### Internal utilities.
45 sub read_or_set ($\$@) {
46 my ($me, $ref, @args) = @_;
47 if (@args == 0) { return $$ref; }
48 elsif (@args == 1) { $$ref = $args[0]; return $me; }
49 elsif (@args > 1) { die "too many arguments"; }
52 ###--------------------------------------------------------------------------
55 export qw{join_paths};
61 $e =~ s#([^/])/+$#$1#;
62 if ($e eq "") { next ELT; }
63 elsif ($p eq "" || $e =~ m#^/#) { $p = $e; }
64 else { $p = "$p/$e"; }
69 export qw{split_path};
73 my ($dir, $base, $ext) =
74 $path =~ m#^ (?: (.*) /)?
77 if (defined $base) { $ext = ".$ext"; }
78 else { $base = $ext; $ext = ""; }
79 return ($dir, $base, $ext);
85 $u =~ s#([^0-9a-zA-Z_./~-])#sprintf "%%%02x", ord $1#eg;
92 $u =~ s#\%([0-9a-fA-F]{2})#chr hex $1#eg;
96 ###--------------------------------------------------------------------------
99 export qw{$SCOPE $SUFFIX};
100 our $SCOPE //= $::SCOPE;
101 our $SUFFIX //= $::SUFFIX;
103 export qw{$IMGROOT $CACHE $TMP};
104 our $IMGROOT //= "$ENV{HOME}/publish/$SCOPE-html$SUFFIX/tgal-images";
106 ($ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache") .
107 "/tgal/$SCOPE$SUFFIX";
108 our $TMP //= "$CACHE/tmp";
110 export qw{$ROOTURL $IMGURL $CACHEURL $STATICURL $SCRIPTURL};
111 my $user = getpwuid($>)->name;
112 our $ROOTURL //= "/~$user";
113 our $IMGURL //= "$ROOTURL/tgal-images";
114 our $CACHEURL //= "$ROOTURL/tgal-cache";
115 our $STATICURL //= "$ROOTURL/tgal-static";
119 our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/";
122 our %SIZE = (smallthumb => 96,
136 our %TYPE = map { $_ => 1 } qw{.jpg .png};
141 my $m = HTML::Mason::Request->instance;
142 my $r = $m->cgi_request;
144 $m->interp->set_escape(u => sub { my ($r) = @_; $$r = urlencode $$r; });
146 return unless !$initp;
148 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
152 ###--------------------------------------------------------------------------
155 export qw{clean_temp_files};
156 sub clean_temp_files () {
157 no autodie qw{kill opendir unlink};
160 unless (opendir $d, $TMP) {
161 if ($! == ENOENT) { return }
162 else { die "failed to read temporary directory `$TMP': $!"; }
165 FILE: while (my $name = readdir $d) {
166 next FILE unless $name =~ /^t(\d+)\-/;
168 next FILE if kill 0, $pid;
169 my $f = "$TMP/$name";
172 if ($! == ENOENT) { next FILE; }
173 else { die "failed to read file metadata for `$f': $!"; }
175 next FILE if $now - $st->mtime() < 300;
177 if ($! == ENOENT) { next FILE; }
178 else { die "failed to delete file `$f': $!"; }
184 ###--------------------------------------------------------------------------
197 package TrivGal::Image {
198 use File::Path qw{make_path};
202 my ($cls, $path) = @_;
203 my $imgpath = "$IMGROOT/$path";
204 my $st = stat $imgpath or die "no image `$path'";
206 path => $path, imgpath => $imgpath,
209 rot => undef, flip => undef,
210 wd => undef, ht => undef,
211 _wd => undef, _ht => undef,
218 return if defined $me->{_wd};
220 my ($wd, $ht, $err) = Image::Size::imgsize $me->{imgpath};
221 defined $wd or die "failed to read size of `$me->{path}': $err";
222 my $sz = $wd; if ($sz < $ht) { $sz = $ht; }
223 @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
228 return if defined $me->{wd};
231 my $exif = new Image::ExifTool; $exif->ExtractInfo($me->{imgpath});
232 my $orient = $exif->GetValue("Orientation", "ValueConv");
233 my ($wd, $ht) = @$me{"_wd", "_ht"};
235 if (defined $orient) { ($rot, $flip) = $ORIENT{$orient}->@*; }
236 else { ($rot, $flip) = (0, 0); }
237 if ($rot%2) { ($wd, $ht) = ($ht, $wd); }
238 @$me{"rot", "flip", "wd", "ht"} = ($rot, $flip, $wd, $ht);
241 sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; }
242 sub wd ($) { my ($me) = @_; $me->_getexif; return $me->{wd}; }
243 sub ht ($) { my ($me) = @_; $me->_getexif; return $me->{ht}; }
247 "$rc" and die "failed to hack `$me->{img}': $rc";
251 my ($me, $scale, $forcep) = @_;
252 my $m = HTML::Mason::Request->instance;
254 my $path = $me->{path};
255 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
259 { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); }
261 my $tail = "scale.$sz/$path";
262 my $thumb = "$CACHE/$tail";
263 my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
265 my $st = stat $thumb;
266 if ($st && $st->mtime > $me->{mtime})
267 { $url = $thumburl; }
270 $m->interp->apply_escapes("$SCRIPTURL/$path", "u") .
273 my $img = $me->{img};
274 unless (defined $img) {
275 $img = $me->{img} = Graphics::Magick->new;
276 _check_gm $img->Read($me->{imgpath});
279 my ($dir, undef, $ext) = TrivGal::split_path $thumb;
281 my $new = "$TMP/t$$-thumb$ext";
282 _check_gm $img->Resize(geometry => $sz);
283 make_path $TMP, { mode => 0771 };
284 _check_gm $img->Write($new);
285 make_path $dir, { mode => 0771 };
295 ###--------------------------------------------------------------------------
296 ### Directory listings.
298 package TrivGal::Item {
300 my ($cls, $name) = @_;
301 return bless { name => $name }, $cls;
304 my ($me, @args) = @_;
305 return TrivGal::read_or_set $me, $me->{name}, @args;
308 my ($me, @args) = @_;
309 return TrivGal::read_or_set $me, $me->{comment}, @args;
320 if (-f "$path/.tgal.index") {
321 open my $f, "<", "$path/.tgal.index";
326 next LINE if /^\s*(\#|$)/;
328 die "no item" unless $item;
329 $comment = defined $comment ? $comment . "\n" . $_ : $_;
331 if ($item && $comment) { $item->comment($comment); }
332 my ($flags, $name, $c) =
333 /^ (?: ([-!]+) \s+)? # flags
335 (\S | \S.*\S )? # start of the comment
338 my $indexp = $flags =~ /!/;
339 my $hidep = $flags =~ /-/;
340 $name = urldecode $name;
342 $item = TrivGal::Item->new($name);
343 if ($name =~ m#/$#) {
345 die "can't index a folder" if $indexp;
348 my (undef, undef, $ext) = split_path $name;
349 die "unknown image type" unless $TYPE{lc $ext};
351 die "two index images" if defined $ix;
356 push @$list, $item unless $hidep;
359 if ($item && $comment) { $item->comment($comment); }
363 unless ($st->mode&0004) { return ([], [], undef); }
369 ENT: for my $e (sort @e) {
370 my (undef, undef, $ext) = split_path $e;
371 my $dotp = $e =~ /^\./;
372 my $st = stat "$path/$e";
375 elsif (-d $st) { $list = \@d; $e .= "/"; }
376 elsif ($TYPE{lc $ext} && -f $st) { $list = \@f; }
377 $list and push @$list, TrivGal::Item->new($e);
382 return (\@d, \@f, $ix);
391 eval { open $f, "<", "$file"; };
393 if ($@->isa("autodie::exception") && $@->errno == ENOENT)
397 while (sysread $f, $buf, 16384) { $contents .= $buf; }
402 export qw{find_covering_file};
403 sub find_covering_file ($$$) {
404 my ($top, $path, $name) = @_;
406 my $stuff = contents "$top/$path/$name"; return $stuff if defined $stuff;
407 if ($path eq "") { return undef; }
408 if ($path =~ m#^(.*)/[^/]+/?#) { $path = $1; }
413 ###----- That's all, folks --------------------------------------------------