Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callback to manipulate FileAdder when performing a copy #3658

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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