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