From: Mark Wooding Date: Mon, 12 Jun 2023 10:38:51 +0000 (+0100) Subject: mason/.perl-lib/TrivGal.pm, mason/dhandler: Rescale images lazily. X-Git-Url: https://git.distorted.org.uk/~mdw/tgal/commitdiff_plain/784bdf8f1aab63a19724cb60026e78f8f7550ab8 mason/.perl-lib/TrivGal.pm, mason/dhandler: Rescale images lazily. If `TrivGal::Image::scale' can't find an existing scaled image, then by default it just returns immediately with a `?scale=...' link. The code acting on this parameter now passes an additional FORCEP argument to `TrivGal::Image::scale' to get it to actually generate the scaled version. Once the scaled version exists, `TrivGal::Image::scale' returns a direct link to it rather than the `?scale=...' link, which saves a redirect round trip. This has two main effects. * Firstly, and most importantly, it allows the main HTML to be served up immediately, without waiting for the (rather time-consuming) image scaling to happen; and it also allows the scaling to happen with a certain amount of parallelism. * Secondly, it also isolates failures (e.g., due to exceeding memory limits) to individual images. --- diff --git a/mason/.perl-lib/TrivGal.pm b/mason/.perl-lib/TrivGal.pm index ec011f3..e329e00 100644 --- a/mason/.perl-lib/TrivGal.pm +++ b/mason/.perl-lib/TrivGal.pm @@ -217,8 +217,8 @@ package TrivGal::Image { }, $cls; } - sub scale ($$) { - my ($me, $scale) = @_; + sub scale ($$;$) { + my ($me, $scale, $forcep) = @_; my $m = HTML::Mason::Request->instance; my $path = $me->{path}; @@ -228,6 +228,9 @@ package TrivGal::Image { $m->interp->apply_escapes("$CACHEURL/scale.$sz/$path", "u"); my $st = stat $thumb; if (defined $st && $st->mtime > $me->{mtime}) { return $thumburl; } + return + $m->interp->apply_escapes("$SCRIPTURL/$path", "u") . "?scale=$scale" + unless $forcep; my ($dir, $base, $ext) = TrivGal::split_path $thumb; my $ty = $TYPE{lc $ext} or die "unknown type `$ext'"; diff --git a/mason/dhandler b/mason/dhandler index 558da02..de50cd8 100755 --- a/mason/dhandler +++ b/mason/dhandler @@ -152,7 +152,7 @@ Failed to find ‘<% $path |h %>’. if (defined $scale) { my $img = TrivGal::Image->new($path); - $m->redirect($img->scale($scale)); + $m->redirect($img->scale($scale, 1)); } my $real = join_paths $IMGROOT, $path;