5b5cbfa297cd95e33d1281700065f9d62d15fc42
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
};
38 sub export
(@
) { push @EXPORT, @_; }
40 ###--------------------------------------------------------------------------
41 ### Internal utilities.
43 sub read_or_set
($\
$@
) {
44 my ($me, $ref, @args) = @_;
45 if (@args == 0) { return $$ref; }
46 elsif (@args == 1) { $$ref = $args[0]; return $me; }
47 elsif (@args > 1) { die "too many arguments"; }
50 ###--------------------------------------------------------------------------
53 export
qw{join_paths
};
59 $e =~ s
,([^/])/+$,$1,;
60 if ($e eq "") { next ELT
; }
61 elsif ($p eq "" || $e =~ m
,^/,) { $p = $e; }
62 else { $p = "$p/$e"; }
67 export
qw{split_path
};
71 my ($dir, $base, $ext) = $path =~ m
,^(?
:(.*)/)?(?:([^/]*)\
.)?
([^./]*)$,;
72 if (defined $base) { $ext = ".$ext"; }
73 else { $base = $ext; $ext = ""; }
74 return ($dir, $base, $ext);
80 $u =~ s
:([^0-9a
-zA
-Z_
./~-]):sprintf "%%%02x", ord $1:eg
;
87 $u =~ s
:\
%([0-9a
-fA
-F
]{2}):chr hex $1:eg
;
91 ###--------------------------------------------------------------------------
96 package TrivGal
::ImageType
{
99 return $TYPE{$ext} = bless { ext
=> $ext }, $cls;
102 my ($me, @args) = @_;
106 my ($me, @args) = @_;
107 return TrivGal
::read_or_set
$me, $me->{mimetype
}, @args;
110 my ($me, @args) = @_;
111 return TrivGal
::read_or_set
$me, $me->{imlibfmt
}, @args;
115 TrivGal
::ImageType
->new(".jpg")->mimetype("image/jpeg")->imlibfmt("jpeg");
116 TrivGal
::ImageType
->new(".png")->mimetype("image/png")->imlibfmt("png");
118 ###--------------------------------------------------------------------------
121 export
qw{$SCOPE $SUFFIX};
122 our $SCOPE //= $::SCOPE
;
123 our $SUFFIX //= $::SUFFIX
;
125 export
qw{$IMGROOT $CACHE $TMP};
126 our $IMGROOT //= "$ENV{HOME}/publish/$SCOPE-html$SUFFIX/tgal-images";
128 ($ENV{XDG_CACHE_HOME
} // "$ENV{HOME}/.cache") .
129 "/tgal/$SCOPE$SUFFIX";
130 our $TMP //= "$CACHE/tmp";
132 export
qw{$ROOTURL $IMGURL $CACHEURL $STATICURL $SCRIPTURL};
133 my $user = getpwuid($>)->name;
134 our $ROOTURL //= "/~$user";
135 our $IMGURL //= "$ROOTURL/tgal-images";
136 our $CACHEURL //= "$ROOTURL/tgal-cache";
137 our $STATICURL //= "$ROOTURL/tgal-static";
141 our %SIZE = (smallthumb
=> 96,
149 my $m = HTML
::Mason
::Request
->instance;
150 my $r = $m->cgi_request;
152 $m->interp->set_escape(u
=> sub { my ($r) = @_; $$r = urlencode
$$r; });
154 return unless !$initp;
156 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
160 ###--------------------------------------------------------------------------
163 export
qw{clean_temp_files
};
164 sub clean_temp_files
() {
167 eval { opendir $d, $TMP; };
169 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
) { return; }
173 FILE
: while (my $name = readdir $d) {
174 next FILE
unless $name =~ /^t(\d+)\-/;
176 next FILE
if kill 0, $pid;
177 my $f = "$TMP/$name";
179 next FILE
if $now - $st->mtime() < 300;
185 ###--------------------------------------------------------------------------
188 package TrivGal
::Image
{
189 use File
::Path
qw{make_path
};
193 my ($cls, $path) = @_;
194 my $imgpath = "$IMGROOT/$path";
195 my $st = stat $imgpath or die "no image `$path'";
204 my ($me, $scale) = @_;
206 my $path = $me->{path
};
207 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
208 my $thumb = "$CACHE/scale.$sz/$path";
209 my $thumburl = "$CACHEURL/scale.$sz/$path";
210 my $st = stat $thumb;
211 if (defined $st && $st->mtime > $me->{mtime
}) { return $thumburl; }
213 my ($dir, $base, $ext) = TrivGal
::split_path
$thumb;
214 my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
216 my $img = $me->{img
};
217 unless (defined $img) {
218 my $imgpath = "$IMGROOT/$path";
219 $img = $me->{img
} = Image
::Imlib2
->load($imgpath);
222 my ($wd, $ht) = ($img->width, $img->height);
223 my $max = $wd > $ht ?
$wd : $ht;
224 if ($max <= $sz) { return "$IMGURL/$path"; }
226 my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
228 $scaled->image_set_format($ty->imlibfmt);
229 $scaled->set_quality(90);
230 my $new = "$TMP/t${$}$ext";
239 ###--------------------------------------------------------------------------
240 ### Directory listings.
242 package TrivGal
::Item
{
244 my ($cls, $name) = @_;
245 return bless { name
=> $name }, $cls;
248 my ($me, @args) = @_;
249 return TrivGal
::read_or_set
$me, $me->{name
}, @args;
252 my ($me, @args) = @_;
253 return TrivGal
::read_or_set
$me, $me->{comment
}, @args;
263 if (-f
"$path/.tgal.index") {
264 open my $f, "<", "$path/.tgal.index";
269 next LINE
if /^\s*(\#|$)/;
271 die "no item" unless $item;
272 $comment = defined $comment ?
$comment . "\n" . $_ : $_;
274 if ($item && $comment) { $item->comment($comment); }
275 my ($indexp, $name, $c) = /(!\s+)?(\S+)\s*(\S|\S.*\S)?\s*$/;
276 $name = urldecode
$name;
278 if ($name =~ m!/$!) {
280 die "can't index a folder" if $indexp;
283 my ($dir, $base, $ext) = TrivGal
::split_path
$name;
284 die "unknown image type" unless $TYPE{lc $ext};
286 die "two index images" if defined $ix;
290 $item = TrivGal
::Item
->new($name);
295 if ($item && $comment) { $item->comment($comment); }
302 ENT
: for my $e (sort @e) {
303 my ($dir, $base, $ext) = split_path
$e;
304 my $dotp = $e =~ /^\./;
305 my $st = stat "$path/$e";
307 if ($dotp || !($st->mode&0004)) { }
308 elsif (-d
$st) { $list = \
@d; }
309 elsif ($TYPE{lc $ext} && -f
$st) { $list = \
@f; }
310 $list and push @
$list, TrivGal
::Item
->new($e);
315 return (\
@d, \
@f, $ix);
324 eval { open $f, "<", "$file"; };
326 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
)
330 while (sysread $f, $buf, 16384) { $contents .= $buf; }
335 export
qw{find_covering_file
};
336 sub find_covering_file
($$$) {
337 my ($top, $path, $name) = @_;
339 my $stuff = contents
"$top/$path/$name"; return $stuff if defined $stuff;
340 if ($path eq "") { return undef; }
341 if ($path =~ m!^(.*)/[^/]+/?!) { $path = $1; }
346 ###----- That's all, folks --------------------------------------------------