Skip to content

Commit

Permalink
Bump maennchen/zipstream-php version (#3246)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Apr 20, 2023
1 parent 1e1ebca commit 034260c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 10 additions & 4 deletions docs/downloading-media/downloading-multiple-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Support/MediaStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 034260c

Please sign in to comment.