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};
33 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 ###--------------------------------------------------------------------------
101 package TrivGal::ImageType {
103 my ($cls, $ext) = @_;
104 return $TYPE{$ext} = bless { ext => $ext }, $cls;
107 my ($me, @args) = @_;
111 my ($me, @args) = @_;
112 return TrivGal::read_or_set $me, $me->{mimetype}, @args;
115 my ($me, @args) = @_;
116 return TrivGal::read_or_set $me, $me->{imlibfmt}, @args;
120 TrivGal::ImageType->new(".jpg")->mimetype("image/jpeg")->imlibfmt("jpeg");
121 TrivGal::ImageType->new(".png")->mimetype("image/png")->imlibfmt("png");
123 ###--------------------------------------------------------------------------
126 export qw{$SCOPE $SUFFIX};
127 our $SCOPE //= $::SCOPE;
128 our $SUFFIX //= $::SUFFIX;
130 export qw{$IMGROOT $CACHE $TMP};
131 our $IMGROOT //= "$ENV{HOME}/publish/$SCOPE-html$SUFFIX/tgal-images";
133 ($ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache") .
134 "/tgal/$SCOPE$SUFFIX";
135 our $TMP //= "$CACHE/tmp";
137 export qw{$ROOTURL $IMGURL $CACHEURL $STATICURL $SCRIPTURL};
138 my $user = getpwuid($>)->name;
139 our $ROOTURL //= "/~$user";
140 our $IMGURL //= "$ROOTURL/tgal-images";
141 our $CACHEURL //= "$ROOTURL/tgal-cache";
142 our $STATICURL //= "$ROOTURL/tgal-static";
146 our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/";
149 our %SIZE = (smallthumb => 96,
159 my $m = HTML::Mason::Request->instance;
160 my $r = $m->cgi_request;
162 $m->interp->set_escape(u => sub { my ($r) = @_; $$r = urlencode $$r; });
164 return unless !$initp;
166 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
170 ###--------------------------------------------------------------------------
173 export qw{clean_temp_files};
174 sub clean_temp_files () {
177 eval { opendir $d, $TMP; };
179 if ($@->isa("autodie::exception") && $@->errno == ENOENT) { return; }
183 FILE: while (my $name = readdir $d) {
184 next FILE unless $name =~ /^t(\d+)\-/;
186 next FILE if kill 0, $pid;
187 my $f = "$TMP/$name";
189 next FILE if $now - $st->mtime() < 300;
195 ###--------------------------------------------------------------------------
208 package TrivGal::Image {
209 use File::Path qw{make_path};
213 my ($cls, $path) = @_;
214 my $imgpath = "$IMGROOT/$path";
215 my $st = stat $imgpath or die "no image `$path'";
217 path => $path, imgpath => $imgpath,
220 _wd => undef, _ht => undef,
227 return if defined $me->{_wd};
229 my ($wd, $ht, $err) = Image::Size::imgsize $me->{imgpath};
230 defined $wd or die "failed to read size of `$me->{path}': $err";
231 my $sz = $wd; if ($sz < $ht) { $sz = $ht; }
232 @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
235 sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; }
238 my ($me, $scale, $forcep) = @_;
239 my $m = HTML::Mason::Request->instance;
241 my $path = $me->{path};
242 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
244 { return $m->interp->apply_escapes("$IMGURL/$path", "u"); }
246 my $tail = "scale.$sz/$path";
247 my $thumb = "$CACHE/$tail";
248 my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
249 my $st = stat $thumb;
250 if (defined $st && $st->mtime > $me->{mtime}) { return $thumburl; }
252 $m->interp->apply_escapes("$SCRIPTURL/$path", "u") . "?scale=$scale"
255 my ($dir, $base, $ext) = TrivGal::split_path $thumb;
256 my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
258 my $img = $me->{img};
259 unless (defined $img) {
260 my $exif = new Image::ExifTool;
261 $exif->ExtractInfo($me->{imgpath});
262 my $orient = $exif->GetValue("Orientation", "ValueConv");
263 $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
264 if (defined $orient) {
265 my ($rot, $flip) = @{$ORIENT{$orient}};
266 if ($rot) { $img->image_orientate($rot); }
267 if ($flip) { $img->flip_horizontal(); }
271 my $sc = $sz/$me->sz;
272 my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
274 $scaled->image_set_format($ty->imlibfmt);
275 $scaled->set_quality(90);
276 my $new = "$TMP/t$$-$ext";
277 make_path $TMP, { mode => 0771 };
279 make_path $dir, { mode => 0771 };
285 ###--------------------------------------------------------------------------
286 ### Directory listings.
288 package TrivGal::Item {
290 my ($cls, $name) = @_;
291 return bless { name => $name }, $cls;
294 my ($me, @args) = @_;
295 return TrivGal::read_or_set $me, $me->{name}, @args;
298 my ($me, @args) = @_;
299 return TrivGal::read_or_set $me, $me->{comment}, @args;
310 if (-f "$path/.tgal.index") {
311 open my $f, "<", "$path/.tgal.index";
316 next LINE if /^\s*(\#|$)/;
318 die "no item" unless $item;
319 $comment = defined $comment ? $comment . "\n" . $_ : $_;
321 if ($item && $comment) { $item->comment($comment); }
322 my ($flags, $name, $c) =
323 /^ (?: ([-!]+) \s+)? # flags
325 (\S | \S.*\S )? # start of the comment
328 my $indexp = $flags =~ /!/;
329 my $hidep = $flags =~ /-/;
330 $name = urldecode $name;
332 $item = TrivGal::Item->new($name);
333 if ($name =~ m#/$#) {
335 die "can't index a folder" if $indexp;
338 my ($dir, $base, $ext) = split_path $name;
339 die "unknown image type" unless $TYPE{lc $ext};
341 die "two index images" if defined $ix;
346 push @$list, $item unless $hidep;
349 if ($item && $comment) { $item->comment($comment); }
353 unless ($st->mode&0004) { return ([], [], undef); }
359 ENT: for my $e (sort @e) {
360 my ($dir, $base, $ext) = split_path $e;
361 my $dotp = $e =~ /^\./;
362 my $st = stat "$path/$e";
365 elsif (-d $st) { $list = \@d; $e .= "/"; }
366 elsif ($TYPE{lc $ext} && -f $st) { $list = \@f; }
367 $list and push @$list, TrivGal::Item->new($e);
372 return (\@d, \@f, $ix);
381 eval { open $f, "<", "$file"; };
383 if ($@->isa("autodie::exception") && $@->errno == ENOENT)
387 while (sysread $f, $buf, 16384) { $contents .= $buf; }
392 export qw{find_covering_file};
393 sub find_covering_file ($$$) {
394 my ($top, $path, $name) = @_;
396 my $stuff = contents "$top/$path/$name"; return $stuff if defined $stuff;
397 if ($path eq "") { return undef; }
398 if ($path =~ m#^(.*)/[^/]+/?#) { $path = $1; }
403 ###----- That's all, folks --------------------------------------------------