From 84460e4c9e238c3f3b661756796f764df2441aaa Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 6 Jun 2021 14:37:43 +0200 Subject: [PATCH 01/16] Add rudimentary AVIF support. Works with recent Chrome, and soon hopefully with Firefox. Needs a recent aomenc and MP4BOx (gpac). AVIFs have to be added manually and offline using the make-avif.pl script for now. --- doc/README | 5 ++++ perl/Sesse/pr0n/Common.pm | 55 ++++++++++++++++++++++++++++++++------- perl/Sesse/pr0n/Image.pm | 8 ++++-- perl/make-avif.pl | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100755 perl/make-avif.pl diff --git a/doc/README b/doc/README index 42d467c..3b088c1 100644 --- a/doc/README +++ b/doc/README @@ -15,6 +15,11 @@ backend pr0n { sub vcl_recv { if (req.http.host ~ "^pr0n\.sesse\.net(:[0-9]+)?$") { set req.backend_hint = pr0n; + if (req.http.accept ~ "(^|,)image/avif($|,|;)") { + set req.http.accept = "image/avif,*/*"; + } else { + set req.http.accept = "*/*"; + } if (req.method == "PUT") { return (pipe); } diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 0495442..8c21541 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -22,6 +22,7 @@ use HTML::Entities; use URI::Escape; use File::Basename; use Crypt::Eksblowfish::Bcrypt; +use File::Temp; BEGIN { use Exporter (); @@ -212,10 +213,10 @@ sub get_disk_location { } sub get_cache_location { - my ($id, $width, $height) = @_; + my ($id, $width, $height, $format) = @_; my $dir = POSIX::floor($id / 256); - return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg"; + return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.$format"; } sub get_infobox_cache_location { @@ -572,14 +573,21 @@ sub read_original_image { } sub ensure_cached { - my ($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; + my ($r, $avif_ok, $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($id, $xres, $yres); + # See if we have an up-to-date AVIF to serve. + # (We never generate them on-the-fly, since they're so slow.) + my $cachename = get_cache_location($id, $xres, $yres, 'avif'); + if ($avif_ok && -r $cachename and (-M $cachename <= -M $fname)) { + return ($cachename, 'image/avif'); + } + + $cachename = get_cache_location($id, $xres, $yres, 'jpg'); if (! -r $cachename or (-M $cachename > -M $fname)) { # If we are in overload mode (aka Slashdot mode), refuse to generate # new thumbnails. @@ -588,14 +596,14 @@ sub ensure_cached { error($r, 'System is in overload mode, not doing any scaling'); } - make_cache($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); + make_cache($r, $filename, $id, $dbwidth, $dbheight, 'jpg', $xres, $yres, @otherres); } return ($cachename, 'image/jpeg'); } sub make_cache { - my ($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; + my ($r, $filename, $id, $dbwidth, $dbheight, $format, $xres, $yres, @otherres) = @_; my ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); @@ -608,7 +616,7 @@ sub make_cache { my $err; while (defined($xres) && defined($yres)) { my ($nxres, $nyres) = (shift @otherres, shift @otherres); - my $cachename = get_cache_location($id, $xres, $yres); + my $cachename = get_cache_location($id, $xres, $yres, $format); my $cimg; if (defined($nxres) && defined($nyres)) { @@ -623,6 +631,11 @@ sub make_cache { my $height = $img->Get('rows'); my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres); + if ($format eq 'avif') { # AVIF uses 4:2:0. + ++$nwidth if ($nwidth % 2 == 1); + ++$nheight if ($nheight % 2 == 1); + } + my $filter = 'Lanczos'; my $quality = 87; my $sf = "1x1"; @@ -634,7 +647,7 @@ sub make_cache { # Strip EXIF tags etc. $cimg->Strip(); - { + if ($format eq 'jpg') { my %parms = ( filename => $cachename, quality => $quality @@ -647,13 +660,37 @@ sub make_cache { $parms{'sampling-factor'} = $sf; } $err = $cimg->write(%parms); + } elsif ($format eq 'avif') { + # ImageMagick doesn't have AVIF support until version 7, + # and Debian hasn't packaged that even in unstable as of 2021. + # So we'll need to do it the manual way. (We don't use /tmp, for security reasons.) + (my $dirname = $cachename) =~ s,/[^/]*$,,; + my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.yuv'); + # Write a Y4M header, so that we get the chroma siting and color space correct. + printf $fh "YUV4MPEG2 W%d H%d F25:1 Ip A1:1 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\nFRAME\n", $nwidth, $nheight; + my %parms = ( + file => $fh, + filename => $raw_filename, + 'sampling-factor' => '2x2' + ); + $cimg->write(%parms); + close($fh); + my $ivf_filename; + ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); + close($fh); + system('aomenc', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-w', $nwidth, '-h', $nheight, '-o', $ivf_filename, $raw_filename); + unlink($raw_filename); + system('MP4Box', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); + unlink($ivf_filename); + } else { + die "Unknown format $format"; } undef $cimg; ($xres, $yres) = ($nxres, $nyres); - log_info($r, "New cache: $nwidth x $nheight for $id.jpg"); + log_info($r, "New cache: $nwidth x $nheight ($format) for $id"); } undef $img; diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index f12a158..13d9ee5 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -58,16 +58,20 @@ sub handler { $dbwidth = $ref->{'width'}; $dbheight = $ref->{'height'}; + my $res = Plack::Response->new(200); + # Scale if we need to do so 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); + my $accept = $r->header('Accept'); + my $avif_ok = (defined($accept) && $accept =~ /(^|,)image\/avif($|,|;)/); + ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $avif_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres); + $res->header('Vary' => 'Accept'); } # Output the image to the user - my $res = Plack::Response->new(200); if (!defined($mime_type)) { $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename); diff --git a/perl/make-avif.pl b/perl/make-avif.pl new file mode 100755 index 0000000..2db5709 --- /dev/null +++ b/perl/make-avif.pl @@ -0,0 +1,45 @@ +#! /usr/bin/perl + +use lib qw(.); +use DBI; +use POSIX; +use Sesse::pr0n::Common; +use strict; +use warnings; + +use Sesse::pr0n::Config; +eval { + require Sesse::pr0n::Config_local; +}; + +my $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; +$dbh->{RaiseError} = 1; + +# TODO: Do we need to care about renders? +for my $id (@ARGV) { + my $dir = POSIX::floor($id / 256); + my $base = $Sesse::pr0n::Config::image_base . "cache/$dir"; + my @res = (); + for my $file (<$base/$id-*-nobox.jpg>) { # TODO: --1--1.jpg, too. + my $fname = File::Basename::basename($file); + my ($width, $height) = $fname =~ /^$id-(\d+)-(\d+)-nobox\.jpg$/ or die $fname; + (my $avif_file = $file) =~ s/jpg$/avif/; + unless (-r $avif_file) { + push @res, ($width, $height); + print "$id to $width x $height...\n"; + } + } + if (scalar @res > 0) { + my $filename = Sesse::pr0n::Common::get_disk_location({}, $id); + + # Look up the width/height in the database. + my ($dbwidth, $dbheight); + my $ref = $dbh->selectrow_hashref('SELECT width,height FROM images WHERE id=?', undef, $id); + $dbwidth = $ref->{'width'}; + $dbheight = $ref->{'height'}; + + Sesse::pr0n::Common::make_cache({}, $filename, $id, $dbwidth, $dbheight, 'avif', @res); + } +} -- 2.39.2 From b6c6d09685a2eded3d376235229d03e296ee3dd6 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 6 Jun 2021 17:59:45 +0200 Subject: [PATCH 02/16] Make AVIF encoding a bit quieter. --- perl/Sesse/pr0n/Common.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 8c21541..e87c195 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -678,9 +678,9 @@ sub make_cache { my $ivf_filename; ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); close($fh); - system('aomenc', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-w', $nwidth, '-h', $nheight, '-o', $ivf_filename, $raw_filename); + system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-w', $nwidth, '-h', $nheight, '-o', $ivf_filename, $raw_filename); unlink($raw_filename); - system('MP4Box', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); + system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); } else { die "Unknown format $format"; -- 2.39.2 From 41782920ffb54fcfa21876a0257059f0d7ae89c3 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 6 Jun 2021 20:04:27 +0200 Subject: [PATCH 03/16] Drop the redundant -w and -h parameters to aomenc. --- perl/Sesse/pr0n/Common.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index e87c195..c859721 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -678,7 +678,7 @@ sub make_cache { my $ivf_filename; ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); close($fh); - system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-w', $nwidth, '-h', $nheight, '-o', $ivf_filename, $raw_filename); + system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); unlink($raw_filename); system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); -- 2.39.2 From 2542338bfc131e075eca5829ba96ba5492ddc6f2 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 6 Jun 2021 20:06:22 +0200 Subject: [PATCH 04/16] Do not give --color-primaries= to aomenc; it causes different rendering from the PNG/JPEG (which presumably assumes BT.709 if nothing else is given). --- perl/Sesse/pr0n/Common.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index c859721..c9511af 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -678,7 +678,7 @@ sub make_cache { my $ivf_filename; ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); close($fh); - system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--color-primaries=bt601', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); + system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); unlink($raw_filename); system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); -- 2.39.2 From c7fd47d6b2323d28a5ece7f3019b40224605cb61 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 9 Jun 2021 00:18:39 +0200 Subject: [PATCH 05/16] Switch to 4:4:4 for AVIF. --- perl/Sesse/pr0n/Common.pm | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index c9511af..f6cea5b 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -631,11 +631,6 @@ sub make_cache { my $height = $img->Get('rows'); my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres); - if ($format eq 'avif') { # AVIF uses 4:2:0. - ++$nwidth if ($nwidth % 2 == 1); - ++$nheight if ($nheight % 2 == 1); - } - my $filter = 'Lanczos'; my $quality = 87; my $sf = "1x1"; @@ -665,20 +660,20 @@ sub make_cache { # and Debian hasn't packaged that even in unstable as of 2021. # So we'll need to do it the manual way. (We don't use /tmp, for security reasons.) (my $dirname = $cachename) =~ s,/[^/]*$,,; - my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.yuv'); - # Write a Y4M header, so that we get the chroma siting and color space correct. - printf $fh "YUV4MPEG2 W%d H%d F25:1 Ip A1:1 C420jpeg XYSCSS=420JPEG XCOLORRANGE=FULL\nFRAME\n", $nwidth, $nheight; + my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ycbcr'); + # Write a Y4M header, so that we get the chroma range correct. + printf $fh "YUV4MPEG2 W%d H%d F25:1 Ip A1:1 C444 XYSCSS=444 XCOLORRANGE=FULL\nFRAME\n", $nwidth, $nheight; my %parms = ( file => $fh, filename => $raw_filename, - 'sampling-factor' => '2x2' + interlace => 'Plane' ); $cimg->write(%parms); close($fh); my $ivf_filename; ($fh, $ivf_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ivf'); close($fh); - system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=10', '--target-bitrate=0', '--good', '--aq-mode=1', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); + system('aomenc', '--quiet', '--cpu-used=0', '--bit-depth=10', '--end-usage=q', '--cq-level=13', '--target-bitrate=0', '--good', '--aq-mode=1', '--matrix-coefficients=bt601', '-o', $ivf_filename, $raw_filename); unlink($raw_filename); system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); -- 2.39.2 From a2d71665db6bf86f40ae9deb956cfc6463645529 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 15 Jun 2021 23:55:45 +0200 Subject: [PATCH 06/16] Support JPEG XL, like AVIF. --- perl/Sesse/pr0n/Common.pm | 23 ++++++++++++++++++++--- perl/Sesse/pr0n/Image.pm | 3 ++- perl/{make-avif.pl => make-jxl.pl} | 6 +++--- 3 files changed, 25 insertions(+), 7 deletions(-) rename perl/{make-avif.pl => make-jxl.pl} (92%) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index f6cea5b..816c003 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -573,16 +573,21 @@ sub read_original_image { } sub ensure_cached { - my ($r, $avif_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; + my ($r, $avif_ok, $jxl_ok, $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); } - # See if we have an up-to-date AVIF to serve. + # See if we have an up-to-date JPEG-XL or AVIF to serve. # (We never generate them on-the-fly, since they're so slow.) - my $cachename = get_cache_location($id, $xres, $yres, 'avif'); + my $cachename = get_cache_location($id, $xres, $yres, 'jxl'); + if ($jxl_ok && -r $cachename and (-M $cachename <= -M $fname)) { + return ($cachename, 'image/jxl'); + } + + $cachename = get_cache_location($id, $xres, $yres, 'avif'); if ($avif_ok && -r $cachename and (-M $cachename <= -M $fname)) { return ($cachename, 'image/avif'); } @@ -677,6 +682,18 @@ sub make_cache { unlink($raw_filename); system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename); unlink($ivf_filename); + } elsif ($format eq 'jxl') { + # Similar, for JPEG-XL. + (my $dirname = $cachename) =~ s,/[^/]*$,,; + my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ppm'); + my %parms = ( + file => $fh, + filename => $raw_filename + ); + $cimg->write(%parms); + close($fh); + system('cjxl', '-p', $raw_filename, $cachename); + unlink($raw_filename); } else { die "Unknown format $format"; } diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index 13d9ee5..8671b57 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -67,7 +67,8 @@ sub handler { } else { my $accept = $r->header('Accept'); my $avif_ok = (defined($accept) && $accept =~ /(^|,)image\/avif($|,|;)/); - ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $avif_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres); + 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'); } diff --git a/perl/make-avif.pl b/perl/make-jxl.pl similarity index 92% rename from perl/make-avif.pl rename to perl/make-jxl.pl index 2db5709..0c5b708 100755 --- a/perl/make-avif.pl +++ b/perl/make-jxl.pl @@ -25,8 +25,8 @@ for my $id (@ARGV) { for my $file (<$base/$id-*-nobox.jpg>) { # TODO: --1--1.jpg, too. my $fname = File::Basename::basename($file); my ($width, $height) = $fname =~ /^$id-(\d+)-(\d+)-nobox\.jpg$/ or die $fname; - (my $avif_file = $file) =~ s/jpg$/avif/; - unless (-r $avif_file) { + (my $jxl_file = $file) =~ s/jpg$/jxl/; + unless (-r $jxl_file) { push @res, ($width, $height); print "$id to $width x $height...\n"; } @@ -40,6 +40,6 @@ for my $id (@ARGV) { $dbwidth = $ref->{'width'}; $dbheight = $ref->{'height'}; - Sesse::pr0n::Common::make_cache({}, $filename, $id, $dbwidth, $dbheight, 'avif', @res); + Sesse::pr0n::Common::make_cache({}, $filename, $id, $dbwidth, $dbheight, 'jxl', @res); } } -- 2.39.2 From 8659566dfd0c4a181b4562bc171a8f7df933668f Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 13 Jul 2021 14:22:43 +0200 Subject: [PATCH 07/16] Fix cache-on-upload. --- perl/Sesse/pr0n/Upload.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/Sesse/pr0n/Upload.pm b/perl/Sesse/pr0n/Upload.pm index 6f44f7a..0fdce3a 100644 --- a/perl/Sesse/pr0n/Upload.pm +++ b/perl/Sesse/pr0n/Upload.pm @@ -97,7 +97,7 @@ sub handler { # Make cache while we're at it. # 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, 320, 256); + Sesse::pr0n::Common::ensure_cached($r, 0, 0, $filename, $newid, undef, undef, 320, 256); # OK, we got this far, commit $dbh->commit; -- 2.39.2 From 30e800d975f1d75656094b37303dcc62facda9a5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 13 Nov 2022 20:49:08 +0100 Subject: [PATCH 08/16] Newer ImageMagick supports NEF via libraw-dev, and the old fallback is seemingly no good, so switch again. --- perl/Sesse/pr0n/Common.pm | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 816c003..8a42698 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -528,17 +528,9 @@ sub read_original_image { } my $err; - # ImageMagick can handle NEF files, but it does it by calling dcraw as a delegate. - # The delegate support is rather broken and causes very odd stuff to happen when - # more than one thread does this at the same time. Thus, we simply do it ourselves. if ($filename =~ /\.(nef|cr2)$/i) { - # this would suffice if ImageMagick gets to fix their handling - # $physical_fname = "NEF:$physical_fname"; - - open DCRAW, "-|", "dcraw", "-w", "-c", $physical_fname - or error("dcraw: $!"); - $err = $magick->Read(file => \*DCRAW); - close(DCRAW); + $physical_fname = "NEF:$physical_fname"; + $err = $magick->Read($physical_fname); } else { # We always want YCbCr JPEGs. Setting this explicitly here instead of using # RGB is slightly faster (no colorspace conversion needed) and works equally -- 2.39.2 From d43ebfec566433afc04bfec5df55f69752df7399 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 13 Nov 2022 20:51:07 +0100 Subject: [PATCH 09/16] Fix strange indent. --- files/pr0n-fullscreen.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/files/pr0n-fullscreen.css b/files/pr0n-fullscreen.css index b501128..d893d0d 100644 --- a/files/pr0n-fullscreen.css +++ b/files/pr0n-fullscreen.css @@ -10,6 +10,9 @@ body { .container { text-align: center; } +.container #optionmenu { + text-align: left; +} img, #text { vertical-align: middle; -- 2.39.2 From c8c8d4ba8a2e5a3c8b6a6b787a354f72c29964e2 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 5 Mar 2023 00:26:37 +0100 Subject: [PATCH 10/16] Render the infobox on the client side. This removes a lot of cached crap on the server side, and just generally makes so much more sense. It makes fullscreen setup a bit slower (though not that much, thanks to a new covering partial index). Interestingly, it now more often shows the infobox on mobile, since it no longer cares about text overflow; it's possible we should try to do something about that. --- files/pr0n-fullscreen.css | 11 +++ files/pr0n-fullscreen.js | 53 ++++++++++----- perl/Sesse/pr0n/Common.pm | 134 +------------------------------------ perl/Sesse/pr0n/Image.pm | 34 ++++------ perl/Sesse/pr0n/Index.pm | 52 +++++++++++++- perl/Sesse/pr0n/pr0n.pm | 1 - perl/update-image-cache.pl | 6 -- sql/pr0n.sql | 6 +- 8 files changed, 119 insertions(+), 178 deletions(-) diff --git a/files/pr0n-fullscreen.css b/files/pr0n-fullscreen.css index d893d0d..d4f897a 100644 --- a/files/pr0n-fullscreen.css +++ b/files/pr0n-fullscreen.css @@ -66,6 +66,17 @@ img, #text { display: none; } +.fsbox { + position: absolute; + transform-origin: top left; + background-color: white; + color: black; + font: 12px sans-serif; + line-height: 24px; + border-top: 1px solid black; + z-index: 1; +} + html, body { overflow: hidden; } diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index 076c626..b431a3c 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -104,7 +104,7 @@ function rename_element(old_name, new_name) return elem; } -function display_image(url, backend_width, backend_height, elem_id, offset, box) +function display_image(url, backend_width, backend_height, elem_id, offset) { // See if this image already exists in the DOM; if not, add it. var img = document.getElementById(elem_id); @@ -112,7 +112,7 @@ function display_image(url, backend_width, backend_height, elem_id, offset, box) img = document.createElement("img"); img.id = elem_id; img.alt = ""; - img.className = box ? "fsbox" : "fsimg"; + img.className = "fsimg"; } img.style.position = "absolute"; img.style.transformOrigin = "top left"; @@ -120,7 +120,7 @@ function display_image(url, backend_width, backend_height, elem_id, offset, box) if (offset === 0) { img.src = url; - position_image(img, backend_width, backend_height, offset, box); + position_image(img, backend_width, backend_height, offset, false); } else { // This is a preload, so wait for the main image to be ready. // The test for .complete is an old IE hack, which I don't know if is relevant anymore. @@ -135,12 +135,41 @@ function display_image(url, backend_width, backend_height, elem_id, offset, box) // scroll offset completely off. img.style.display = 'none'; setTimeout(function() { - position_image(img, backend_width, backend_height, offset, box); + position_image(img, backend_width, backend_height, offset, false); img.style.display = null; }, 1); } } +function display_infobox(html, backend_width, backend_height, elem_id, offset) +{ + // See if this image already exists in the DOM; if not, add it. + var box = document.getElementById(elem_id); + if (box === null) { + box = document.createElement("div"); + box.id = elem_id; + box.alt = ""; + box.className = "fsbox"; + } + box.style.position = "absolute"; + box.style.transformOrigin = "top left"; + box.innerHTML = html; + document.getElementById("main").appendChild(box); + + if (offset === 0) { + position_image(box, backend_width, backend_height, offset, true); + } else { + // This is a preload. + // Seemingly one needs to delay position_image(), or Firefox will set the initial + // scroll offset completely off. + box.style.display = 'none'; + setTimeout(function() { + position_image(box, backend_width, backend_height, offset, true); + box.style.display = null; + }, 1); + } +} + function display_image_num(num, offset) { var screen_size = find_width(); @@ -160,19 +189,11 @@ function display_image_num(num, offset) var url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/" + filename; var elem_id = num; - display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, false); + display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset); if (global_infobox) { - var url; - var dpr = find_dpr(); var elem_id = num + "_box"; - if (dpr == 1) { - url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/box/" + filename; - } else { - url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "@" + dpr.toFixed(2) + "/box/" + filename; - } - display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, true); - document.getElementById(elem_id).style.transform += " scale(" + (1.0 / dpr) + ")"; + display_infobox(global_image_list[num][4], adjusted_size[2], adjusted_size[3], elem_id, offset); } if (offset === 0) { @@ -254,6 +275,8 @@ function position_image(img, backend_width, backend_height, offset, box) if (box) { img.style.top = Math.min(top + height, screen_size[1] - 24) / dpr + "px"; + img.style.width = (width / dpr) + "px"; + img.style.height = "24px"; } else { img.style.top = (top / dpr) + "px"; img.style.lineHeight = (height / dpr) + "px"; @@ -531,7 +554,7 @@ function set_swipe_pos(x, transition) var inum = parseInt(child.id.replace("_box", "")); var offset = inum - global_image_num; child.style.transition = transition; - child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px) scale(" + (1.0 / dpr) + ")"; + child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)"; } } } diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 8a42698..2799364 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -219,17 +219,6 @@ sub get_cache_location { return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.$format"; } -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-box\@$dpr.png"; - } -} - sub ensure_disk_location_exists { my ($r, $id) = @_; my $dir = POSIX::floor($id / 256); @@ -708,69 +697,6 @@ sub make_cache { } } -sub ensure_infobox_cached { - my ($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres) = @_; - - my ($new_dbwidth, $new_dbheight); - - my $fname = get_disk_location($r, $id); - 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 - # 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); @@ -778,8 +704,8 @@ sub get_mimetype_from_filename { return $type; } -sub make_infobox { - my ($img, $info, $r, $dpr) = @_; +sub make_infobox_parts { + my ($info) = @_; # The infobox is of the form # "Time - date - focal length, shutter time, aperture, sensitivity, exposure bias - flash", @@ -894,57 +820,7 @@ sub make_infobox { } } - return 0 if (scalar @parts == 0); - - # Find the required width - my $th = 0; - my $tw = 0; - - for my $part (@parts) { - my $font; - if ($part->[1]) { - $font = '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf'; - } else { - $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf'; - } - - my (undef, undef, $h, undef, $w) = ($img->QueryFontMetrics(text=>$part->[0], font=>$font, pointsize=>12*$dpr)); - - $tw += $w; - $th = $h if ($h > $th); - } - - return 0 if ($tw > $img->Get('columns')); - - my $x = 0; - my $y = $img->Get('rows') - 24*$dpr; - - # Hit exact DCT blocks - $y -= ($y % 8); - - my $points = sprintf "%u,%u %u,%u", $x, $y, ($img->Get('columns') - 1), ($img->Get('rows') - 1); - my $lpoints = sprintf "%u,%u %u,%u", $x, $y, ($img->Get('columns') - 1), $y; - $img->Draw(primitive=>'rectangle', stroke=>'white', fill=>'white', points=>$points); - $img->Draw(primitive=>'line', stroke=>'black', strokewidth=>$dpr, points=>$lpoints); - - # Start writing out the text - $x = ($img->Get('columns') - $tw) / 2; - - my $room = ($img->Get('rows') - $dpr - $y - $th); - $y = ($img->Get('rows') - $dpr) - $room/2; - - for my $part (@parts) { - my $font; - if ($part->[1]) { - $font = '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf'; - } else { - $font = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf'; - } - $img->Annotate(text=>$part->[0], font=>$font, pointsize=>12*$dpr, x=>int($x), y=>int($y)); - $x += ($img->QueryFontMetrics(text=>$part->[0], font=>$font, pointsize=>12*$dpr))[4]; - } - - return 1; + return @parts; } sub gcd { @@ -1048,10 +924,6 @@ sub get_all_cache_urls { push @ret, "/$event/$1x$2/$filename"; } elsif ($fname =~ /^$id-(\d+)-(\d+)-nobox\.jpg$/) { push @ret, "/$event/$1x$2/nobox/$filename"; - } elsif ($fname =~ /^$id--1--1-box\.png$/) { - push @ret, "/$event/box/$filename"; - } elsif ($fname =~ /^$id-(\d+)-(\d+)-box\.png$/) { - push @ret, "/$event/$1x$2/box/$filename"; } else { log_warn($r, "Couldn't find a purging URL for $fname"); } diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index 8671b57..f37e7ac 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -15,24 +15,20 @@ sub handler { # 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._()-]+)$#) { + if ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/original/?([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $3; - $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._()-]+)$#) { + $filename = $2; + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)(?:\@(\d+(?:\.\d+)?))?/([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $6; + $filename = $5; $xres = $2; $yres = $3; $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._()-]+)$#) { + } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/([a-zA-Z0-9._()-]+)$#) { $event = $1; - $filename = $3; + $filename = $2; $xres = -1; $yres = -1; - $infobox = 1 if (defined($2) && $2 eq 'box/'); } $dpr //= 1; @@ -46,8 +42,8 @@ sub handler { 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. + 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)); @@ -62,15 +58,11 @@ sub handler { # Scale if we need to do so my ($fname, $mime_type); - if ($infobox) { - ($fname, $mime_type) = Sesse::pr0n::Common::ensure_infobox_cached($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres); - } else { - 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'); - } + 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 diff --git a/perl/Sesse/pr0n/Index.pm b/perl/Sesse/pr0n/Index.pm index 1be7b9e..d72be98 100644 --- a/perl/Sesse/pr0n/Index.pm +++ b/perl/Sesse/pr0n/Index.pm @@ -161,7 +161,51 @@ sub handler { # Find all images related to this event. my $limit = (defined($start) && defined($num) && !$settings{'fullscreen'}) ? (" LIMIT $num OFFSET " . ($start-1)) : ""; - my $q = $dbh->prepare("SELECT *, (date - INTERVAL '6 hours')::date AS day FROM images WHERE vhost=? $where AND NOT is_render ORDER BY (date - INTERVAL '6 hours')::date $datesort,takenby,date,filename $limit") + my $extra_joins = ""; + my $extra_fields = ""; + if ($settings{'fullscreen'}) { + $extra_joins = <<"EOF"; + LEFT JOIN exif_info exif_prog ON images.id=exif_prog.image AND exif_prog.key = 'ExposureProgram' + LEFT JOIN exif_info exif_focal ON images.id=exif_focal.image AND exif_focal.key = 'FocalLength' + LEFT JOIN exif_info exif_shutter ON images.id=exif_shutter.image AND exif_shutter.key = 'ExposureTime' + LEFT JOIN exif_info exif_fnum ON images.id=exif_fnum.image AND exif_fnum.key = 'FNumber' + LEFT JOIN exif_info exif_iso1 ON images.id=exif_iso1.image AND exif_iso1.key = 'ISO' + LEFT JOIN exif_info exif_iso2 ON images.id=exif_iso2.image AND exif_iso2.key = 'ISOSetting' + LEFT JOIN exif_info exif_ev1 ON images.id=exif_ev1.image AND exif_ev1.key = 'ExposureBiasValue' + LEFT JOIN exif_info exif_ev2 ON images.id=exif_ev2.image AND exif_ev2.key = 'ExposureCompensation' + LEFT JOIN exif_info exif_date ON images.id=exif_date.image AND exif_date.key = 'DateTimeOriginal' + LEFT JOIN exif_info exif_model ON images.id=exif_model.image AND exif_model.key = 'Model' + LEFT JOIN exif_info exif_flash ON images.id=exif_flash.image AND exif_flash.key = 'Flash' +EOF + $extra_fields = <<"EOF"; + exif_prog.value AS "ExposureProgram", + exif_focal.value AS "FocalLength", + exif_shutter.value AS "ExposureTime", + exif_fnum.value AS "FNumber", + exif_iso1.value AS "ISO", + exif_iso2.value AS "ISOSetting", + exif_ev1.value AS "ExposureBiasValue", + exif_ev2.value AS "ExposureCompensation", + exif_date.value AS "DateTimeOriginal", + exif_model.value AS "Model", + exif_flash.value AS "Flash", +EOF + } + + my $q = $dbh->prepare(<<"EOF") +SELECT *, + $extra_fields + (date - INTERVAL '6 hours')::date AS day +FROM + images + $extra_joins +WHERE + vhost=? + $where + AND NOT is_render +ORDER BY (date - INTERVAL '6 hours')::date $datesort,takenby,date,filename +$limit +EOF or return dberror($r, "prepare()"); $q->execute(Sesse::pr0n::Common::get_server_name($r)) or return dberror($r, "image enumeration"); @@ -178,11 +222,13 @@ sub handler { while (my $ref = $q->fetchrow_hashref()) { my $width = defined($ref->{'width'}) ? $ref->{'width'} : -1; my $height = defined($ref->{'height'}) ? $ref->{'height'} : -1; - push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height ]; + my @parts = Sesse::pr0n::Common::make_infobox_parts($ref); + @parts = map { $_->[1] ? "" . HTML::Entities::encode_entities($_->[0]) . "" : HTML::Entities::encode_entities($_->[0]) } @parts; + push @files, [ $ref->{'event'}, $ref->{'filename'}, $width, $height, join('', @parts) ]; } for my $i (0..$#files) { - my $line = sprintf " [ \"%s\", \"%s\", %d, %d ]", @{$files[$i]}; + my $line = sprintf " [ \"%s\", \"%s\", %d, %d, \"%s\" ]", @{$files[$i]}; $line .= "," unless ($i == $#files); $io->print($line . "\n"); } diff --git a/perl/Sesse/pr0n/pr0n.pm b/perl/Sesse/pr0n/pr0n.pm index 3c9161b..2517b84 100644 --- a/perl/Sesse/pr0n/pr0n.pm +++ b/perl/Sesse/pr0n/pr0n.pm @@ -71,7 +71,6 @@ sub handler { return Sesse::pr0n::Index::handler($r); } elsif ($uri =~ m#^/[a-zA-Z0-9-]+/ (\d+x\d+ ( \@\d+(\.\d+)? )? / | original/ )? - ((?:no)?box/)? [a-zA-Z0-9._()-]+$#x) { return Sesse::pr0n::Image::handler($r); } diff --git a/perl/update-image-cache.pl b/perl/update-image-cache.pl index 0ed6931..f50d3a0 100755 --- a/perl/update-image-cache.pl +++ b/perl/update-image-cache.pl @@ -103,7 +103,6 @@ while (my $ref = $q->fetchrow_hashref) { if (!$regen_mipmaps) { @files = grep { !/mipmap/ } @files; } - my @boxres = (); my @noboxres = (); my $any_old = 0; for my $c (@files) { @@ -113,8 +112,6 @@ while (my $ref = $q->fetchrow_hashref) { } 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]; } } next unless $any_old; @@ -122,9 +119,6 @@ while (my $ref = $q->fetchrow_hashref) { if (scalar @noboxres > 0) { Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, sort_res(@noboxres)); } - if (scalar @boxres > 0) { - 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 %a = map { $_ => 1 } @files; diff --git a/sql/pr0n.sql b/sql/pr0n.sql index 371e05c..d3541ed 100644 --- a/sql/pr0n.sql +++ b/sql/pr0n.sql @@ -71,7 +71,8 @@ CREATE TABLE users ( ); -- Mainly used for manual queries -- usually too slow to be very useful --- for web views in the long run. +-- for web views in the long run (except for the special case of the +-- partial index below). CREATE TABLE exif_info ( image integer NOT NULL REFERENCES images (id) ON DELETE CASCADE, key varchar NOT NULL, @@ -81,6 +82,9 @@ CREATE TABLE exif_info ( ); CLUSTER exif_info_pkey ON exif_info; +CREATE UNIQUE INDEX exif_info_fast_listing ON exif_info (image, key) INCLUDE (value) + WHERE key IN ('ExposureProgram', 'FocalLength', 'ExposureTime', 'FNumber', 'ISO', 'ISOSetting', + 'ExposureBiasValue', 'ExposureCompensation', 'DateTimeOriginal', 'Model', 'Flash'); GRANT INSERT ON TABLE deleted_images TO pr0n; GRANT INSERT,SELECT,UPDATE,DELETE ON TABLE events TO pr0n; -- 2.39.2 From 6de4bf1f755b62791d9721ee3a9cfcaed53e4571 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 18 Mar 2023 16:28:23 +0100 Subject: [PATCH 11/16] Only show the infobox if there is room for all the text. --- files/pr0n-fullscreen.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index b431a3c..75ef1f8 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -275,8 +275,14 @@ function position_image(img, backend_width, backend_height, offset, box) if (box) { img.style.top = Math.min(top + height, screen_size[1] - 24) / dpr + "px"; - img.style.width = (width / dpr) + "px"; img.style.height = "24px"; + img.style.width = null; + img.style.whiteSpace = 'nowrap'; + if (offset == 0) { + // Hide the box if there's no room for all the text. + img.style.opacity = (img.clientWidth < width / dpr + 10) ? null : 0.0; + } + img.style.width = (width / dpr) + "px"; } else { img.style.top = (top / dpr) + "px"; img.style.lineHeight = (height / dpr) + "px"; -- 2.39.2 From 578882ecd71341f35e8f938442ceda1bdf74afd2 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 18 Mar 2023 18:13:52 +0100 Subject: [PATCH 12/16] Hide the infobox immediately when going to immersive mode. --- files/pr0n-fullscreen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index 75ef1f8..7f5aed6 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -317,7 +317,7 @@ function update_shown_images() // inum !== global_image_num + 1) { // to_remove.push(child); //} - if (inum !== global_image_num) { + if (inum !== global_image_num || (child.className === "fsbox" && !global_infobox)) { to_remove.push(child); } } -- 2.39.2 From c3fdefc2ca0f2703e47be494ff72a7fd25f9e8f8 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 19 Mar 2023 13:12:08 +0100 Subject: [PATCH 13/16] Fix a swiping issue. A infobox that was too wide could be dragged in and then magically disappear; this would be since clientWidth would be 0 when we measured (because style.display was 'none' before positioning, and then fixed only immediately afterwards). --- files/pr0n-fullscreen.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index 7f5aed6..a2480b4 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -135,8 +135,8 @@ function display_image(url, backend_width, backend_height, elem_id, offset) // scroll offset completely off. img.style.display = 'none'; setTimeout(function() { + img.style.display = null; // Must be done before position_image(), for measurement. position_image(img, backend_width, backend_height, offset, false); - img.style.display = null; }, 1); } } @@ -164,8 +164,8 @@ function display_infobox(html, backend_width, backend_height, elem_id, offset) // scroll offset completely off. box.style.display = 'none'; setTimeout(function() { + box.style.display = null; // Must be done before position_image(), for measurement. position_image(box, backend_width, backend_height, offset, true); - box.style.display = null; }, 1); } } @@ -278,10 +278,8 @@ function position_image(img, backend_width, backend_height, offset, box) img.style.height = "24px"; img.style.width = null; img.style.whiteSpace = 'nowrap'; - if (offset == 0) { - // Hide the box if there's no room for all the text. - img.style.opacity = (img.clientWidth < width / dpr + 10) ? null : 0.0; - } + // Hide the box if there's no room for all the text. + img.style.opacity = (img.clientWidth < width / dpr + 10) ? null : 0.0; img.style.width = (width / dpr) + "px"; } else { img.style.top = (top / dpr) + "px"; -- 2.39.2 From 78229c85e0965be553209569da657ffb0a35366e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 19 Mar 2023 21:35:10 +0100 Subject: [PATCH 14/16] Fix an extremely long-standing error with mkdir() racing from multiple threads. --- perl/Sesse/pr0n/Common.pm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 2799364..101cee6 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -226,13 +226,15 @@ sub ensure_disk_location_exists { my $img_dir = $Sesse::pr0n::Config::image_base . "/images/$dir/"; if (! -d $img_dir) { log_info($r, "Need to create new image directory $img_dir"); - mkdir($img_dir) or die "Couldn't create new image directory $img_dir"; + mkdir($img_dir); # Ignore errors, there could be a race. + -d $img_dir or die "Couldn't create new image directory $img_dir"; } my $cache_dir = $Sesse::pr0n::Config::image_base . "/cache/$dir/"; if (! -d $cache_dir) { log_info($r, "Need to create new cache directory $cache_dir"); - mkdir($cache_dir) or die "Couldn't create new image directory $cache_dir"; + mkdir($cache_dir); # Ignore errors, there could be a race. + -d $cache_dir or die "Couldn't create new cache directory $cache_dir"; } } -- 2.39.2 From 7f954ef5ec86c9ab790a89a9c153190bedfea54c Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 20 Mar 2023 00:04:18 +0100 Subject: [PATCH 15/16] Fix infobox placement in edge cases on high-DPI screens. --- files/pr0n-fullscreen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index a2480b4..ecabed0 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -274,7 +274,7 @@ function position_image(img, backend_width, backend_height, offset, box) img.style.transform = "translate(" + extra_x_offset + "px,0px)"; if (box) { - img.style.top = Math.min(top + height, screen_size[1] - 24) / dpr + "px"; + img.style.top = Math.min(top + height, screen_size[1] - 24 * dpr) / dpr + "px"; img.style.height = "24px"; img.style.width = null; img.style.whiteSpace = 'nowrap'; -- 2.39.2 From dd3b387113e206600d881da65692a61dcccea869 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 20 Mar 2023 00:33:07 +0100 Subject: [PATCH 16/16] Fix an (irrelevant) confusion about addEventListener. --- files/pr0n-fullscreen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/pr0n-fullscreen.js b/files/pr0n-fullscreen.js index ecabed0..f08605c 100644 --- a/files/pr0n-fullscreen.js +++ b/files/pr0n-fullscreen.js @@ -128,7 +128,7 @@ function display_image(url, backend_width, backend_height, elem_id, offset) if (main_img === null || main_img.complete) { img.src = url; } else { - main_img.addEventListener('load', function() { img.src = url; }, false); + main_img.addEventListener('load', function() { img.src = url; }, { 'once': true }); } // Seemingly one needs to delay position_image(), or Firefox will set the initial -- 2.39.2