Compare commits
No commits in common. "79cb5a8f58cc2d36f8df25d95de3c042a0bb8c80" and "820a7255176a2ffdfed011877cebce3644688d35" have entirely different histories.
79cb5a8f58
...
820a725517
|
@ -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
|
||||||
....
|
....
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -2,89 +2,96 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper;
|
namespace Zoomyboy\MedialibraryHelper;
|
||||||
|
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||||
|
|
||||||
class CollectionExtension
|
class CollectionExtension
|
||||||
{
|
{
|
||||||
public function forceFileName()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
MediaCollection::mixin(new class() {
|
||||||
}
|
public function forceFileName()
|
||||||
|
{
|
||||||
public function storing()
|
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('storing', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function destroyed()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('destroyed', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function after()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('after', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function withDefaultProperties()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stored()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('stored', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function withPropertyValidation()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('withPropertyValidation', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function withFallback()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('withFallback', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function maxWidth()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('maxWidth', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function runCallback()
|
|
||||||
{
|
|
||||||
return function (string $callback, ...$parameters) {
|
|
||||||
$this->setDefaultCustomCallbacks();
|
|
||||||
|
|
||||||
return call_user_func($this->customCallbacks->get($callback), ...$parameters);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerCustomCallback()
|
|
||||||
{
|
|
||||||
return function (string $name, callable $callback) {
|
|
||||||
$this->setDefaultCustomCallbacks();
|
|
||||||
$this->customCallbacks->put($name, $callback);
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setDefaultCustomCallbacks()
|
|
||||||
{
|
|
||||||
return function () {
|
|
||||||
if (property_exists($this, 'customCallbacks')) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
$this->customCallbacks = collect([
|
|
||||||
'forceFileName' => fn ($model, $name) => $name,
|
public function storing()
|
||||||
'maxWidth' => fn ($size) => null,
|
{
|
||||||
'stored' => fn ($event) => true,
|
return fn ($callback) => $this->registerCustomCallback('storing', $callback);
|
||||||
'after' => fn ($event) => true,
|
}
|
||||||
'destroyed' => fn ($event) => true,
|
|
||||||
'storing' => fn ($adder, $name) => $adder,
|
public function destroyed()
|
||||||
'withDefaultProperties' => fn ($path) => [],
|
{
|
||||||
'withPropertyValidation' => fn ($path) => [],
|
return fn ($callback) => $this->registerCustomCallback('destroyed', $callback);
|
||||||
'withFallback' => fn ($parent) => null,
|
}
|
||||||
]);
|
|
||||||
};
|
public function after()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('after', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withDefaultProperties()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stored()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('stored', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withPropertyValidation()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('withPropertyValidation', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withFallback()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('withFallback', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function maxWidth()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('maxWidth', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function runCallback()
|
||||||
|
{
|
||||||
|
return function (string $callback, ...$parameters) {
|
||||||
|
$this->setDefaultCustomCallbacks();
|
||||||
|
|
||||||
|
return call_user_func($this->customCallbacks->get($callback), ...$parameters);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function registerCustomCallback()
|
||||||
|
{
|
||||||
|
return function (string $name, callable $callback) {
|
||||||
|
$this->setDefaultCustomCallbacks();
|
||||||
|
$this->customCallbacks->put($name, $callback);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDefaultCustomCallbacks()
|
||||||
|
{
|
||||||
|
return function () {
|
||||||
|
if (property_exists($this, 'customCallbacks')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->customCallbacks = collect([
|
||||||
|
'forceFileName' => fn ($model, $name) => $name,
|
||||||
|
'maxWidth' => fn ($size) => null,
|
||||||
|
'stored' => fn ($event) => true,
|
||||||
|
'after' => fn ($event) => true,
|
||||||
|
'destroyed' => fn ($event) => true,
|
||||||
|
'storing' => fn ($adder, $name) => $adder,
|
||||||
|
'withDefaultProperties' => fn ($path) => [],
|
||||||
|
'withPropertyValidation' => fn ($path) => [],
|
||||||
|
'withFallback' => fn ($parent) => null,
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in New Issue