From 0e21049d2a267d7ab85fe2bcdefebdb0e60cd33d Mon Sep 17 00:00:00 2001 From: Chris Page Date: Fri, 16 Feb 2024 10:28:07 +0000 Subject: [PATCH] Refactored how download file name is determined within MediaStream zip file (#3541) --- src/Support/MediaStream.php | 5 +++-- tests/Support/MediaStreamTest.php | 20 ++++++++++++++++++++ tests/TestSupport/TestMediaModel.php | 13 +++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/TestSupport/TestMediaModel.php diff --git a/src/Support/MediaStream.php b/src/Support/MediaStream.php index 2e7be6f42..eb0bd2f50 100644 --- a/src/Support/MediaStream.php +++ b/src/Support/MediaStream.php @@ -93,6 +93,7 @@ public function getZipStream(): ZipStream protected function getZipStreamContents(): Collection { + return $this->mediaItems->map(fn (Media $media, $mediaItemIndex) => [ 'fileNameInZip' => $this->getZipFileNamePrefix($this->mediaItems, $mediaItemIndex).$this->getFileNameWithSuffix($this->mediaItems, $mediaItemIndex), 'media' => $media, @@ -103,14 +104,14 @@ protected function getFileNameWithSuffix(Collection $mediaItems, int $currentInd { $fileNameCount = 0; - $fileName = $mediaItems[$currentIndex]->file_name; + $fileName = $mediaItems[$currentIndex]->getDownloadFilename(); foreach ($mediaItems as $index => $media) { if ($index >= $currentIndex) { break; } - if ($this->getZipFileNamePrefix($mediaItems, $index).$media->file_name === $this->getZipFileNamePrefix($mediaItems, $currentIndex).$fileName) { + if ($this->getZipFileNamePrefix($mediaItems, $index).$media->getDownloadFilename() === $this->getZipFileNamePrefix($mediaItems, $currentIndex).$fileName) { $fileNameCount++; } } diff --git a/tests/Support/MediaStreamTest.php b/tests/Support/MediaStreamTest.php index 0464160f7..fadb3fac8 100644 --- a/tests/Support/MediaStreamTest.php +++ b/tests/Support/MediaStreamTest.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Route; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\Support\MediaStream; +use Spatie\MediaLibrary\Tests\TestSupport\TestMediaModel; use Spatie\TemporaryDirectory\TemporaryDirectory; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -44,6 +45,25 @@ $this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'test (2).jpg'); }); +it('will respect the filename set by getDownloadFilename method', function () { + $zipStreamResponse = MediaStream::create('my-media.zip') + ->addMedia(Media::find(1)) + ->addMedia(TestMediaModel::find(2)) + ->addMedia(TestMediaModel::find(2)); + + ob_start(); + @$zipStreamResponse->toResponse(request())->sendContent(); + $content = ob_get_contents(); + ob_end_clean(); + + $temporaryDirectory = (new TemporaryDirectory())->create(); + file_put_contents($temporaryDirectory->path('response.zip'), $content); + + $this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'test.jpg'); + $this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'overriden_testing.jpg'); + $this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'overriden_testing (1).jpg'); +}); + test('media can be added to it one by one', function () { $zipStreamResponse = MediaStream::create('my-media.zip') ->addMedia(Media::find(1)) diff --git a/tests/TestSupport/TestMediaModel.php b/tests/TestSupport/TestMediaModel.php new file mode 100644 index 000000000..f5f60cc34 --- /dev/null +++ b/tests/TestSupport/TestMediaModel.php @@ -0,0 +1,13 @@ +