]> git.sesse.net Git - pr0n/blobdiff - perl/Sesse/pr0n/Common.pm
Small simplification.
[pr0n] / perl / Sesse / pr0n / Common.pm
index c706bb7f72a720fa5f244b8ed2d2e5b5b044693c..4839285dba1b99ea3e790f90a5296d9fdfcb39e4 100644 (file)
@@ -32,7 +32,7 @@ BEGIN {
                require Sesse::pr0n::Config_local;
        };
 
-       $VERSION     = "v3.01";
+       $VERSION     = "v3.20";
        @ISA         = qw(Exporter);
        @EXPORT      = qw(&error &dberror);
        %EXPORT_TAGS = qw();
@@ -207,22 +207,25 @@ sub get_dbh {
 
 sub get_disk_location {
        my ($r, $id) = @_;
-        my $dir = POSIX::floor($id / 256);
+       my $dir = POSIX::floor($id / 256);
        return $Sesse::pr0n::Config::image_base . "images/$dir/$id.jpg";
 }
 
 sub get_cache_location {
-       my ($r, $id, $width, $height, $infobox, $dpr) = @_;
-        my $dir = POSIX::floor($id / 256);
+       my ($id, $width, $height) = @_;
+       my $dir = POSIX::floor($id / 256);
 
-       if ($infobox) {
-               if ($dpr == 1) {
-                       return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box.png";
-               } else {
-                       return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box\@$dpr.png";
-               }
+       return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg";
+}
+
+sub get_infobox_cache_location {
+       my ($id, $width, $height, $dpr) = @_;
+       my $dir = POSIX::floor($id / 256);
+
+       if ($dpr == 1) {
+               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box.png";
        } else {
-               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg";
+               return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box\@$dpr.png";
        }
 }
 
@@ -245,7 +248,7 @@ sub ensure_disk_location_exists {
 
 sub get_mipmap_location {
        my ($r, $id, $width, $height) = @_;
-        my $dir = POSIX::floor($id / 256);
+       my $dir = POSIX::floor($id / 256);
 
        return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-mipmap-$width-$height.jpg";
 }
@@ -569,16 +572,14 @@ sub read_original_image {
 }
 
 sub ensure_cached {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres, @otherres) = @_;
-
-       my ($new_dbwidth, $new_dbheight);
+       my ($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_;
 
        my $fname = get_disk_location($r, $id);
        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($id, $xres, $yres);
        my $err;
        if (! -r $cachename or (-M $cachename > -M $fname)) {
                # If we are in overload mode (aka Slashdot mode), refuse to generate
@@ -588,12 +589,17 @@ sub ensure_cached {
                        error($r, 'System is in overload mode, not doing any scaling');
                }
 
-               my $img;
-               ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
+               my ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres);
+
+               # Update the SQL database if it doesn't contain the required info
+               if (!defined($dbwidth) && defined($new_dbwidth)) {
+                       log_info($r, "Updating width/height for $id: $new_dbwidth x $new_dbheight");
+                       update_image_info($r, $id, $new_dbwidth, $new_dbheight);
+               }
 
                while (defined($xres) && defined($yres)) {
                        my ($nxres, $nyres) = (shift @otherres, shift @otherres);
-                       my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr);
+                       my $cachename = get_cache_location($id, $xres, $yres);
                        
                        my $cimg;
                        if (defined($nxres) && defined($nyres)) {
@@ -613,7 +619,7 @@ sub ensure_cached {
                        my $sf = "1x1";
 
                        if ($xres != -1) {
-                               $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter, 'sampling-factor'=>$sf);
+                               $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter);
                        }
 
                        # Strip EXIF tags etc.
@@ -651,23 +657,17 @@ sub ensure_cached {
                        }
                }
        }
-       
-       # Update the SQL database if it doesn't contain the required info
-       if (!defined($dbwidth) && defined($new_dbwidth)) {
-               log_info($r, "Updating width/height for $id: $new_dbwidth x $new_dbheight");
-               update_image_info($r, $id, $new_dbwidth, $new_dbheight);
-       }
 
        return ($cachename, 'image/jpeg');
 }
 
 sub ensure_infobox_cached {
-       my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres) = @_;
+       my ($r, $filename, $id, $dbwidth, $dbheight, $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 $cachename = get_infobox_cache_location($id, $xres, $yres, $dpr);
        my $err;
        if (! -r $cachename or (-M $cachename > -M $fname)) {
                # If we are in overload mode (aka Slashdot mode), refuse to generate
@@ -719,11 +719,10 @@ sub ensure_infobox_cached {
 
                $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');
        }
-}
 
+       return ($cachename, 'image/png');
+}
 
 sub get_mimetype_from_filename {
        my $filename = shift;
@@ -745,7 +744,7 @@ sub make_infobox {
                $info->{'ExposureProgram'} =~ /shutter\b.*\bpriority/i);
        my $manual_aperture = (defined($info->{'ExposureProgram'}) &&
                $info->{'ExposureProgram'} =~ /aperture\b.*\bpriority/i);
-       if ($info->{'ExposureProgram'} =~ /manual/i) {
+       if (defined($info->{'ExposureProgram'}) && $info->{'ExposureProgram'} =~ /manual/i) {
                $manual_shutter = 1;
                $manual_aperture = 1;
        }
@@ -815,13 +814,13 @@ sub make_infobox {
                $model =~ s/^\s+//;
                $model =~ s/\s+$//;
 
-               push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
+               push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0);
                push @parts, [ $model, 0 ];
        }
        
        # classic fields
        if (scalar @classic_fields > 0) {
-               push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
+               push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0);
 
                my $first_elem = 1;
                for my $field (@classic_fields) {
@@ -836,14 +835,14 @@ sub make_infobox {
                    $info->{'Flash'} =~ /no flash/i ||
                    $info->{'Flash'} =~ /not fired/i ||
                    $info->{'Flash'} =~ /Off/)  {
-                       push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
+                       push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0);
                        push @parts, [ "No flash", 0 ];
                } elsif ($info->{'Flash'} =~ /fired/i ||
                         $info->{'Flash'} =~ /On/) {
-                       push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
+                       push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0);
                        push @parts, [ "Flash", 0 ];
                } else {
-                       push @parts, [ ' - ', 0 ] if (scalar @parts > 0);
+                       push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0);
                        push @parts, [ $info->{'Flash'}, 0 ];
                }
        }