mason/.perl-lib/TrivGal.pm, mason/dhandler: Rescale images lazily.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 12 Jun 2023 10:38:51 +0000 (11:38 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 12 Jun 2023 10:48:39 +0000 (11:48 +0100)
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.

mason/.perl-lib/TrivGal.pm
mason/dhandler

index ec011f3..e329e00 100644 (file)
@@ -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'";
index 558da02..de50cd8 100755 (executable)
@@ -152,7 +152,7 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 
        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;