X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=perl%2FSesse%2Fpr0n%2FImage.pm;h=f12a15891792ed1ba0e195e815c4c157df2d1b29;hb=HEAD;hp=da45cfbd098571baece177482b5f795549c2d04f;hpb=f7c70497096c80290e4a8d5d69fe8417d1aa931f;p=pr0n diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index da45cfb..f37e7ac 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -13,29 +13,24 @@ sub handler { # die "Har du lest FAQen?"; # } - # Find the event and file name - my ($event,$filename,$xres,$yres); - my $infobox = 'both'; - if ($r->uri =~ m#^/([a-zA-Z0-9-]+)/original/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { + # Find the event and file name (nobox/ is for compatibility with legacy URLs). + my ($event,$filename,$xres,$yres,$dpr); + if ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/original/?([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $3; - $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/'); - $infobox = 'box' if (defined($2) && $2 eq 'box/'); - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { + $filename = $2; + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)(?:\@(\d+(?:\.\d+)?))?/([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $5; $xres = $2; $yres = $3; - $infobox = 'nobox' if (defined($4) && $4 eq 'nobox/'); - $infobox = 'box' if (defined($4) && $4 eq 'box/'); - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { + $dpr = $4; + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $3; + $filename = $2; $xres = -1; $yres = -1; - $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/'); - $infobox = 'box' if (defined($2) && $2 eq 'box/'); } + $dpr //= 1; my ($id, $dbwidth, $dbheight); #if ($event eq 'single' && $filename =~ /^(\d+)\.jpeg$/) { @@ -43,37 +38,52 @@ 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'})) { + # We have a render, and we're not asked for the original. + $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'}; + my $res = Plack::Response->new(200); + # Scale if we need to do so - my ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres); + my ($fname, $mime_type); + my $accept = $r->header('Accept'); + my $avif_ok = (defined($accept) && $accept =~ /(^|,)image\/avif($|,|;)/); + my $jxl_ok = (defined($accept) && $accept =~ /(^|,)image\/jxl($|,|;)/); + ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $avif_ok, $jxl_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres); + $res->header('Vary' => 'Accept'); # Output the image to the user + 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); - - # If the client can use cache, by all means do so - if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) { - return $rc; - } + $res->content_length($size); + Sesse::pr0n::Common::set_last_modified($res, $mtime); - $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;