diff --git a/README.md b/README.md index 337708d..796391c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This package creates routes for the popular Medialibrary Package from Spatie (). In RegisterMediaCollections, you have the following methods available: -You can set a filename by default for the file. This accepts the associated Model, as well as the original filename. You should return the new name of the file without the extension (e.g. disc). +You can set a filename by default for the file. This accepts the associated Model, as well as the original basename (without extension). You should return the new name of the file without the extension (e.g. disc). ``` forceFileName(fn ($model, $path) => Str::slug($path)) @@ -25,4 +25,3 @@ You can call whatever you want after an image has been added, modified or delete .... }) ``` - diff --git a/src/MediaController.php b/src/MediaController.php index 0f25106..10430a3 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -14,6 +14,7 @@ use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data; use Spatie\MediaLibrary\MediaCollections\FileAdder; use Spatie\MediaLibrary\MediaCollections\MediaCollection; use Spatie\MediaLibrary\MediaCollections\Models\Media; +use Symfony\Component\HttpFoundation\File\File; class MediaController { @@ -46,17 +47,16 @@ class MediaController $content = $isSingle ? [$request->input('payload')] : $request->input('payload'); $medias = collect($content)->map(function ($c) use ($collection, $model) { - $pathinfo = pathinfo($c['name']); - $basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']); - $path = $basename . '.' . $pathinfo['extension']; + $file = new MediaFile($c['name']); + $file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename())); $adder = $this->fileAdderFromData($model, $c['content'], $collection) - ->usingName($basename) - ->usingFileName($path) - ->withCustomProperties($collection->runCallback('withDefaultProperties', $path)); + ->usingName($file->getBasename()) + ->usingFileName($file->getFilename()) + ->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename())); return tap( - $collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name), + $collection->runCallback('storing', $adder, $file->getFilename())->toMediaCollection($collection->name), fn ($media) => $collection->runCallback('stored', $media) ); }); diff --git a/src/MediaFile.php b/src/MediaFile.php new file mode 100644 index 0000000..784ff2a --- /dev/null +++ b/src/MediaFile.php @@ -0,0 +1,32 @@ +file = new File($path, false); + } + + public function getBasename(): string + { + return $this->file->getBasename('.' . $this->file->getExtension()); + } + + public function setBasename(string $basename): void + { + $newInstance = new self(($this->getPath() ? $this->getPath() . '/' : '') . $basename . '.' . $this->getExtension()); + $this->file = $newInstance->file; + } + + public function __call($method, $arguments) + { + return $this->file->{$method}(...$arguments); + } +}