update filename hook

This commit is contained in:
philipp lang 2023-03-08 01:38:57 +01:00
parent 47c734dfd4
commit 63fb409951
3 changed files with 4 additions and 4 deletions

View File

@ -65,7 +65,7 @@ class CollectionExtension
return; return;
} }
$this->customCallbacks = collect([ $this->customCallbacks = collect([
'forceFileName' => fn ($name) => $name, 'forceFileName' => fn ($model, $name) => $name,
'stored' => fn ($event) => true, 'stored' => fn ($event) => true,
'storing' => fn ($adder, $name) => $adder, 'storing' => fn ($adder, $name) => $adder,
'withDefaultProperties' => fn ($path) => [], 'withDefaultProperties' => fn ($path) => [],

View File

@ -45,7 +45,7 @@ class MediaController
$medias = collect($content)->map(function ($c) use ($collection, $model) { $medias = collect($content)->map(function ($c) use ($collection, $model) {
$pathinfo = pathinfo($c['name']); $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'])); Storage::disk('public')->put($path, base64_decode($c['content']));
$adder = $model $adder = $model
->addMedia(Storage::disk('public')->path($path)) ->addMedia(Storage::disk('public')->path($path))

View File

@ -29,11 +29,11 @@ class Post extends Model implements HasMedia
$this->addMediaCollection('images'); $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'); 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'); return $name.' '.now()->format('Y-m-d');
}); });