Compare commits

..

No commits in common. "79cb5a8f58cc2d36f8df25d95de3c042a0bb8c80" and "820a7255176a2ffdfed011877cebce3644688d35" have entirely different histories.

6 changed files with 102 additions and 200 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: 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 basename (without extension). 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 filename. You should return the new name of the file with the extension (e.g. disc.jpg).
``` ```
forceFileName(fn ($model, $path) => Str::slug($path)) forceFileName(fn ($model, $path) => Str::slug($path))
@ -25,3 +25,4 @@ You can call whatever you want after an image has been added, modified or delete
.... ....
}) })
``` ```

View File

@ -2,8 +2,13 @@
namespace Zoomyboy\MedialibraryHelper; namespace Zoomyboy\MedialibraryHelper;
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
class CollectionExtension class CollectionExtension
{ {
public function boot(): void
{
MediaCollection::mixin(new class() {
public function forceFileName() public function forceFileName()
{ {
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback); return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
@ -68,7 +73,7 @@ class CollectionExtension
}; };
} }
protected function setDefaultCustomCallbacks() public function setDefaultCustomCallbacks()
{ {
return function () { return function () {
if (property_exists($this, 'customCallbacks')) { if (property_exists($this, 'customCallbacks')) {
@ -87,4 +92,6 @@ class CollectionExtension
]); ]);
}; };
} }
});
}
} }

View File

@ -14,7 +14,6 @@ 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
{ {
@ -23,8 +22,8 @@ class MediaController
public function store(Request $request) public function store(Request $request)
{ {
$request->validate([ $request->validate([
'name' => 'string',
'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())], 'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())],
'id' => 'required',
]); ]);
$model = $this->validateModel($request); $model = $this->validateModel($request);
@ -34,28 +33,30 @@ class MediaController
$request->validate($isSingle ? [ $request->validate($isSingle ? [
'payload' => 'array', 'payload' => 'array',
'payload.name' => 'required|string|regex:/\..*$/|max:255', 'payload.*' => '',
'payload.name' => 'required|string|max:255',
'payload.content' => 'required|string', 'payload.content' => 'required|string',
] : [ ] : [
'payload' => 'required|array|min:1', 'payload' => 'required|array|min:1',
'payload.*' => 'array', 'payload.*' => 'array',
'payload.*.name' => 'required|string|regex:/\..*$/|max:255', 'payload.*.name' => 'string',
'payload.*.content' => 'required|string', 'payload.*.content' => 'string',
]); ]);
$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) {
$file = new MediaFile($c['name']); $pathinfo = pathinfo($c['name']);
$file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename())); $basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
$path = $basename . '.' . $pathinfo['extension'];
$adder = $this->fileAdderFromData($model, $c['content'], $collection) $adder = $this->fileAdderFromData($model, $c['content'], $collection)
->usingName($file->getBasename()) ->usingName($basename)
->usingFileName($file->getFilename()) ->usingFileName($path)
->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename())); ->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
return tap( return tap(
$collection->runCallback('storing', $adder, $file->getFilename())->toMediaCollection($collection->name), $collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
fn ($media) => $collection->runCallback('stored', $media) fn ($media) => $collection->runCallback('stored', $media)
); );
}); });

View File

@ -1,32 +0,0 @@
<?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);
}
}

View File

@ -4,7 +4,6 @@ namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Routing\Router; use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider as BaseServiceProvider; use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
class ServiceProvider extends BaseServiceProvider class ServiceProvider extends BaseServiceProvider
{ {
@ -24,8 +23,7 @@ class ServiceProvider extends BaseServiceProvider
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update'); $router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
}); });
MediaCollection::mixin(app(CollectionExtension::class)); app(CollectionExtension::class)->boot();
// app(CollectionExtension::class)->boot();
} }
/** /**

View File

@ -35,79 +35,6 @@ test('it uploads a single file to a single file collection', function () {
$response->assertJsonMissingPath('model_id'); $response->assertJsonMissingPath('model_id');
}); });
test('test validation', function (array $attributes, string $messages) {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
...$attributes
])->assertJsonValidationErrors($messages);
})->with([
'missing collection' => [
['collection' => ''],
'collection'
],
'missing id' => [
['id' => ''],
'id'
],
]);
test('test validation for payload', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => '',
'name' => 'beispiel bild.jpg',
],
])->assertJsonValidationErrors('payload.content');
});
test('test validation for name', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => '',
],
])->assertJsonValidationErrors('payload.name');
});
test('test validation for extension', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => 'aaa',
],
])->assertJsonValidationErrors('payload.name');
});
test('it uploads a single image to a single file collection', function () { test('it uploads a single image to a single file collection', function () {
$this->auth()->registerModel(); $this->auth()->registerModel();
$post = $this->newPost(); $post = $this->newPost();