838de976ae71ed160e709e1db6f90884f8bfe22d
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 = (bigthumb
=> 228, view
=> 1200);
146 my $m = HTML
::Mason
::Request
->instance;
147 my $r = $m->cgi_request;
149 $m->interp->set_escape(u
=> sub { my ($r) = @_; $$r = urlencode
$$r; });
151 return unless !$initp;
153 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
157 ###--------------------------------------------------------------------------
160 export
qw{clean_temp_files
};
161 sub clean_temp_files
() {
164 eval { opendir $d, $TMP; };
166 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
) { return; }
170 FILE
: while (my $name = readdir $d) {
171 next FILE
unless $name =~ /^t(\d+)\-/;
173 next FILE
if kill 0, $pid;
174 my $f = "$TMP/$name";
176 next FILE
if $now - $st->mtime() < 300;
182 ###--------------------------------------------------------------------------
185 package TrivGal
::Image
{
186 use File
::Path
qw{make_path
};
190 my ($cls, $path) = @_;
191 my $imgpath = "$IMGROOT/$path";
192 my $st = stat $imgpath or die "no image `$path'";
201 my ($me, $scale) = @_;
203 my $path = $me->{path
};
204 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
205 my $thumb = "$CACHE/scale.$sz/$path";
206 my $thumburl = "$CACHEURL/scale.$sz/$path";
207 my $st = stat $thumb;
208 if (defined $st && $st->mtime > $me->{mtime
}) { return $thumburl; }
210 my ($dir, $base, $ext) = TrivGal
::split_path
$thumb;
211 my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
213 my $img = $me->{img
};
214 unless (defined $img) {
215 my $imgpath = "$IMGROOT/$path";
216 $img = $me->{img
} = Image
::Imlib2
->load($imgpath);
219 my ($wd, $ht) = ($img->width, $img->height);
220 my $max = $wd > $ht ?
$wd : $ht;
221 if ($max <= $sz) { return "$IMGURL/$path"; }
223 my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
225 $scaled->image_set_format($ty->imlibfmt);
226 $scaled->set_quality(90);
227 my $new = "$TMP/t${$}$ext";
236 ###--------------------------------------------------------------------------
237 ### Directory listings.
239 package TrivGal
::Item
{
241 my ($cls, $name) = @_;
242 return bless { name
=> $name }, $cls;
245 my ($me, @args) = @_;
246 return TrivGal
::read_or_set
$me, $me->{name
}, @args;
249 my ($me, @args) = @_;
250 return TrivGal
::read_or_set
$me, $me->{comment
}, @args;
260 if (-f
"$path/.tgal.index") {
261 open my $f, "<", "$path/.tgal.index";
266 next LINE
if /^\s*(\#|$)/;
268 die "no item" unless $item;
269 $comment = defined $comment ?
$comment . "\n" . $_ : $_;
271 if ($item && $comment) { $item->comment($comment); }
272 my ($indexp, $name, $c) = /(!\s+)?(\S+)\s*(\S|\S.*\S)?\s*$/;
273 $name = urldecode
$name;
275 if ($name =~ m!/$!) {
277 die "can't index a folder" if $indexp;
280 my ($dir, $base, $ext) = TrivGal
::split_path
$name;
281 die "unknown image type" unless $TYPE{lc $ext};
283 die "two index images" if defined $ix;
287 $item = TrivGal
::Item
->new($name);
292 if ($item && $comment) { $item->comment($comment); }
299 ENT
: for my $e (sort @e) {
300 my ($dir, $base, $ext) = split_path
$e;
301 my $dotp = $e =~ /^\./;
302 my $st = stat "$path/$e";
304 if ($dotp || !($st->mode&0004)) { }
305 elsif (-d
$st) { $list = \
@d; }
306 elsif ($TYPE{lc $ext} && -f
$st) { $list = \
@f; }
307 $list and push @
$list, TrivGal
::Item
->new($e);
312 return (\
@d, \
@f, $ix);
321 eval { open $f, "<", "$file"; };
323 if ($@
->isa("autodie::exception") && $@
->errno == ENOENT
)
327 while (sysread $f, $buf, 16384) { $contents .= $buf; }
332 export
qw{find_covering_file
};
333 sub find_covering_file
($$$) {
334 my ($top, $path, $name) = @_;
336 my $stuff = contents
"$top/$path/$name"; return $stuff if defined $stuff;
337 if ($path eq "") { return undef; }
338 if ($path =~ m!^(.*)/[^/]+/?!) { $path = $1; }
343 ###----- That's all, folks --------------------------------------------------