diff --git a/lib/Ansel.php b/lib/Ansel.php index 161bd5fa..c87ab456 100644 --- a/lib/Ansel.php +++ b/lib/Ansel.php @@ -1,5 +1,7 @@ $image->filename); } - $zip = Horde_Compress::factory('zip'); - $body = $zip->compress($zipfiles); + $zip = (new CompressFactory())->create('zip'); + $body = $zip->compressFiles($zipfiles); if (!empty($gallery)) { $filename = (!empty($slug) ? $slug : $gallery->id) . '.zip'; } else { diff --git a/lib/View/Upload.php b/lib/View/Upload.php index e414cd80..6213e13c 100644 --- a/lib/View/Upload.php +++ b/lib/View/Upload.php @@ -1,5 +1,9 @@ decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST)); - } catch (Horde_Compress_Exception $e) { + $zip = (new CompressFactory())->create('zip'); + $files = $zip->decompress($data, array('action' => CompressZip::ZIP_LIST)); + } catch (CompressException $e) { throw new Ansel_Exception($e); } @@ -547,14 +551,15 @@ private function _handleZip($filename) /* Extract the image */ try { - $zdata = $zip->decompress( + $result = $zip->decompress( $data, array( - 'action' => Horde_Compress_Zip::ZIP_DATA, + 'action' => CompressZip::ZIP_DATA, 'info' => $files, 'key' => $key) ); - } catch (Horde_Compress_Exception $e) { + $zdata = is_array($result) ? $result['data'] : $result; + } catch (CompressException $e) { throw new Ansel_Exception($e); } diff --git a/test/Ansel/AnselCompressTest.php b/test/Ansel/AnselCompressTest.php new file mode 100644 index 00000000..a2a53385 --- /dev/null +++ b/test/Ansel/AnselCompressTest.php @@ -0,0 +1,128 @@ +create('zip'); + + $files = [ + ['data' => 'image-binary-data-1', 'name' => 'photo1.jpg'], + ['data' => 'image-binary-data-2', 'name' => 'photo2.jpg'], + ]; + + $body = $zip->compressFiles($files); + + $this->assertIsString($body); + $this->assertNotEmpty($body); + $this->assertGreaterThan(0, strlen($body)); + } + + /** + * View/Upload.php pattern: decompress ZIP_LIST returns array of entries + * with 'name' key that the caller uses to filter meta files. + */ + public function testDecompressListReturnsEntriesWithNames(): void + { + $zip = (new CompressFactory())->create('zip'); + + $files = [ + ['data' => 'img-a', 'name' => 'gallery/photo_a.png'], + ['data' => 'img-b', 'name' => 'gallery/photo_b.png'], + ['data' => 'meta', 'name' => '__MACOSX/.DS_Store'], + ]; + + $archive = $zip->compressFiles($files); + $listing = $zip->decompress($archive, [ + 'action' => Zip::ZIP_LIST, + ]); + + $this->assertIsArray($listing); + $this->assertCount(3, $listing); + $this->assertEquals('gallery/photo_a.png', $listing[0]['name']); + $this->assertEquals('gallery/photo_b.png', $listing[1]['name']); + $this->assertEquals('__MACOSX/.DS_Store', $listing[2]['name']); + } + + /** + * View/Upload.php pattern: decompress ZIP_DATA returns file content. + * The PSR-4 API returns ['data' => string], so the caller unwraps it. + * The addImage() call receives a raw string either way. + */ + public function testDecompressDataReturnsContent(): void + { + $zip = (new CompressFactory())->create('zip'); + + $originalContent = random_bytes(256); + $files = [ + ['data' => $originalContent, 'name' => 'test_image.jpg'], + ]; + + $archive = $zip->compressFiles($files); + $listing = $zip->decompress($archive, [ + 'action' => Zip::ZIP_LIST, + ]); + + $result = $zip->decompress($archive, [ + 'action' => Zip::ZIP_DATA, + 'info' => $listing, + 'key' => 0, + ]); + + // PSR-4 returns ['data' => string]; caller unwraps with is_array check. + $zdata = is_array($result) ? $result['data'] : $result; + $this->assertIsString($zdata); + $this->assertEquals($originalContent, $zdata); + } + + /** + * Round-trip: compress multiple files then extract each one individually. + * Proves the full cycle matches what Ansel does in Upload.php. + */ + public function testRoundTripMultipleFiles(): void + { + $zip = (new CompressFactory())->create('zip'); + + $inputFiles = [ + ['data' => 'content-alpha', 'name' => 'alpha.dat'], + ['data' => 'content-beta', 'name' => 'beta.dat'], + ['data' => 'content-gamma', 'name' => 'gamma.dat'], + ]; + + $archive = $zip->compressFiles($inputFiles); + $listing = $zip->decompress($archive, [ + 'action' => Zip::ZIP_LIST, + ]); + + $this->assertCount(3, $listing); + + foreach ($inputFiles as $key => $input) { + $result = $zip->decompress($archive, [ + 'action' => Zip::ZIP_DATA, + 'info' => $listing, + 'key' => $key, + ]); + $extracted = is_array($result) ? $result['data'] : $result; + $this->assertEquals($input['data'], $extracted, "File at key $key mismatch"); + } + } +}