Skip to content

Commit

Permalink
Allow passing the image data do the resizing daemon. No need to write…
Browse files Browse the repository at this point in the history
… it out to a file and read it in again.
  • Loading branch information
mherger committed May 11, 2019
1 parent 280dd1f commit 48a5fdc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
17 changes: 14 additions & 3 deletions Slim/Utils/ImageResizer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions Slim/Web/ImageProxy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 11 additions & 5 deletions gdresized.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -156,7 +163,7 @@ BEGIN

# do resize
Slim::Utils::GDResizer->gdresize(
file => $file,
file => $data ? \$data : $file,
debug => DEBUG,
faster => $faster,
cache => $cache,
Expand Down Expand Up @@ -188,7 +195,6 @@ sub new {
my $class = shift;
my $root = shift;

warn 'imageproxy';
return $class->SUPER::new($root, 'imgproxy', 86400*30);
}

Expand Down

0 comments on commit 48a5fdc

Please sign in to comment.