Add MediaFile Helper
This commit is contained in:
parent
38c40c48ac
commit
2e90fde134
|
@ -6,7 +6,7 @@ This package creates routes for the popular Medialibrary Package from Spatie ().
|
||||||
|
|
||||||
In RegisterMediaCollections, you have the following methods available:
|
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))
|
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
|
||||||
....
|
....
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
|
||||||
use Spatie\MediaLibrary\MediaCollections\FileAdder;
|
use Spatie\MediaLibrary\MediaCollections\FileAdder;
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
class MediaController
|
class MediaController
|
||||||
{
|
{
|
||||||
|
@ -46,17 +47,16 @@ class MediaController
|
||||||
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
|
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
|
||||||
|
|
||||||
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
||||||
$pathinfo = pathinfo($c['name']);
|
$file = new MediaFile($c['name']);
|
||||||
$basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
|
$file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename()));
|
||||||
$path = $basename . '.' . $pathinfo['extension'];
|
|
||||||
|
|
||||||
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
|
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
|
||||||
->usingName($basename)
|
->usingName($file->getBasename())
|
||||||
->usingFileName($path)
|
->usingFileName($file->getFilename())
|
||||||
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
|
->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename()));
|
||||||
|
|
||||||
return tap(
|
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)
|
fn ($media) => $collection->runCallback('stored', $media)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue