Skip to content

Commit

Permalink
Merge branch 'main' of github.com:spatie/laravel-medialibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 15, 2024
2 parents e663609 + 4ebb564 commit 21a4e6b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2.1.0
uses: dependabot/fetch-metadata@v2.2.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `laravel-medialibrary` will be documented in this file

## 11.7.3 - 2024-07-02

### What's Changed

* Added callback to manipulate FileAdder when performing a copy by @chrispage1 in https://github.com/spatie/laravel-medialibrary/pull/3658

**Full Changelog**: https://github.com/spatie/laravel-medialibrary/compare/11.7.2...11.7.3

## 11.7.2 - 2024-06-21

### What's Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Single application licenses maybe installed in a single Laravel app. In case you

## Current version

The current version of Media Library Pro is v4.
The current version of Media Library Pro is v5.

You will find upgrade instructions [here](/docs/laravel-medialibrary/v11/handling-uploads-with-media-library-pro/upgrading).

Expand Down
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 21a4e6b

Please sign in to comment.