]> git.sesse.net Git - pr0n/blobdiff - perl/Sesse/pr0n/Common.pm
Last change broke uploads altogether. Fix.
[pr0n] / perl / Sesse / pr0n / Common.pm
index f91526001efcea11858a187b0db1bea2bcdf6922..0b2fa2488f1df4e7e3e463e015d6db0a13a4663e 100644 (file)
@@ -26,13 +26,19 @@ BEGIN {
        use Exporter ();
        our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
 
-       $VERSION     = "v2.04";
+       use Sesse::pr0n::Config;
+       eval {
+               require Sesse::pr0n::Config_local;
+       };
+
+       $VERSION     = "v2.10";
        @ISA         = qw(Exporter);
        @EXPORT      = qw(&error &dberror);
        %EXPORT_TAGS = qw();
        @EXPORT_OK   = qw(&error &dberror);
 
-       our $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=127.0.0.1", "pr0n", "EsVdwImY")
+       our $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
+               $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
                or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
        our $mimetypes = new MIME::Types;
        
@@ -111,10 +117,10 @@ sub scale_aspect {
        return ($width, $height);
 }
 
-sub print_link {
-       my ($r, $title, $baseurl, $param, $defparam) = @_;
-       my $str = "<a href=\"$baseurl";
+sub get_query_string {
+       my ($param, $defparam) = @_;
        my $first = 1;
+       my $str = "";
 
        while (my ($key, $value) = each %$param) {
                next unless defined($value);
@@ -124,8 +130,12 @@ sub print_link {
                $str .= "$key=$value";
                $first = 0;
        }
-       
-       $str .= "\">$title</a>";
+       return $str;
+}
+
+sub print_link {
+       my ($r, $title, $baseurl, $param, $defparam) = @_;
+       my $str = "<a href=\"$baseurl" . get_query_string($param, $defparam) . "\">$title</a>";
        $r->print($str);
 }
 
@@ -134,7 +144,8 @@ sub get_dbh {
        if (!(defined($dbh) && $dbh->ping)) {
                # Try to reconnect
                Apache2::ServerUtil->server->log_error("Lost contact with PostgreSQL server, trying to reconnect...");
-               unless ($dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=127.0.0.1", "pr0n", "EsVdwImY")) {
+               unless ($dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
+                       $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)) {
                        $dbh = undef;
                        die "Couldn't connect to PostgreSQL database";
                }
@@ -185,8 +196,8 @@ sub update_width_height {
 
        # update the last_picture cache as well (this should of course be done
        # via a trigger, but this is less complicated :-) )
-       $dbh->do('UPDATE events SET last_picture=(SELECT COALESCE(MAX(date),\'1970-01-01 00:00:00\') FROM images WHERE event=events.id) WHERE id=(SELECT event FROM images WHERE id=?)',
-               undef, $id)
+       $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE event=(SELECT event FROM images WHERE id=?)',
+               undef, $datetime, $id)
                or die "Couldn't update last_picture in SQL: $!";
 }
 
@@ -262,7 +273,7 @@ sub ensure_cached {
        my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres, @otherres) = @_;
 
        my $fname = get_disk_location($r, $id);
-       unless (defined($xres) && ($xres < $dbheight || $yres < $dbwidth || $dbwidth == -1 || $dbheight == -1)) {
+       unless (defined($xres) && ($xres < $dbheight || $yres < $dbwidth || $dbwidth == -1 || $dbheight == -1 || $xres == -1)) {
                return ($fname, 0);
        }
 
@@ -281,7 +292,6 @@ sub ensure_cached {
 
                # NEF files aren't autodetected
                $fname = "NEF:$fname" if ($filename =~ /\.nef$/i);
-               $r->log->warn("Generating $fname for $filename");
                
                my $err = $magick->Read($fname);
                if ($err) {
@@ -334,9 +344,11 @@ sub ensure_cached {
                                $quality = 80;
                        }
 
-                       $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter);
+                       if ($xres != -1) {
+                               $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter);
+                       }
 
-                       if (($nwidth >= 800 || $nheight >= 600) && $infobox == 1) {
+                       if (($nwidth >= 800 || $nheight >= 600 || $xres == -1) && $infobox == 1) {
                                make_infobox($cimg, $info, $r);
                        }
 
@@ -385,7 +397,12 @@ sub make_infobox {
                push @lines, "$1-$2-$3 $4:$5";
        }
 
-       push @lines, $info->{'Model'} if (defined($info->{'Model'}));
+       if (defined($info->{'Model'})) {
+               my $model = $info->{'Model'}; 
+               $model =~ s/^\s+//;
+               $model =~ s/\s+$//;
+               push @lines, $model;
+       }
        
        # classic fields
        if (defined($info->{'FocalLength'}) && $info->{'FocalLength'} =~ /^(\d+)(?:\.\d+)?(?:mm)?$/) {
@@ -429,9 +446,13 @@ sub make_infobox {
        }
 
        if (defined($info->{'Flash'})) {
-               if ($info->{'Flash'} =~ /did not fire/ || $info->{'Flash'} =~ /No Flash/) {
+               if ($info->{'Flash'} =~ /did not fire/i ||
+                   $info->{'Flash'} =~ /no flash/i ||
+                   $info->{'Flash'} =~ /not fired/i ||
+                   $info->{'Flash'} =~ /Off/)  {
                        push @lines, "No flash";
-               } elsif ($info->{'Flash'} =~ /fired/) {
+               } elsif ($info->{'Flash'} =~ /fired/i ||
+                        $info->{'Flash'} =~ /On/) {
                        push @lines, "Flash";
                } else {
                        push @lines, $info->{'Flash'};