Skip to content

Commit

Permalink
Refactored how download file name is determined within MediaStream zi…
Browse files Browse the repository at this point in the history
…p file (#3541)
  • Loading branch information
chrispage1 authored Feb 16, 2024
1 parent 31edff5 commit 0e21049
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Support/MediaStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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++;
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/MediaStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions tests/TestSupport/TestMediaModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\MediaLibrary\Tests\TestSupport;

use Spatie\MediaLibrary\MediaCollections\Models\Media;

class TestMediaModel extends Media
{
public function getDownloadFilename(): string
{
return 'overriden_testing.jpg';
}
}

0 comments on commit 0e21049

Please sign in to comment.