Skip to content

Commit

Permalink
Remove ZipStream V2 support (#3473)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Dec 21, 2023
1 parent 280e20b commit 0d1547c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
12 changes: 3 additions & 9 deletions docs/downloading-media/downloading-multiple-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ 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(&$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);
}
->useZipOptions(function (&$zipOptions) {
// ZipStream ^3.0 uses array
$zipOptions['defaultEnableZeroHeader'] = true;
})
->addMedia($downloads);
}
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ parameters:

# For MediaLibraryPro dependencies
- "#MediaLibraryPro\\\\#"

# This must be removed after drop ZipStream V2
- "#ZipStream\\\\#"
13 changes: 4 additions & 9 deletions src/Support/MediaStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
use Illuminate\Support\Collection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\HttpFoundation\StreamedResponse;
use ZipStream\Option\Archive as ArchiveOptions;
use ZipStream\ZipStream;

class MediaStream implements Responsable
{
protected Collection $mediaItems;

protected array|ArchiveOptions $zipOptions;
protected array $zipOptions;

public static function create(string $zipName): self
{
Expand All @@ -24,7 +23,7 @@ public function __construct(protected string $zipName)
{
$this->mediaItems = collect();

$this->zipOptions = class_exists(ArchiveOptions::class) ? new ArchiveOptions() : [];
$this->zipOptions = [];
}

public function useZipOptions(callable $zipOptionsCallable): self
Expand Down Expand Up @@ -74,12 +73,8 @@ public function toResponse($request): StreamedResponse

public function getZipStream(): ZipStream
{
if (class_exists(ArchiveOptions::class)) {
$zip = new ZipStream($this->zipName, $this->zipOptions);
} else {
$this->zipOptions['outputName'] = $this->zipName;
$zip = new ZipStream(...$this->zipOptions);
}
$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 0d1547c

Please sign in to comment.