mason/.perl-lib/TrivGal.pm (Image::scale): Restructure the control flow.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 09:05:15 +0000 (10:05 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 09:21:39 +0000 (10:21 +0100)
Now every path sets `$url' and then we return it at the end.  I'd hoped
to eliminate the multiple calls to `apply_escapes', but the query string
makes that rather annoying without introducing it as a concept
throughout the function, which seems even uglier.

mason/.perl-lib/TrivGal.pm

index 594b383..2d65be5 100644 (file)
@@ -240,45 +240,54 @@ package TrivGal::Image {
 
     my $path = $me->{path};
     my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
+    my $url;
+
     if ($me->sz <= $sz)
-      { return $m->interp->apply_escapes("$IMGURL/$path", "u"); }
-
-    my $tail = "scale.$sz/$path";
-    my $thumb = "$CACHE/$tail";
-    my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "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'";
-
-    my $img = $me->{img};
-    unless (defined $img) {
-      my $exif = new Image::ExifTool;
-      $exif->ExtractInfo($me->{imgpath});
-      my $orient = $exif->GetValue("Orientation", "ValueConv");
-      $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
-      if (defined $orient) {
-       my ($rot, $flip) = @{$ORIENT{$orient}};
-       if ($rot) { $img->image_orientate($rot); }
-       if ($flip) { $img->flip_horizontal(); }
+      { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); }
+    else {
+      my $tail = "scale.$sz/$path";
+      my $thumb = "$CACHE/$tail";
+      my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u");
+
+      my $st = stat $thumb;
+      if ($st && $st->mtime > $me->{mtime})
+       { $url = $thumburl; }
+      elsif (!$forcep) {
+       $url =
+         $m->interp->apply_escapes("$SCRIPTURL/$path", "u") .
+         "?scale=$scale";
+      } else {
+       my ($dir, $base, $ext) = TrivGal::split_path $thumb;
+       my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
+
+       my $img = $me->{img};
+       unless (defined $img) {
+         my $exif = new Image::ExifTool;
+         $exif->ExtractInfo($me->{imgpath});
+         my $orient = $exif->GetValue("Orientation", "ValueConv");
+         $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
+         if (defined $orient) {
+           my ($rot, $flip) = @{$ORIENT{$orient}};
+           if ($rot) { $img->image_orientate($rot); }
+           if ($flip) { $img->flip_horizontal(); }
+         }
+       }
+
+       my $sc = $sz/$me->sz;
+       my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
+
+       $scaled->image_set_format($ty->imlibfmt);
+       $scaled->set_quality(90);
+       my $new = "$TMP/t$$-$ext";
+       make_path $TMP, { mode => 0771 };
+       $scaled->save($new);
+       make_path $dir, { mode => 0771 };
+       rename $new, $thumb;
+       $url = $thumburl;
       }
     }
 
-    my $sc = $sz/$me->sz;
-    my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
-
-    $scaled->image_set_format($ty->imlibfmt);
-    $scaled->set_quality(90);
-    my $new = "$TMP/t$$-$ext";
-    make_path $TMP, { mode => 0771 };
-    $scaled->save($new);
-    make_path $dir, { mode => 0771 };
-    rename $new, $thumb;
-    return $thumburl;
+    return $url;
   }
 }