From 48a5fdc8cbe1050a72ddc073cefd6023267d7936 Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Sat, 11 May 2019 14:00:05 +0200 Subject: [PATCH] Allow passing the image data do the resizing daemon. No need to write it out to a file and read it in again. --- Slim/Utils/ImageResizer.pm | 17 ++++++++++++++--- Slim/Web/ImageProxy.pm | 14 +------------- gdresized.pl | 16 +++++++++++----- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Slim/Utils/ImageResizer.pm b/Slim/Utils/ImageResizer.pm index 849871e95d3..849ee6dab29 100644 --- a/Slim/Utils/ImageResizer.pm +++ b/Slim/Utils/ImageResizer.pm @@ -24,8 +24,12 @@ my $pending_requests = 0; my $hasDaemon; sub hasDaemon { - if (!defined $hasDaemon) { - $hasDaemon = !main::SCANNER && !main::ISWINDOWS && -r SOCKET_PATH && -w _; + my ($class, $check) = @_; + + if (!defined $hasDaemon || $check) { + $hasDaemon = (!main::SCANNER && !main::ISWINDOWS && -r SOCKET_PATH && -w _) or do { + unlink SOCKET_PATH; + }; } return $hasDaemon; @@ -102,7 +106,14 @@ sub resize { }, ); - $handle->push_write( pack('Z*Z*Z*Z*', $file, $specs, $cacheroot, $cachekey) . "\015\012" ); + # need to protect \n\r in the file from ending the sending prematurely + # this is NOT 100% safe, as it might break + if (ref $file) { + $$file =~ s/\n/\\0x12/sg; + $$file =~ s/\r/\\0x15/sg; + } + + $handle->push_write( pack('Z* Z* Z* Z* I/a*', ref $file ? 'data' : $file, $specs, $cacheroot, $cachekey, ref $file ? $$file : '') . "\015\012" ); }, sub { # prepare callback, used to set the timeout return SOCKET_TIMEOUT; diff --git a/Slim/Web/ImageProxy.pm b/Slim/Web/ImageProxy.pm index 00761c2794f..5890b48cccc 100644 --- a/Slim/Web/ImageProxy.pm +++ b/Slim/Web/ImageProxy.pm @@ -223,19 +223,7 @@ sub _gotArtwork { return _gotArtworkError($http); } - if ( Slim::Utils::ImageResizer::hasDaemon() ) { - # We don't use SimpleAsyncHTTP's saveAs feature, as this wouldn't keep a copy in the cache, which we might need - # if we wanted other sizes of the same url - my $fullpath = catdir( $prefs->get('cachedir'), 'imgproxy_' . Digest::MD5::md5_hex($url) ); - - # Unfortunately we have to write the data to a file, in case LMS was using an external image resizer (TinyLMS) - File::Slurp::write_file($fullpath, $http->contentRef); - - _resizeFromFile($http->url, $fullpath); - } - else { - _resizeFromFile($http->url, $http->contentRef, $http); - } + _resizeFromFile($http->url, $http->contentRef, $http); } sub _gotArtworkError { diff --git a/gdresized.pl b/gdresized.pl index b9ce6bf0473..e482684ce2d 100755 --- a/gdresized.pl +++ b/gdresized.pl @@ -133,14 +133,21 @@ BEGIN # get command my $buf = <$client>; - - my ($file, $spec, $cacheroot, $cachekey) = unpack 'Z*Z*Z*Z*', $buf; + my ($file, $spec, $cacheroot, $cachekey, $data) = unpack 'Z* Z* Z* Z* I/a*', $buf; + + if ($data) { + # trim cr/lf from the end + $data =~ s/\015\012$//s; + $data =~ s/\\0x12/\n/sg; + $data =~ s/\\0x15/\r/sg; + } + # An empty spec is allowed, this returns the original image $spec ||= 'XxX'; my $imageproxy = $cachekey =~ /^imageproxy/; - DEBUG && warn sprintf("file=%s, spec=%s, cacheroot=%s, cachekey=%s imageproxy=%s\n", $file, $spec, $cacheroot, $cachekey, $imageproxy || 0); + DEBUG && warn sprintf("file=%s, spec=%s, cacheroot=%s, cachekey=%s, imageproxy=%s, imagedata=%s bytes\n", $file, $spec, $cacheroot, $cachekey, $imageproxy || 0, length($data)); if ( !$file || !$spec || !$cacheroot || !$cachekey ) { die "Invalid parameters: $file, $spec, $cacheroot, $cachekey\n"; @@ -156,7 +163,7 @@ BEGIN # do resize Slim::Utils::GDResizer->gdresize( - file => $file, + file => $data ? \$data : $file, debug => DEBUG, faster => $faster, cache => $cache, @@ -188,7 +195,6 @@ sub new { my $class = shift; my $root = shift; -warn 'imageproxy'; return $class->SUPER::new($root, 'imgproxy', 86400*30); }