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
() {
159 eval { opendir $d, $TMP; };
161 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
) { return; }
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";
171 next FILE
if $now - $st->mtime() < 300;
177 ###--------------------------------------------------------------------------
190 package TrivGal
::Image
{
191 use File
::Path
qw{make_path
};
195 my ($cls, $path) = @_;
196 my $imgpath = "$IMGROOT/$path";
197 my $st = stat $imgpath or die "no image `$path'";
199 path
=> $path, imgpath
=> $imgpath,
202 rot
=> undef, flip
=> undef,
203 wd
=> undef, ht
=> undef,
204 _wd
=> undef, _ht
=> undef,
211 return if defined $me->{_wd
};
213 my ($wd, $ht, $err) = Image
::Size
::imgsize
$me->{imgpath
};
214 defined $wd or die "failed to read size of `$me->{path}': $err";
215 my $sz = $wd; if ($sz < $ht) { $sz = $ht; }
216 @
$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
221 return if defined $me->{wd
};
224 my $exif = new Image
::ExifTool
; $exif->ExtractInfo($me->{imgpath
});
225 my $orient = $exif->GetValue("Orientation", "ValueConv");
226 my ($wd, $ht) = @
$me{"_wd", "_ht"};
228 if (defined $orient) { ($rot, $flip) = $ORIENT{$orient}->@
*; }
229 else { ($rot, $flip) = (0, 0); }
230 if ($rot%2) { ($wd, $ht) = ($ht, $wd); }
231 @
$me{"rot", "flip", "wd", "ht"} = ($rot, $flip, $wd, $ht);
234 sub sz
($) { my ($me) = @_; $me->_getsz; return $me->{sz
}; }
235 sub wd
($) { my ($me) = @_; $me->_getexif; return $me->{wd
}; }
236 sub ht
($) { my ($me) = @_; $me->_getexif; return $me->{ht
}; }
240 "$rc" and die "failed to hack `$me->{img}': $rc";
244 my ($me, $scale, $forcep) = @_;
245 my $m = HTML
::Mason
::Request
->instance;
247 my $path = $me->{path
};
248 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
252 { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); }
254 my $tail = "scale.$sz/$path";
255 my $thumb = "$CACHE/$tail";
256 my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
258 my $st = stat $thumb;
259 if ($st && $st->mtime > $me->{mtime
})
260 { $url = $thumburl; }
263 $m->interp->apply_escapes("$SCRIPTURL/$path", "u") .
266 my $img = $me->{img
};
267 unless (defined $img) {
268 $img = $me->{img
} = Graphics
::Magick
->new;
269 _check_gm
$img->Read($me->{imgpath
});
272 my ($dir, undef, $ext) = TrivGal
::split_path
$thumb;
274 my $new = "$TMP/t$$-thumb$ext";
275 _check_gm
$img->Resize(geometry
=> $sz);
276 make_path
$TMP, { mode
=> 0771 };
277 _check_gm
$img->Write($new);
278 make_path
$dir, { mode
=> 0771 };
288 ###--------------------------------------------------------------------------
289 ### Directory listings.
291 package TrivGal
::Item
{
293 my ($cls, $name) = @_;
294 return bless { name
=> $name }, $cls;
297 my ($me, @args) = @_;
298 return TrivGal
::read_or_set
$me, $me->{name
}, @args;
301 my ($me, @args) = @_;
302 return TrivGal
::read_or_set
$me, $me->{comment
}, @args;
313 if (-f
"$path/.tgal.index") {
314 open my $f, "<", "$path/.tgal.index";
319 next LINE
if /^\s*(\#|$)/;
321 die "no item" unless $item;
322 $comment = defined $comment ?
$comment . "\n" . $_ : $_;
324 if ($item && $comment) { $item->comment($comment); }
325 my ($flags, $name, $c) =
326 /^ (?
: ([-!]+) \s
+)?
# flags
328 (\S
| \S
.*\S
)?
# start of the comment
331 my $indexp = $flags =~ /!/;
332 my $hidep = $flags =~ /-/;
333 $name = urldecode
$name;
335 $item = TrivGal
::Item
->new($name);
336 if ($name =~ m
#/$#) {
338 die "can't index a folder" if $indexp;
341 my (undef, undef, $ext) = split_path
$name;
342 die "unknown image type" unless $TYPE{lc $ext};
344 die "two index images" if defined $ix;
349 push @
$list, $item unless $hidep;
352 if ($item && $comment) { $item->comment($comment); }
356 unless ($st->mode&0004) { return ([], [], undef); }
362 ENT
: for my $e (sort @e) {
363 my (undef, undef, $ext) = split_path
$e;
364 my $dotp = $e =~ /^\./;
365 my $st = stat "$path/$e";
368 elsif (-d
$st) { $list = \
@d; $e .= "/"; }
369 elsif ($TYPE{lc $ext} && -f
$st) { $list = \
@f; }
370 $list and push @
$list, TrivGal
::Item
->new($e);
375 return (\
@d, \
@f, $ix);
384 eval { open $f, "<", "$file"; };
386 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
)
390 while (sysread $f, $buf, 16384) { $contents .= $buf; }
395 export
qw{find_covering_file
};
396 sub find_covering_file
($$$) {
397 my ($top, $path, $name) = @_;
399 my $stuff = contents
"$top/$path/$name"; return $stuff if defined $stuff;
400 if ($path eq "") { return undef; }
401 if ($path =~ m
#^(.*)/[^/]+/?#) { $path = $1; }
406 ###----- That's all, folks --------------------------------------------------