From 0c2dce3f63bbcce76d2cf5a0e66ec7b4e0b7bef7 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 8 Feb 2025 11:53:40 -0600 Subject: [PATCH] Handle TIFF files with multiple pages --- lib/Artwork.php | 6 +++--- lib/Feed.php | 2 +- lib/Image.php | 34 +++++++++++++++++++++++++-------- scripts/generate-bulk-downloads | 4 ++-- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/Artwork.php b/lib/Artwork.php index ce007d9b..9e49fd0f 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -865,21 +865,21 @@ class Artwork{ ', [$this->ArtworkId]); try{ - unlink($this->ImageFsPath); + @unlink($this->ImageFsPath); } catch(\Safe\Exceptions\FilesystemException){ // Pass. } try{ - unlink($this->ThumbFsPath); + @unlink($this->ThumbFsPath); } catch(\Safe\Exceptions\FilesystemException){ // Pass. } try{ - unlink($this->Thumb2xFsPath); + @unlink($this->Thumb2xFsPath); } catch(\Safe\Exceptions\FilesystemException){ // Pass. diff --git a/lib/Feed.php b/lib/Feed.php index 953fe07f..fad72e22 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -50,7 +50,7 @@ abstract class Feed{ file_put_contents($tempFilename, $xmlString); exec('se clean ' . escapeshellarg($tempFilename) . ' 2>&1', $output); // Capture the result in case there's an error, otherwise it prints to stdout $output = file_get_contents($tempFilename); - unlink($tempFilename); + @unlink($tempFilename); // At the moment, `se clean` strips stylesheet declarations. Restore them here. if($this->Stylesheet !== null){ diff --git a/lib/Image.php b/lib/Image.php index 67c2f05b..d50d3230 100644 --- a/lib/Image.php +++ b/lib/Image.php @@ -1,5 +1,6 @@ Path)['filename'] . '.jpg'; + $basename = pathinfo($this->Path)['filename']; + $tempDirectory = sys_get_temp_dir(); + $tempFilename = $tempDirectory . '/se-' . $basename . '.jpg'; try{ exec('convert '. escapeshellarg($this->Path) . ' ' . escapeshellarg($tempFilename), $shellOutput, $resultCode); - if($resultCode !== 0 || !is_file($tempFilename)){ + if($resultCode !== 0){ throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG'); } - $handle = \Safe\imagecreatefromjpeg($tempFilename); + // Sometimes TIFF files can have multiple images, or "pages" in one file. In that case, `convert` outputs multiple files named `-0.jpg`, `-1.jpg`, etc., instead of `.jpg`. + // Test for that case here. + $pagedFilename = $tempDirectory . '/se-' . $basename . '-0.jpg'; + if(is_file($pagedFilename)){ + // This TIFF has pages! + $handle = \Safe\imagecreatefromjpeg($pagedFilename); + } + elseif(is_file($tempFilename)){ + // Regular TIFF. + $handle = \Safe\imagecreatefromjpeg($tempFilename); + } + else{ + throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG'); + } } finally{ - try{ - unlink($tempFilename); - } - catch(Exception){ - // Pass if file doesn't exist. + foreach(glob($tempDirectory . '/se-' . $basename . '*.jpg') as $filename){ + try{ + @unlink($filename); + } + catch(Exception){ + // Pass. + } } } diff --git a/scripts/generate-bulk-downloads b/scripts/generate-bulk-downloads index 813cf663..f321580a 100755 --- a/scripts/generate-bulk-downloads +++ b/scripts/generate-bulk-downloads @@ -35,7 +35,7 @@ function rrmdir(string $src): void{ rrmdir($full); } else{ - unlink($full); + @unlink($full); } } } @@ -99,7 +99,7 @@ function CreateZip(string $filePath, array $ebooks, string $type, string $webRoo // We have to do a copy, then unlink because `rename()` can't rename across file systems. // If the bulk downloads are symlinked to a storage volume, then `rename()` won't work. copy($tempFilename, $filePath); - unlink($tempFilename); + @unlink($tempFilename); exec('attr -q -s se-ebook-type -V ' . escapeshellarg($type) . ' ' . escapeshellarg($filePath)); }