generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c669e92
commit 8d2ee6b
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Finller\Media; | ||
|
||
use Finller\Media\Models\Media; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use Illuminate\Contracts\Support\Responsable; | ||
use Illuminate\Support\Collection; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use ZipStream\ZipStream; | ||
|
||
class MediaZipper implements Responsable | ||
{ | ||
/** | ||
* @param Collection<int, Media> $media | ||
*/ | ||
public function __construct( | ||
public Collection $media = new Collection(), | ||
public string $fileName = 'media.zip', | ||
public array $zipStreamOptions = [], | ||
) { | ||
$this->zipStreamOptions['outputName'] = $fileName; | ||
} | ||
|
||
public function toFile(Filesystem $storage, string $path, array $options = []): string|false | ||
{ | ||
$tempStream = fopen('php://memory', 'w+'); | ||
|
||
$zipStream = $this->getZipStream([ | ||
'outputStream' => $tempStream, | ||
]); | ||
|
||
$success = $storage->writeStream($path, $tempStream, $options); | ||
|
||
fclose($tempStream); | ||
|
||
return $success ? $path : false; | ||
} | ||
|
||
public function getZipStream(array $options = []) | ||
{ | ||
$zip = new ZipStream( | ||
...$this->zipStreamOptions, | ||
...$options, | ||
); | ||
|
||
foreach ($this->media as $item) { | ||
$stream = $item->readStream(); | ||
|
||
$zip->addFileFromStream( | ||
fileName: $item->file_name, | ||
stream: $stream | ||
); | ||
|
||
if (is_resource($stream)) { | ||
fclose($stream); | ||
} | ||
} | ||
|
||
$zip->finish(); | ||
|
||
return $zip; | ||
} | ||
|
||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function toResponse($request): StreamedResponse | ||
{ | ||
return new StreamedResponse(fn () => $this->getZipStream(), 200, [ | ||
'Content-Disposition' => "attachment; filename=\"{$this->fileName}\"", | ||
'Content-Type' => 'application/octet-stream', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Finller\Media\Database\Factories\MediaFactory; | ||
use Finller\Media\MediaZipper; | ||
use Finller\Media\Models\Media; | ||
use Illuminate\Http\UploadedFile; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
it('zip media and store it in a file', function () { | ||
$storage = Storage::fake('media'); | ||
|
||
$media = collect([ | ||
MediaFactory::new()->make(), | ||
MediaFactory::new()->make(), | ||
])->each(function (Media $media) { | ||
$media->storeFile( | ||
file: UploadedFile::fake()->image('foo.jpg'), | ||
collection_name: 'avatar', | ||
name: 'avatar', | ||
disk: 'media' | ||
); | ||
}); | ||
|
||
$zipper = new MediaZipper($media); | ||
|
||
$zipper->toFile($storage, $zipper->fileName); | ||
|
||
Storage::disk('media')->assertExists($zipper->fileName); | ||
|
||
}); |