Skip to content

Commit 0e21049

Browse files
authored
Refactored how download file name is determined within MediaStream zip file (#3541)
1 parent 31edff5 commit 0e21049

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Support/MediaStream.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function getZipStream(): ZipStream
9393

9494
protected function getZipStreamContents(): Collection
9595
{
96+
9697
return $this->mediaItems->map(fn (Media $media, $mediaItemIndex) => [
9798
'fileNameInZip' => $this->getZipFileNamePrefix($this->mediaItems, $mediaItemIndex).$this->getFileNameWithSuffix($this->mediaItems, $mediaItemIndex),
9899
'media' => $media,
@@ -103,14 +104,14 @@ protected function getFileNameWithSuffix(Collection $mediaItems, int $currentInd
103104
{
104105
$fileNameCount = 0;
105106

106-
$fileName = $mediaItems[$currentIndex]->file_name;
107+
$fileName = $mediaItems[$currentIndex]->getDownloadFilename();
107108

108109
foreach ($mediaItems as $index => $media) {
109110
if ($index >= $currentIndex) {
110111
break;
111112
}
112113

113-
if ($this->getZipFileNamePrefix($mediaItems, $index).$media->file_name === $this->getZipFileNamePrefix($mediaItems, $currentIndex).$fileName) {
114+
if ($this->getZipFileNamePrefix($mediaItems, $index).$media->getDownloadFilename() === $this->getZipFileNamePrefix($mediaItems, $currentIndex).$fileName) {
114115
$fileNameCount++;
115116
}
116117
}

tests/Support/MediaStreamTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Route;
44
use Spatie\MediaLibrary\MediaCollections\Models\Media;
55
use Spatie\MediaLibrary\Support\MediaStream;
6+
use Spatie\MediaLibrary\Tests\TestSupport\TestMediaModel;
67
use Spatie\TemporaryDirectory\TemporaryDirectory;
78
use Symfony\Component\HttpFoundation\StreamedResponse;
89

@@ -44,6 +45,25 @@
4445
$this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'test (2).jpg');
4546
});
4647

48+
it('will respect the filename set by getDownloadFilename method', function () {
49+
$zipStreamResponse = MediaStream::create('my-media.zip')
50+
->addMedia(Media::find(1))
51+
->addMedia(TestMediaModel::find(2))
52+
->addMedia(TestMediaModel::find(2));
53+
54+
ob_start();
55+
@$zipStreamResponse->toResponse(request())->sendContent();
56+
$content = ob_get_contents();
57+
ob_end_clean();
58+
59+
$temporaryDirectory = (new TemporaryDirectory())->create();
60+
file_put_contents($temporaryDirectory->path('response.zip'), $content);
61+
62+
$this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'test.jpg');
63+
$this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'overriden_testing.jpg');
64+
$this->assertFileExistsInZip($temporaryDirectory->path('response.zip'), 'overriden_testing (1).jpg');
65+
});
66+
4767
test('media can be added to it one by one', function () {
4868
$zipStreamResponse = MediaStream::create('my-media.zip')
4969
->addMedia(Media::find(1))

tests/TestSupport/TestMediaModel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\MediaLibrary\Tests\TestSupport;
4+
5+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
6+
7+
class TestMediaModel extends Media
8+
{
9+
public function getDownloadFilename(): string
10+
{
11+
return 'overriden_testing.jpg';
12+
}
13+
}

0 commit comments

Comments
 (0)