mason/.perl-lib/TrivGal.pm (%SIZE): Modify and extend the available sizes.
[tgal] / mason / .perl-lib / TrivGal.pm
CommitLineData
6ac5dde2
MW
1### -*-cperl-*-
2###
3### Main output for Trivial Gallery.
4###
5### (c) 2021 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of Trivial Gallery.
11###
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.
16###
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.
21###
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/>.
25
26package TrivGal;
27
28use autodie qw{:all};
29
30use Errno;
31use Exporter qw{import};
6ac5dde2 32use File::stat;
bda837fc 33use Image::ExifTool qw{};
6ac5dde2 34use Image::Imlib2;
8b97a4e3 35use Image::Size qw{};
6ac5dde2
MW
36use User::pwent;
37use POSIX;
38
39our @EXPORT;
40sub export (@) { push @EXPORT, @_; }
41
42###--------------------------------------------------------------------------
43### Internal utilities.
44
45sub 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"; }
50}
51
52###--------------------------------------------------------------------------
53### Random utilities.
54
55export qw{join_paths};
56sub join_paths (@) {
57 my @p = @_;
58 my $p = "";
59 ELT: for my $e (@p) {
e9eac06d
MW
60 $e =~ s#^/{2,}#/#;
61 $e =~ s#([^/])/+$#$1#;
6ac5dde2 62 if ($e eq "") { next ELT; }
e9eac06d 63 elsif ($p eq "" || $e =~ m#^/#) { $p = $e; }
6ac5dde2
MW
64 else { $p = "$p/$e"; }
65 }
66 return $p;
67}
68
69export qw{split_path};
70sub split_path ($) {
71 my ($path) = @_;
72
9fc5e8c1
MW
73 my ($dir, $base, $ext) =
74 $path =~ m#^ (?: (.*) /)?
75 (?: ([^/]*) \.)?
76 ([^./]*) $#x;
6ac5dde2
MW
77 if (defined $base) { $ext = ".$ext"; }
78 else { $base = $ext; $ext = ""; }
79 return ($dir, $base, $ext);
80}
81
82export qw{urlencode};
83sub urlencode ($) {
84 my ($u) = @_;
e9eac06d 85 $u =~ s#([^0-9a-zA-Z_./~-])#sprintf "%%%02x", ord $1#eg;
6ac5dde2
MW
86 return $u;
87}
88
89export qw{urldecode};
90sub urldecode ($) {
91 my ($u) = @_;
e9eac06d 92 $u =~ s#\%([0-9a-fA-F]{2})#chr hex $1#eg;
6ac5dde2
MW
93 return $u;
94}
95
96###--------------------------------------------------------------------------
97### Image types.
98
99our %TYPE;
100
101package TrivGal::ImageType {
102 sub new ($$) {
103 my ($cls, $ext) = @_;
104 return $TYPE{$ext} = bless { ext => $ext }, $cls;
105 }
106 sub ext ($) {
107 my ($me, @args) = @_;
108 return $me->{ext};
109 }
110 sub mimetype ($@) {
111 my ($me, @args) = @_;
112 return TrivGal::read_or_set $me, $me->{mimetype}, @args;
113 }
114 sub imlibfmt ($@) {
115 my ($me, @args) = @_;
116 return TrivGal::read_or_set $me, $me->{imlibfmt}, @args;
117 }
65e4ebfd 118}
6ac5dde2
MW
119
120TrivGal::ImageType->new(".jpg")->mimetype("image/jpeg")->imlibfmt("jpeg");
121TrivGal::ImageType->new(".png")->mimetype("image/png")->imlibfmt("png");
122
123###--------------------------------------------------------------------------
124### Configuration.
125
126export qw{$SCOPE $SUFFIX};
127our $SCOPE //= $::SCOPE;
128our $SUFFIX //= $::SUFFIX;
129
130export qw{$IMGROOT $CACHE $TMP};
131our $IMGROOT //= "$ENV{HOME}/publish/$SCOPE-html$SUFFIX/tgal-images";
132our $CACHE //=
133 ($ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache") .
134 "/tgal/$SCOPE$SUFFIX";
135our $TMP //= "$CACHE/tmp";
136
137export qw{$ROOTURL $IMGURL $CACHEURL $STATICURL $SCRIPTURL};
138my $user = getpwuid($>)->name;
139our $ROOTURL //= "/~$user";
140our $IMGURL //= "$ROOTURL/tgal-images";
141our $CACHEURL //= "$ROOTURL/tgal-cache";
142our $STATICURL //= "$ROOTURL/tgal-static";
143our $SCRIPTURL;
144
a38a867d
MW
145export qw{$SRCURL};
146our $SRCURL = "https://git.distorted.org.uk/~mdw/tgal/";
147
6ac5dde2 148export qw{%SIZE};
1408b7a2
MW
149our %SIZE = (smallthumb => 96,
150 medthumb => 144,
3de03c7a
MW
151 bigthumb => 216,
152 tiny => 320,
40c686c2
MW
153 small => 480,
154 embed => 720,
3de03c7a
MW
155 medium => 1080,
156 big => 1600,
157 large => 2400,
158 huge => 3600,
159 vast => 5400,
160 immense => 8100);
6ac5dde2
MW
161
162export qw{init};
163my $initp = 0;
164sub init () {
165 my $m = HTML::Mason::Request->instance;
166 my $r = $m->cgi_request;
167
168 $m->interp->set_escape(u => sub { my ($r) = @_; $$r = urlencode $$r; });
169
170 return unless !$initp;
171
172 $SCRIPTURL //= $r->subprocess_env("SCRIPT_NAME");
173 $initp = 1;
174}
175
176###--------------------------------------------------------------------------
177### Temporary files.
178
179export qw{clean_temp_files};
180sub clean_temp_files () {
181 my $d;
182
183 eval { opendir $d, $TMP; };
184 if ($@) {
185 if ($@->isa("autodie::exception") && $@->errno == ENOENT) { return; }
186 else { die $@; }
187 }
188 my $now = time;
189 FILE: while (my $name = readdir $d) {
190 next FILE unless $name =~ /^t(\d+)\-/;
191 my $pid = $1;
192 next FILE if kill 0, $pid;
193 my $f = "$TMP/$name";
194 my $st = stat $name;
195 next FILE if $now - $st->mtime() < 300;
196 unlink $f;
197 }
198 closedir $d;
199}
200
201###--------------------------------------------------------------------------
202### Scaled images.
203
bda837fc
MW
204my %ORIENT =
205 (1 => [0, 0],
206 2 => [0, 1],
207 3 => [2, 0],
208 4 => [2, 1],
209 5 => [3, 1],
210 6 => [1, 0],
211 7 => [1, 1],
212 8 => [3, 0]);
213
4e74ddf4
MW
214package TrivGal::Image {
215 use File::Path qw{make_path};
216 use File::stat;
217
218 sub new ($$) {
219 my ($cls, $path) = @_;
220 my $imgpath = "$IMGROOT/$path";
221 my $st = stat $imgpath or die "no image `$path'";
222 return bless {
4fcd273f 223 path => $path, imgpath => $imgpath,
4e74ddf4 224 mtime => $st->mtime,
8b97a4e3 225 img => undef,
5d6997b3
MW
226 rot => undef, flip => undef,
227 wd => undef, ht => undef,
8b97a4e3
MW
228 _wd => undef, _ht => undef,
229 sz => undef
4e74ddf4
MW
230 }, $cls;
231 }
232
8b97a4e3
MW
233 sub _getsz ($) {
234 my ($me) = @_;
235 return if defined $me->{_wd};
236
237 my ($wd, $ht, $err) = Image::Size::imgsize $me->{imgpath};
238 defined $wd or die "failed to read size of `$me->{path}': $err";
239 my $sz = $wd; if ($sz < $ht) { $sz = $ht; }
240 @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
241 }
242
5d6997b3
MW
243 sub _getexif ($) {
244 my ($me) = @_;
245 return if defined $me->{wd};
246
247 $me->_getsz;
248 my $exif = new Image::ExifTool; $exif->ExtractInfo($me->{imgpath});
249 my $orient = $exif->GetValue("Orientation", "ValueConv");
250 my ($wd, $ht) = @$me{"_wd", "_ht"};
251 my ($rot, $flip);
252 if (defined $orient) { ($rot, $flip) = $ORIENT{$orient}->@*; }
253 else { ($rot, $flip) = (0, 0); }
254 if ($rot%2) { ($wd, $ht) = ($ht, $wd); }
255 @$me{"rot", "flip", "wd", "ht"} = ($rot, $flip, $wd, $ht);
256 }
257
8b97a4e3 258 sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; }
5d6997b3
MW
259 sub wd ($) { my ($me) = @_; $me->_getexif; return $me->{wd}; }
260 sub ht ($) { my ($me) = @_; $me->_getexif; return $me->{ht}; }
8b97a4e3 261
784bdf8f
MW
262 sub scale ($$;$) {
263 my ($me, $scale, $forcep) = @_;
1b3d6791 264 my $m = HTML::Mason::Request->instance;
4e74ddf4
MW
265
266 my $path = $me->{path};
267 my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
351bb9a6
MW
268 my $url;
269
373818eb 270 if ($me->sz <= $sz)
351bb9a6
MW
271 { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); }
272 else {
273 my $tail = "scale.$sz/$path";
274 my $thumb = "$CACHE/$tail";
275 my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
276
277 my $st = stat $thumb;
278 if ($st && $st->mtime > $me->{mtime})
279 { $url = $thumburl; }
280 elsif (!$forcep) {
281 $url =
282 $m->interp->apply_escapes("$SCRIPTURL/$path", "u") .
283 "?scale=$scale";
284 } else {
351bb9a6
MW
285 my $img = $me->{img};
286 unless (defined $img) {
5d6997b3 287 $me->_getexif;
351bb9a6 288 $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
5d6997b3
MW
289 if ($me->{rot}) { $img->image_orientate($me->{rot}); }
290 if ($me->{flip}) { $img->flip_horizontal(); }
351bb9a6
MW
291 }
292
603ed7ab
MW
293 my ($dir, undef, $ext) = TrivGal::split_path $thumb;
294 my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
351bb9a6
MW
295 my $sc = $sz/$me->sz;
296 my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
297
298 $scaled->image_set_format($ty->imlibfmt);
299 $scaled->set_quality(90);
300 my $new = "$TMP/t$$-$ext";
301 make_path $TMP, { mode => 0771 };
302 $scaled->save($new);
303 make_path $dir, { mode => 0771 };
304 rename $new, $thumb;
305 $url = $thumburl;
bda837fc 306 }
4e74ddf4
MW
307 }
308
351bb9a6 309 return $url;
4e74ddf4 310 }
6ac5dde2
MW
311}
312
313###--------------------------------------------------------------------------
314### Directory listings.
315
316package TrivGal::Item {
317 sub new ($$) {
318 my ($cls, $name) = @_;
319 return bless { name => $name }, $cls;
320 }
321 sub name ($@) {
322 my ($me, @args) = @_;
323 return TrivGal::read_or_set $me, $me->{name}, @args;
324 }
325 sub comment ($@) {
326 my ($me, @args) = @_;
327 return TrivGal::read_or_set $me, $me->{comment}, @args;
328 }
65e4ebfd 329}
6ac5dde2
MW
330
331export qw{listdir};
332sub listdir ($) {
333 my ($path) = @_;
334 my (@d, @f);
335 my $ix = undef;
336
24f4eac3 337 $path =~ s#/$##;
6ac5dde2
MW
338 if (-f "$path/.tgal.index") {
339 open my $f, "<", "$path/.tgal.index";
340 my $item = undef;
341 my $comment = undef;
342 LINE: while (<$f>) {
343 chomp;
344 next LINE if /^\s*(\#|$)/;
345 if (s/^\s+//) {
346 die "no item" unless $item;
347 $comment = defined $comment ? $comment . "\n" . $_ : $_;
348 } else {
349 if ($item && $comment) { $item->comment($comment); }
9940cc5a 350 my ($flags, $name, $c) =
6e51720c 351 /^ (?: ([-!]+) \s+)? # flags
9fc5e8c1
MW
352 (\S+) \s* # filename
353 (\S | \S.*\S )? # start of the comment
354 \s*
355 $/x;
9940cc5a 356 my $indexp = $flags =~ /!/;
6e51720c 357 my $hidep = $flags =~ /-/;
6ac5dde2
MW
358 $name = urldecode $name;
359 my $list;
0485371e 360 $item = TrivGal::Item->new($name);
e9eac06d 361 if ($name =~ m#/$#) {
6ac5dde2
MW
362 $list = \@d;
363 die "can't index a folder" if $indexp;
364 } else {
365 $list = \@f;
d8ab1dea 366 my (undef, undef, $ext) = split_path $name;
6ac5dde2
MW
367 die "unknown image type" unless $TYPE{lc $ext};
368 if ($indexp) {
369 die "two index images" if defined $ix;
370 $ix = $item;
371 }
372 }
6ac5dde2 373 $comment = $c;
6e51720c 374 push @$list, $item unless $hidep;
6ac5dde2
MW
375 }
376 }
377 if ($item && $comment) { $item->comment($comment); }
378 close $f;
379 } else {
ebe19115 380 my $st = stat $path;
32fc7001
MW
381 unless ($st->mode&0004) { return ([], [], undef); }
382
6ac5dde2
MW
383 opendir $d, $path;
384 my @e = readdir $d;
385 closedir $d;
386
387 ENT: for my $e (sort @e) {
d8ab1dea 388 my (undef, undef, $ext) = split_path $e;
6ac5dde2
MW
389 my $dotp = $e =~ /^\./;
390 my $st = stat "$path/$e";
391 my $list = undef;
32fc7001 392 if ($dotp) { }
24f4eac3 393 elsif (-d $st) { $list = \@d; $e .= "/"; }
6ac5dde2
MW
394 elsif ($TYPE{lc $ext} && -f $st) { $list = \@f; }
395 $list and push @$list, TrivGal::Item->new($e);
396 }
397 $ix = $f[0] if @f;
398 }
399
400 return (\@d, \@f, $ix);
401}
402
403export qw{contents};
404sub contents ($) {
405 my ($file) = @_;
406 my $contents = "";
407 my $f;
408 local $@;
409 eval { open $f, "<", "$file"; };
410 if ($@) {
411 if ($@->isa("autodie::exception") && $@->errno == ENOENT)
412 { return undef; }
413 die $@;
414 }
415 while (sysread $f, $buf, 16384) { $contents .= $buf; }
416 close $f;
417 return $contents;
418}
419
420export qw{find_covering_file};
421sub find_covering_file ($$$) {
422 my ($top, $path, $name) = @_;
423 for (;;) {
424 my $stuff = contents "$top/$path/$name"; return $stuff if defined $stuff;
425 if ($path eq "") { return undef; }
e9eac06d 426 if ($path =~ m#^(.*)/[^/]+/?#) { $path = $1; }
6ac5dde2
MW
427 else { $path = ""; }
428 }
429}
430
431###----- That's all, folks --------------------------------------------------
432
433clean_temp_files;
434
4351;