From 034260c6b383bdbca7df1f18b06898036a5273f1 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Thu, 20 Apr 2023 01:49:40 -0500 Subject: [PATCH] Bump maennchen/zipstream-php version (#3246) --- composer.json | 2 +- .../downloading-multiple-files.md | 14 ++++++++++---- src/Support/MediaStream.php | 11 ++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index a2798a0bd..6da76bcd2 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "illuminate/pipeline": "^9.18|^10.0", "illuminate/support": "^9.18|^10.0", "intervention/image": "^2.7", - "maennchen/zipstream-php": "^2.0", + "maennchen/zipstream-php": "^2.0|^3.0", "spatie/image": "^2.2.2", "spatie/temporary-directory": "^2.0", "symfony/console": "^6.0" diff --git a/docs/downloading-media/downloading-multiple-files.md b/docs/downloading-media/downloading-multiple-files.md index 3ab23b208..351d575d2 100644 --- a/docs/downloading-media/downloading-multiple-files.md +++ b/docs/downloading-media/downloading-multiple-files.md @@ -30,13 +30,12 @@ class DownloadMediaController You can also pass any custom options to the `ZipStream` instance using the `useZipOptions` method. -All the available options are listed on the [ZipStream-PHP wiki](https://github.com/maennchen/ZipStream-PHP/wiki/Available-options). +All the available options are listed on the [ZipStream-PHP guide](https://maennchen.dev/ZipStream-PHP/guide/Options.html). Here's an example on how it can be used: ```php use Spatie\MediaLibrary\Support\MediaStream; -use ZipStream\Option\Archive as ArchiveOptions; class DownloadMediaController { @@ -48,8 +47,15 @@ class DownloadMediaController // Download the files associated with the media in a streamed way. // No prob if your files are very large. return MediaStream::create('my-files.zip') - ->useZipOptions(function(ArchiveOptions $zipOptions) { - $zipOptions->setZeroHeader(true); + ->useZipOptions(function(&$zipOptions) { + if (is_array($zipOptions)) { + // ZipStream ^3.0 uses array + $zipOptions['defaultEnableZeroHeader'] = true; + } else { + // ZipStream ^2.0 uses \ZipStream\Option\Archive + /** @var \ZipStream\Option\Archive $zipOptions */ + $zipOptions->setZeroHeader(true); + } }) ->addMedia($downloads); } diff --git a/src/Support/MediaStream.php b/src/Support/MediaStream.php index 604d0d922..0d0f54cad 100644 --- a/src/Support/MediaStream.php +++ b/src/Support/MediaStream.php @@ -13,7 +13,7 @@ class MediaStream implements Responsable { protected Collection $mediaItems; - protected ArchiveOptions $zipOptions; + protected array|ArchiveOptions $zipOptions; public static function create(string $zipName): self { @@ -24,7 +24,7 @@ public function __construct(protected string $zipName) { $this->mediaItems = collect(); - $this->zipOptions = new ArchiveOptions(); + $this->zipOptions = class_exists(ArchiveOptions::class) ? new ArchiveOptions() : []; } public function useZipOptions(callable $zipOptionsCallable): self @@ -74,7 +74,12 @@ public function toResponse($request): StreamedResponse public function getZipStream(): ZipStream { - $zip = new ZipStream($this->zipName, $this->zipOptions); + if (class_exists(ArchiveOptions::class)) { + $zip = new ZipStream($this->zipName, $this->zipOptions); + } else { + $this->zipOptions['outputName'] = $this->zipName; + $zip = new ZipStream(...$this->zipOptions); + } $this->getZipStreamContents()->each(function (array $mediaInZip) use ($zip) { $stream = $mediaInZip['media']->stream();