Skip to content

Commit

Permalink
Added callback to manipulate FileAdder when performing a copy (#3658)
Browse files Browse the repository at this point in the history
* New 'getDownloadFilename' method for extensibility
Added Symfony HeaderUtils to create content disposition

* Refactored how download file name is determined

* Added closure to allow direct customisation of FileAdder on copy

* Fix styling

---------

Co-authored-by: chrispage1 <[email protected]>
  • Loading branch information
chrispage1 and chrispage1 committed Jul 2, 2024
1 parent 038c1ba commit c3828e7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/MediaCollections/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\MediaLibrary\MediaCollections\Models;

use Closure;
use DateTimeInterface;
use Illuminate\Contracts\Mail\Attachable;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -20,6 +21,7 @@
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\MediaCollections\FileAdder;
use Spatie\MediaLibrary\MediaCollections\Filesystem;
use Spatie\MediaLibrary\MediaCollections\HtmlableMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
Expand Down Expand Up @@ -393,9 +395,16 @@ public function move(HasMedia $model, $collectionName = 'default', string $diskN
return $newMedia;
}

/** @param string $collectionName */
public function copy(HasMedia $model, $collectionName = 'default', string $diskName = '', string $fileName = ''): self
{
/**
* @param null|Closure(FileAdder): FileAdder $fileAdderCallback
*/
public function copy(
HasMedia $model,
string $collectionName = 'default',
string $diskName = '',
string $fileName = '',
?Closure $fileAdderCallback = null
): self {
$temporaryDirectory = TemporaryDirectory::create();

$temporaryFile = $temporaryDirectory->path('/').DIRECTORY_SEPARATOR.$this->file_name;
Expand All @@ -411,11 +420,16 @@ public function copy(HasMedia $model, $collectionName = 'default', string $diskN
->setOrder($this->order_column)
->withManipulations($this->manipulations)
->withCustomProperties($this->custom_properties);

if ($fileName !== '') {
$fileAdder->usingFileName($fileName);
}
$newMedia = $fileAdder
->toMediaCollection($collectionName, $diskName);

if ($fileAdderCallback instanceof Closure) {
$fileAdder = $fileAdderCallback($fileAdder);
}

$newMedia = $fileAdder->toMediaCollection($collectionName, $diskName);

$temporaryDirectory->delete();

Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/Media/CopyTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Spatie\MediaLibrary\MediaCollections\FileAdder;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;

it('can copy media from one model to another', function () {
Expand Down Expand Up @@ -99,6 +100,30 @@
expect('custom-property-value')->toEqual($movedMedia->getCustomProperty('custom-property-name'));
});

it('can handle file adder callback', function () {
/** @var TestModel $model */
$model = TestModel::create(['name' => 'test']);

/** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $media */
$media = $model
->addMedia($this->getTestJpg())
->usingName('custom-name')
->withCustomProperties(['custom-property-name' => 'custom-property-value'])
->toMediaCollection();

$this->assertFileExists($this->getMediaDirectory($media->id.'/test.jpg'));

$anotherModel = TestModel::create(['name' => 'another-test']);

$movedMedia = $media->copy($anotherModel, 'images', fileAdderCallback: function (FileAdder $fileAdder): FileAdder {
return $fileAdder->usingFileName('new-filename.jpg');
});

$movedMedia->refresh();

expect($movedMedia->file_name)->toBe('new-filename.jpg');
});

it('can copy file with accent', function () {
if (! file_exists(storage_path('media-library/temp'))) {
mkdir(storage_path('media-library/temp'), 0777, true);
Expand Down

0 comments on commit c3828e7

Please sign in to comment.