mason/.perl-lib/TrivGal.pm, mason/dhandler: Rescale images lazily.
[tgal] / mason / .perl-lib / TrivGal.pm
index 9b05723..e329e00 100644 (file)
@@ -217,15 +217,20 @@ 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};
     my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
     my $thumb = "$CACHE/scale.$sz/$path";
-    my $thumburl = "$CACHEURL/scale.$sz/$path";
+    my $thumburl =
+      $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'";
@@ -285,6 +290,7 @@ sub listdir ($) {
   my (@d, @f);
   my $ix = undef;
 
+  $path =~ s#/$##;
   if (-f "$path/.tgal.index") {
     open my $f, "<", "$path/.tgal.index";
     my $item = undef;
@@ -311,7 +317,7 @@ sub listdir ($) {
          die "can't index a folder" if $indexp;
        } else {
          $list = \@f;
-         my ($dir, $base, $ext) = TrivGal::split_path $name;
+         my ($dir, $base, $ext) = split_path $name;
          die "unknown image type" unless $TYPE{lc $ext};
          if ($indexp) {
            die "two index images" if defined $ix;
@@ -338,7 +344,7 @@ sub listdir ($) {
       my $st = stat "$path/$e";
       my $list = undef;
       if ($dotp) { }
-      elsif (-d $st) { $list = \@d; }
+      elsif (-d $st) { $list = \@d; $e .= "/"; }
       elsif ($TYPE{lc $ext} && -f $st) { $list = \@f; }
       $list and push @$list, TrivGal::Item->new($e);
     }