diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index 2b9e9a562..27c23a4ee 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -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 }}" diff --git a/CHANGELOG.md b/CHANGELOG.md index 709bfb36c..6c482c110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/handling-uploads-with-media-library-pro/installation.md b/docs/handling-uploads-with-media-library-pro/installation.md index cea6af433..8ec92342b 100644 --- a/docs/handling-uploads-with-media-library-pro/installation.md +++ b/docs/handling-uploads-with-media-library-pro/installation.md @@ -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). diff --git a/src/MediaCollections/Models/Media.php b/src/MediaCollections/Models/Media.php index a34dd74f6..706822c62 100644 --- a/src/MediaCollections/Models/Media.php +++ b/src/MediaCollections/Models/Media.php @@ -2,6 +2,7 @@ namespace Spatie\MediaLibrary\MediaCollections\Models; +use Closure; use DateTimeInterface; use Illuminate\Contracts\Mail\Attachable; use Illuminate\Contracts\Support\Htmlable; @@ -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; @@ -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; @@ -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(); diff --git a/tests/Feature/Media/CopyTest.php b/tests/Feature/Media/CopyTest.php index 6112105a6..198ae43a1 100644 --- a/tests/Feature/Media/CopyTest.php +++ b/tests/Feature/Media/CopyTest.php @@ -1,5 +1,6 @@ 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);