X-Git-Url: https://git.sesse.net/?p=pr0n;a=blobdiff_plain;f=perl%2FSesse%2Fpr0n%2FImage.pm;h=f12a15891792ed1ba0e195e815c4c157df2d1b29;hp=7f3e3c09825a015f9295c40f2541c2c53dea60a6;hb=6c8c4bf3f1e3109523f9ec6117abfdf0e04a0d99;hpb=4e9898a97f5c3c52d1277ed73cb4365b8644870a diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index 7f3e3c0..f12a158 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -13,25 +13,28 @@ sub handler { # die "Har du lest FAQen?"; # } - # Find the event and file name - my ($event,$filename,$xres,$yres); - my $infobox = 1; - if ($r->uri =~ m#^/([a-zA-Z0-9-]+)/original/(nobox/)?([a-zA-Z0-9._-]+)$#) { + # Find the event and file name (nobox/ is for compatibility with legacy URLs). + my ($event,$filename,$xres,$yres,$dpr); + my $infobox = 0; + if ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/original/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $3; - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)/(nobox/)?([a-zA-Z0-9._-]+)$#) { + $infobox = 1 if (defined($2) && $2 eq 'box/'); + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)(?:\@(\d+(?:\.\d+)?))?/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $5; + $filename = $6; $xres = $2; $yres = $3; - $infobox = 0 if (defined($4)); - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(nobox/)?([a-zA-Z0-9._-]+)$#) { + $dpr = $4; + $infobox = 1 if (defined($5) && $5 eq 'box/'); + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $3; $xres = -1; $yres = -1; - $infobox = 0 if (defined($2)); + $infobox = 1 if (defined($2) && $2 eq 'box/'); } + $dpr //= 1; my ($id, $dbwidth, $dbheight); #if ($event eq 'single' && $filename =~ /^(\d+)\.jpeg$/) { @@ -39,45 +42,51 @@ sub handler { #} else { # Look it up in the database - my $ref = $dbh->selectrow_hashref('SELECT id,width,height FROM images WHERE event=? AND vhost=? AND filename=?', - undef, $event, $r->get_server_name, $filename); - error($r, "Could not find $event/$filename", 404, "File not found") unless (defined($ref)); + my $ref = $dbh->selectrow_hashref('SELECT id,render_id,width,height FROM images WHERE event=? AND vhost=? AND filename=?', + undef, $event, Sesse::pr0n::Common::get_server_name($r), $filename); + return error($r, "Could not find $event/$filename", 404, "File not found") unless (defined($ref)); + + if (defined($xres) && defined($yres) && defined($ref->{'render_id'}) && !$infobox) { + # We have a render, we're not asked for the original, and we do not have infobox. + $ref = $dbh->selectrow_hashref('SELECT id,filename,width,height FROM images WHERE id=?', + undef, $ref->{'render_id'}); + return error($r, "Could not find render of $event/$filename", 404, "File not found") unless (defined($ref)); + $filename = $ref->{'filename'}; + } $id = $ref->{'id'}; $dbwidth = $ref->{'width'}; $dbheight = $ref->{'height'}; - #} - - $dbwidth = -1 unless defined($dbwidth); - $dbheight = -1 unless defined($dbheight); - # Scale if we need to do so - my ($fname,$thumbnail) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $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, $xres, $yres); + } # Output the image to the user - my $mime_type; - if ($thumbnail) { - $mime_type = "image/jpeg"; - } else { + my $res = Plack::Response->new(200); + + if (!defined($mime_type)) { $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename); } - $r->content_type($mime_type); + $res->content_type($mime_type); my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname) - or error($r, "stat of $fname: $!"); + or return error($r, "stat of $fname: $!"); - $r->set_content_length($size); - $r->set_last_modified($mtime); + $res->content_length($size); + Sesse::pr0n::Common::set_last_modified($res, $mtime); - # If the client can use cache, by all means do so - if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) { - return $rc; - } - - $r->sendfile($fname); + # # If the client can use cache, by all means do so + #if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) { + # return $rc; + #} - return Apache2::Const::OK; + $res->content(IO::File::WithPath->new($fname)); + return $res; } 1;