Add MediaFile Helper

This commit is contained in:
philipp lang 2024-01-03 00:46:45 +01:00
parent 38c40c48ac
commit 2e90fde134
3 changed files with 40 additions and 9 deletions

View File

@ -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
....
})
```

View File

@ -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)
);
});

32
src/MediaFile.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Zoomyboy\MedialibraryHelper;
use Symfony\Component\HttpFoundation\File\File;
class MediaFile
{
public File $file;
public function __construct(string $path)
{
$this->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);
}
}