From 63fb409951d194f9253b837642a50a06a88526ad Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 8 Mar 2023 01:38:57 +0100 Subject: [PATCH] update filename hook --- src/CollectionExtension.php | 2 +- src/MediaController.php | 2 +- tests/Models/Post.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CollectionExtension.php b/src/CollectionExtension.php index 0949822..4c318af 100644 --- a/src/CollectionExtension.php +++ b/src/CollectionExtension.php @@ -65,7 +65,7 @@ class CollectionExtension return; } $this->customCallbacks = collect([ - 'forceFileName' => fn ($name) => $name, + 'forceFileName' => fn ($model, $name) => $name, 'stored' => fn ($event) => true, 'storing' => fn ($adder, $name) => $adder, 'withDefaultProperties' => fn ($path) => [], diff --git a/src/MediaController.php b/src/MediaController.php index 7e0cdb3..525dc50 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -45,7 +45,7 @@ class MediaController $medias = collect($content)->map(function ($c) use ($collection, $model) { $pathinfo = pathinfo($c['name']); - $path = $collection->runCallback('forceFileName', $pathinfo['filename']).'.'.$pathinfo['extension']; + $path = $collection->runCallback('forceFileName', $model, $pathinfo['filename']).'.'.$pathinfo['extension']; Storage::disk('public')->put($path, base64_decode($c['content'])); $adder = $model ->addMedia(Storage::disk('public')->path($path)) diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 388da41..304dc4b 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -29,11 +29,11 @@ class Post extends Model implements HasMedia $this->addMediaCollection('images'); - $this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($name) { + $this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) { return $name.' '.now()->format('Y-m-d'); }); - $this->addMediaCollection('multipleForced')->forceFileName(function ($name) { + $this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) { return $name.' '.now()->format('Y-m-d'); });