From 63a6053c0eebcfb45a2addd1724619eb8ebfc026 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Fri, 6 Nov 2026 15:09:35 +0100 Subject: [PATCH] fix(preview): send the whole JPEG to Imaginary Read the EXIF data through a separate stream and send the file stream unchanged. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Carl Schwan --- lib/private/Preview/Imaginary.php | 24 +++++++------ tests/lib/Preview/ImaginaryTest.php | 54 ++++++++++++++++++----------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/lib/private/Preview/Imaginary.php b/lib/private/Preview/Imaginary.php index baa010ff8a1da..5a9e832a86ba2 100644 --- a/lib/private/Preview/Imaginary.php +++ b/lib/private/Preview/Imaginary.php @@ -7,8 +7,6 @@ namespace OC\Preview; -use GuzzleHttp\Psr7\AppendStream; -use GuzzleHttp\Psr7\Utils; use OC\StreamImage; use OCP\Files\File; use OCP\Http\Client\IClientService; @@ -108,15 +106,9 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop default: } - $body = $stream; if ($autorotate && $file->getMimeType() === 'image/jpeg') { // Each pipeline operation re-encodes the full image, only rotate when needed - $head = fread($stream, self::EXIF_HEAD_SIZE); - if ($head === false) { - return null; - } - $autorotate = $this->needsRotation($head); - $body = new AppendStream([Utils::streamFor($head), Utils::streamFor($stream)]); + $autorotate = $this->needsRotation($file); } $operations = []; @@ -166,7 +158,7 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop 'headers' => [ 'Content-Type' => $file->getMimeType(), ], - 'body' => $body, + 'body' => $stream, 'nextcloud' => ['allow_local_address' => true], 'timeout' => 120, 'connect_timeout' => 3, @@ -201,10 +193,20 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop return $image->valid() ? $image : null; } - private function needsRotation(string $head): bool { + private function needsRotation(File $file): bool { if (!is_callable('exif_read_data')) { return true; } + // Read separately, the stream sent to Imaginary has to start at the beginning of the file + $stream = $file->fopen('r'); + if (!is_resource($stream)) { + return true; + } + $head = fread($stream, self::EXIF_HEAD_SIZE); + fclose($stream); + if ($head === false) { + return true; + } $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($head), 'IFD0'); return $exif !== false && isset($exif['Orientation']) && (int)$exif['Orientation'] !== 1; } diff --git a/tests/lib/Preview/ImaginaryTest.php b/tests/lib/Preview/ImaginaryTest.php index 484f02ecc7555..81d15c928c318 100644 --- a/tests/lib/Preview/ImaginaryTest.php +++ b/tests/lib/Preview/ImaginaryTest.php @@ -38,34 +38,46 @@ protected function tearDown(): void { parent::tearDown(); } - private function getJpeg(?int $orientation): string { - $image = imagecreatetruecolor(40, 30); + private function getJpeg(?int $orientation, bool $large = false): string { + $image = imagecreatetruecolor($large ? 600 : 40, $large ? 600 : 30); + if ($large) { + // Noise does not compress, the image data gets larger than the part read for the orientation + for ($x = 0; $x < 600; $x++) { + for ($y = 0; $y < 600; $y++) { + imagesetpixel($image, $x, $y, mt_rand(0, 0xffffff)); + } + } + } ob_start(); - imagejpeg($image); + imagejpeg($image, null, 100); $jpeg = ob_get_clean(); - if ($orientation === null) { - return $jpeg; - } - // EXIF segment with only the orientation - $tiff = "II*\0" . pack('V', 8) . pack('v', 1) . pack('vvVvv', 0x0112, 3, 1, $orientation, 0) . pack('V', 0); - $app1 = "\xFF\xE1" . pack('n', 2 + 6 + strlen($tiff)) . "Exif\0\0" . $tiff; - return substr($jpeg, 0, 2) . $app1 . substr($jpeg, 2); + $segments = ''; + if ($orientation !== null) { + // EXIF segment with only the orientation + $tiff = "II*\0" . pack('V', 8) . pack('v', 1) . pack('vvVvv', 0x0112, 3, 1, $orientation, 0) . pack('V', 0); + $segments .= "\xFF\xE1" . pack('n', 2 + 6 + strlen($tiff)) . "Exif\0\0" . $tiff; + } + return substr($jpeg, 0, 2) . $segments . substr($jpeg, 2); } - #[TestWith([null, false])] - #[TestWith([1, false])] - #[TestWith([6, true])] - public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAutorotate): void { - $jpeg = $this->getJpeg($orientation); - $stream = fopen('php://memory', 'r+'); - fwrite($stream, $jpeg); - rewind($stream); + #[TestWith([null, false, false])] + #[TestWith([1, false, false])] + #[TestWith([6, true, false])] + #[TestWith([null, false, true])] + #[TestWith([6, true, true])] + public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAutorotate, bool $large): void { + $jpeg = $this->getJpeg($orientation, $large); $file = $this->createMock(File::class); $file->method('getSize')->willReturn(strlen($jpeg)); $file->method('getMimeType')->willReturn('image/jpeg'); - $file->method('fopen')->willReturn($stream); + $file->method('fopen')->willReturnCallback(function () use ($jpeg) { + $stream = fopen('php://memory', 'r+'); + fwrite($stream, $jpeg); + rewind($stream); + return $stream; + }); $response = $this->createMock(IResponse::class); $response->method('getStatusCode')->willReturn(200); @@ -81,9 +93,9 @@ public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAuto $operations = array_column(json_decode($options['query']['operations'], true), 'operation'); $this->assertSame($expectAutorotate ? ['autorotate', 'fit'] : ['fit'], $operations); - // The whole file is sent, including the head read for the orientation + // Read from the start, like the HTTP client does $body = $options['body']; - $this->assertSame($jpeg, $body instanceof StreamInterface ? $body->getContents() : stream_get_contents($body)); + $this->assertSame($jpeg, $body instanceof StreamInterface ? (string)$body : stream_get_contents($body, -1, 0)); return $response; });