]> git.sesse.net Git - pr0n/commitdiff
Split infobox generation out into its own function.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Nov 2015 15:37:54 +0000 (16:37 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Nov 2015 15:37:54 +0000 (16:37 +0100)
perl/Sesse/pr0n/Common.pm
perl/Sesse/pr0n/Image.pm
perl/Sesse/pr0n/WebDAV.pm
perl/update-image-cache.pl

index 6685008d9a98eba693b6eceb7718521c20628123..c706bb7f72a720fa5f244b8ed2d2e5b5b044693c 100644 (file)
@@ -569,15 +569,13 @@ sub read_original_image {
 }
 
 sub ensure_cached {
 }
 
 sub ensure_cached {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres, @otherres) = @_;
+       my ($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres, @otherres) = @_;
 
        my ($new_dbwidth, $new_dbheight);
 
        my $fname = get_disk_location($r, $id);
 
        my ($new_dbwidth, $new_dbheight);
 
        my $fname = get_disk_location($r, $id);
-       if (!$infobox) {
-               unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) {
-                       return ($fname, undef);
-               }
+       unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) {
+               return ($fname, undef);
        }
 
        my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr);
        }
 
        my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr);
@@ -590,56 +588,6 @@ sub ensure_cached {
                        error($r, 'System is in overload mode, not doing any scaling');
                }
 
                        error($r, 'System is in overload mode, not doing any scaling');
                }
 
-               # If we're being asked for the box, make a new image with it.
-               # We don't care about @otherres since each of these images are
-               # already pretty cheap to generate, but we need the exact width so we can make
-               # one in the right size.
-               if ($infobox) {
-                       my ($img, $width, $height);
-
-                       # This is slow, but should fortunately almost never happen, so don't bother
-                       # special-casing it.
-                       if (!defined($dbwidth) || !defined($dbheight)) {
-                               $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0);
-                               $new_dbwidth = $width = $img->Get('columns');
-                               $new_dbheight = $height = $img->Get('rows');
-                       } else {
-                               $img = Image::Magick->new;
-                               $width = $dbwidth;
-                               $height = $dbheight;
-                       }
-                       
-                       if (defined($xres) && defined($yres)) {
-                               ($width, $height) = scale_aspect($width, $height, $xres, $yres);
-                       }
-                       $height = 24 * $dpr;
-                       $img->Set(size=>($width . "x" . $height));
-                       $img->Read('xc:white');
-                               
-                       my $info = Image::ExifTool::ImageInfo($fname);
-                       if (make_infobox($img, $info, $r, $dpr)) {
-                               $img->Quantize(colors=>16, dither=>'False');
-
-                               # Since the image is grayscale, ImageMagick overrides us and writes this
-                               # as grayscale anyway, but at least we get rid of the alpha channel this
-                               # way.
-                               $img->Set(type=>'Palette');
-                       } else {
-                               # Not enough room for the text, make a tiny dummy transparent infobox
-                               @$img = ();
-                               $img->Set(size=>"1x1");
-                               $img->Read('null:');
-
-                               $width = 1;
-                               $height = 1;
-                       }
-                               
-                       $err = $img->write(filename => $cachename, quality => 90, depth => 8);
-                       log_info($r, "New infobox cache: $width x $height for $id.jpg");
-                       
-                       return ($cachename, 'image/png');
-               }
-
                my $img;
                ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
 
                my $img;
                ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
 
@@ -713,6 +661,70 @@ sub ensure_cached {
        return ($cachename, 'image/jpeg');
 }
 
        return ($cachename, 'image/jpeg');
 }
 
+sub ensure_infobox_cached {
+       my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres) = @_;
+
+       my ($new_dbwidth, $new_dbheight);
+
+       my $fname = get_disk_location($r, $id);
+       my $cachename = get_cache_location($r, $id, $xres, $yres, 1, $dpr);
+       my $err;
+       if (! -r $cachename or (-M $cachename > -M $fname)) {
+               # If we are in overload mode (aka Slashdot mode), refuse to generate
+               # new thumbnails.
+               if (Sesse::pr0n::Overload::is_in_overload($r)) {
+                       log_warn($r, "In overload mode, not scaling $id to $xres x $yres");
+                       error($r, 'System is in overload mode, not doing any scaling');
+               }
+
+               # We need the exact width so we can make one in the right size.
+               my ($width, $height);
+
+               # This is slow, but should fortunately almost never happen, so don't bother
+               # special-casing it.
+               if (!defined($dbwidth) || !defined($dbheight)) {
+                       my $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0);
+                       $new_dbwidth = $width = $img->Get('columns');
+                       $new_dbheight = $height = $img->Get('rows');
+               } else {
+                       $width = $dbwidth;
+                       $height = $dbheight;
+               }
+               my $img = Image::Magick->new;
+
+               if (defined($xres) && defined($yres)) {
+                       ($width, $height) = scale_aspect($width, $height, $xres, $yres);
+               }
+               $height = 24 * $dpr;
+               $img->Set(size=>($width . "x" . $height));
+               $img->Read('xc:white');
+
+               my $info = Image::ExifTool::ImageInfo($fname);
+               if (make_infobox($img, $info, $r, $dpr)) {
+                       $img->Quantize(colors=>16, dither=>'False');
+
+                       # Since the image is grayscale, ImageMagick overrides us and writes this
+                       # as grayscale anyway, but at least we get rid of the alpha channel this
+                       # way.
+                       $img->Set(type=>'Palette');
+               } else {
+                       # Not enough room for the text, make a tiny dummy transparent infobox
+                       @$img = ();
+                       $img->Set(size=>"1x1");
+                       $img->Read('null:');
+
+                       $width = 1;
+                       $height = 1;
+               }
+
+               $err = $img->write(filename => $cachename, quality => 90, depth => 8);
+               log_info($r, "New infobox cache: $width x $height for $id.jpg");
+
+               return ($cachename, 'image/png');
+       }
+}
+
+
 sub get_mimetype_from_filename {
        my $filename = shift;
        my MIME::Type $type = $mimetypes->mimeTypeOf($filename);
 sub get_mimetype_from_filename {
        my $filename = shift;
        my MIME::Type $type = $mimetypes->mimeTypeOf($filename);
index 6a4f7a80e71a64f2653d1a2d5d596a04c065aa18..4e272fac5856d29d6af1e485e43d3ee4a656dd28 100644 (file)
@@ -51,7 +51,12 @@ sub handler {
        $dbheight = $ref->{'height'};
 
        # Scale if we need to do so
        $dbheight = $ref->{'height'};
 
        # Scale if we need to do so
-       my ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres);
+       my ($fname, $mime_type);
+       if ($infobox) {
+               ($fname, $mime_type) = Sesse::pr0n::Common::ensure_infobox_cached($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres);
+       } else {
+               ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres);
+       }
 
        # Output the image to the user
        my $res = Plack::Response->new(200);
 
        # Output the image to the user
        my $res = Plack::Response->new(200);
index fb365e79b1e645bbd6b68156131ae9001579cce4..678db9805f1acd4b82ef3cecc65873d82e052386 100644 (file)
@@ -493,7 +493,7 @@ EOF
                                if ($filename !~ /^\.(_|DS_Store)/) {
                                        # FIXME: Ideally we'd want to ensure cache of -1x-1 here as well (for NEFs), but that would
                                        # preclude mipmapping in its current form.
                                if ($filename !~ /^\.(_|DS_Store)/) {
                                        # FIXME: Ideally we'd want to ensure cache of -1x-1 here as well (for NEFs), but that would
                                        # preclude mipmapping in its current form.
-                                       Sesse::pr0n::Common::ensure_cached($r, $filename, $newid, undef, undef, 0, 1, 80, 64, 320, 256);
+                                       Sesse::pr0n::Common::ensure_cached($r, $filename, $newid, undef, undef, 1, 80, 64, 320, 256);
                                }
                                
                                # OK, we got this far, commit
                                }
                                
                                # OK, we got this far, commit
index 8a19841edf2e8307e6146efc74fa28fc99c2c0aa..213a7a0cb1ee6b14386161357762e699ad6b6318 100755 (executable)
@@ -101,7 +101,6 @@ while (my $ref = $q->fetchrow_hashref) {
        if (!$regen_mipmaps) {
                @files = grep { !/mipmap/ } @files;
        }
        if (!$regen_mipmaps) {
                @files = grep { !/mipmap/ } @files;
        }
-       my @bothres = ();
        my @boxres = ();
        my @noboxres = ();
        my $any_old = 0;
        my @boxres = ();
        my @noboxres = ();
        my $any_old = 0;
@@ -110,9 +109,7 @@ while (my $ref = $q->fetchrow_hashref) {
                if ($mtime < $threshold) {
                        $any_old = 1;
                }
                if ($mtime < $threshold) {
                        $any_old = 1;
                }
-               if ($c =~ /$id-(\d+)-(\d+)\.jpg/ || $c =~ /$id-(-1)-(-1)\.jpg/) {
-                       push @bothres, [$1, $2];
-               } elsif ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
+               if ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
                        push @noboxres, [$1, $2];
                } elsif ($c =~ /$id-(\d+)-(\d+)-box\.png/ || $c =~ /$id-(-1)-(-1)-box\.png/) {
                        push @boxres, [$1, $2];
                        push @noboxres, [$1, $2];
                } elsif ($c =~ /$id-(\d+)-(\d+)-box\.png/ || $c =~ /$id-(-1)-(-1)-box\.png/) {
                        push @boxres, [$1, $2];
@@ -120,14 +117,11 @@ while (my $ref = $q->fetchrow_hashref) {
        }
        next unless $any_old;
        unlink (@files);
        }
        next unless $any_old;
        unlink (@files);
-       if (scalar @bothres > 0) {
-               Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'both', 1, sort_res(@bothres));
-       }
        if (scalar @noboxres > 0) {
        if (scalar @noboxres > 0) {
-               Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'nobox', 1, sort_res(@noboxres));
+               Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 1, sort_res(@noboxres));
        }
        if (scalar @boxres > 0) {
        }
        if (scalar @boxres > 0) {
-               Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'box', 1, sort_res(@boxres));
+               Sesse::pr0n::Common::ensure_infobox_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 1, sort_res(@boxres));
        }
        
        my @newfiles = glob("../cache/$dir/$id-*.jpg");
        }
        
        my @newfiles = glob("../cache/$dir/$id-*.jpg");