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{};
39 sub export
(@
) { push @EXPORT, @_; }
41 ###--------------------------------------------------------------------------
42 ### Internal utilities.
44 sub read_or_set
($\
$@
) {
45 my ($me, $ref, @args) = @_;
46 if (@args == 0) { return $$ref; }
47 elsif (@args == 1) { $$ref = $args[0]; return $me; }
48 elsif (@args > 1) { die "too many arguments"; }
51 ###--------------------------------------------------------------------------
54 export
qw{join_paths
};
60 $e =~ s
,([^/])/+$,$1,;
61 if ($e eq "") { next ELT
; }
62 elsif ($p eq "" || $e =~ m
,^/,) { $p = $e; }
63 else { $p = "$p/$e"; }
68 export
qw{split_path
};
72 my ($dir, $base, $ext) = $path =~ m
,^(?
:(.*)/)?(?:([^/]*)\
.)?
([^./]*)$,;
73 if (defined $base) { $ext = ".$ext"; }
74 else { $base = $ext; $ext = ""; }
75 return ($dir, $base, $ext);
81 $u =~ s
:([^0-9a
-zA
-Z_
./~-]):sprintf "%%%02x", ord $1:eg
;
88 $u =~ s
:\
%([0-9a
-fA
-F
]{2}):chr hex $1:eg
;
92 ###--------------------------------------------------------------------------
97 package TrivGal
::ImageType
{
100 return $TYPE{$ext} = bless { ext
=> $ext }, $cls;
103 my ($me, @args) = @_;
107 my ($me, @args) = @_;
108 return TrivGal
::read_or_set
$me, $me->{mimetype
}, @args;
111 my ($me, @args) = @_;
112 return TrivGal
::read_or_set
$me, $me->{imlibfmt
}, @args;
116 TrivGal
::ImageType
->new(".jpg")->mimetype("image/jpeg")->imlibfmt("jpeg");
117 TrivGal
::ImageType
->new(".png")->mimetype("image/png")->imlibfmt("png");
119 ###--------------------------------------------------------------------------
122 export
qw{$SCOPE $SUFFIX};
123 our $SCOPE //= $::SCOPE
;
124 our $SUFFIX //= $::SUFFIX
;
126 export
qw{$IMGROOT $CACHE $TMP};
127 our $IMGROOT //= "$ENV{HOME}/publish/$SCOPE-html$SUFFIX/tgal-images";
129 ($ENV{XDG_CACHE_HOME
} // "$ENV{HOME}/.cache") .
130 "/tgal/$SCOPE$SUFFIX";
131 our $TMP //= "$CACHE/tmp";
133 export
qw{$ROOTURL $IMGURL $CACHEURL $STATICURL $SCRIPTURL};
134 my $user = getpwuid($>)->name;
135 our $ROOTURL //= "/~$user";
136 our $IMGURL //= "$ROOTURL/tgal-images";
137 our $CACHEURL //= "$ROOTURL/tgal-cache";
138 our $STATICURL //= "$ROOTURL/tgal-static";
142 our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/";
145 our %SIZE = (smallthumb
=> 96,
153 my $m = HTML
::Mason
::Request
->instance;
154 my $r = $m->cgi_request;
156 $m->interp->set_escape(u
=> sub { my ($r) = @_; $$r = urlencode
$$r; });
158 return unless !$initp;
160 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
164 ###--------------------------------------------------------------------------
167 export
qw{clean_temp_files
};
168 sub clean_temp_files
() {
171 eval { opendir $d, $TMP; };
173 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
) { return; }
177 FILE
: while (my $name = readdir $d) {
178 next FILE
unless $name =~ /^t(\d+)\-/;
180 next FILE
if kill 0, $pid;
181 my $f = "$TMP/$name";
183 next FILE
if $now - $st->mtime() < 300;
189 ###--------------------------------------------------------------------------
202 package TrivGal
::Image
{
203 use File
::Path
qw{make_path
};
207 my ($cls, $path) = @_;
208 my $imgpath = "$IMGROOT/$path";
209 my $st = stat $imgpath or die "no image `$path'";
218 my ($me, $scale) = @_;
220 my $path = $me->{path
};
221 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
222 my $thumb = "$CACHE/scale.$sz/$path";
223 my $thumburl = "$CACHEURL/scale.$sz/$path";
224 my $st = stat $thumb;
225 if (defined $st && $st->mtime > $me->{mtime
}) { return $thumburl; }
227 my ($dir, $base, $ext) = TrivGal
::split_path
$thumb;
228 my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
230 my $img = $me->{img
};
231 unless (defined $img) {
232 my $imgpath = "$IMGROOT/$path";
233 my $exif = new Image
::ExifTool
;
234 $exif->ExtractInfo($imgpath);
235 my $orient = $exif->GetValue("Orientation", "ValueConv");
236 $img = $me->{img
} = Image
::Imlib2
->load($imgpath);
237 if (defined $orient) {
238 my ($rot, $flip) = @
{$ORIENT{$orient}};
239 if ($rot) { $img->image_orientate($rot); }
240 if ($flip) { $img->flip_horizontal($rot); }
244 my ($wd, $ht) = ($img->width, $img->height);
245 my $max = $wd > $ht ?
$wd : $ht;
246 if ($max <= $sz) { return "$IMGURL/$path"; }
248 my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
250 $scaled->image_set_format($ty->imlibfmt);
251 $scaled->set_quality(90);
252 my $new = "$TMP/t${$}$ext";
261 ###--------------------------------------------------------------------------
262 ### Directory listings.
264 package TrivGal
::Item
{
266 my ($cls, $name) = @_;
267 return bless { name
=> $name }, $cls;
270 my ($me, @args) = @_;
271 return TrivGal
::read_or_set
$me, $me->{name
}, @args;
274 my ($me, @args) = @_;
275 return TrivGal
::read_or_set
$me, $me->{comment
}, @args;
285 if (-f
"$path/.tgal.index") {
286 open my $f, "<", "$path/.tgal.index";
291 next LINE
if /^\s*(\#|$)/;
293 die "no item" unless $item;
294 $comment = defined $comment ?
$comment . "\n" . $_ : $_;
296 if ($item && $comment) { $item->comment($comment); }
297 my ($indexp, $name, $c) = /(!\s+)?(\S+)\s*(\S|\S.*\S)?\s*$/;
298 $name = urldecode
$name;
300 if ($name =~ m!/$!) {
302 die "can't index a folder" if $indexp;
305 my ($dir, $base, $ext) = TrivGal
::split_path
$name;
306 die "unknown image type" unless $TYPE{lc $ext};
308 die "two index images" if defined $ix;
312 $item = TrivGal
::Item
->new($name);
317 if ($item && $comment) { $item->comment($comment); }
320 my $st = stat "$path/$e";
321 unless ($st->mode&0004) { return ([], [], undef); }
327 ENT
: for my $e (sort @e) {
328 my ($dir, $base, $ext) = split_path
$e;
329 my $dotp = $e =~ /^\./;
330 my $st = stat "$path/$e";
333 elsif (-d
$st) { $list = \
@d; }
334 elsif ($TYPE{lc $ext} && -f
$st) { $list = \
@f; }
335 $list and push @
$list, TrivGal
::Item
->new($e);
340 return (\
@d, \
@f, $ix);
349 eval { open $f, "<", "$file"; };
351 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
)
355 while (sysread $f, $buf, 16384) { $contents .= $buf; }
360 export
qw{find_covering_file
};
361 sub find_covering_file
($$$) {
362 my ($top, $path, $name) = @_;
364 my $stuff = contents
"$top/$path/$name"; return $stuff if defined $stuff;
365 if ($path eq "") { return undef; }
366 if ($path =~ m!^(.*)/[^/]+/?!) { $path = $1; }
371 ###----- That's all, folks --------------------------------------------------