Compare commits
No commits in common. "master" and "dev" have entirely different histories.
28
README.md
28
README.md
|
@ -1,28 +0,0 @@
|
||||||
# Laravel Medialibrary Helper
|
|
||||||
|
|
||||||
This package creates routes for the popular Medialibrary Package from Spatie ().
|
|
||||||
|
|
||||||
## Available methods
|
|
||||||
|
|
||||||
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 with the extension (e.g. disc.jpg).
|
|
||||||
|
|
||||||
```
|
|
||||||
forceFileName(fn ($model, $path) => Str::slug($path))
|
|
||||||
```
|
|
||||||
|
|
||||||
You can set a max width (in Pixels) for images. This will resize the image BEFORE any normal Medialibrary conversions take place.
|
|
||||||
|
|
||||||
```
|
|
||||||
maxWidth(fn () => 2500)
|
|
||||||
```
|
|
||||||
|
|
||||||
You can call whatever you want after an image has been added, modified or deleted.
|
|
||||||
|
|
||||||
```
|
|
||||||
->after(function ($model) {
|
|
||||||
....
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"ext-imagick": ">=3.6.0",
|
|
||||||
"spatie/laravel-medialibrary": "^10.7",
|
"spatie/laravel-medialibrary": "^10.7",
|
||||||
"laravel/framework": "^9.50",
|
"laravel/framework": "^9.50",
|
||||||
"spatie/laravel-data": "^3.1",
|
"spatie/laravel-data": "^3.1",
|
||||||
|
@ -36,12 +35,5 @@
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
"pestphp/pest-plugin": true
|
"pestphp/pest-plugin": true
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"laravel": {
|
|
||||||
"providers": [
|
|
||||||
"Zoomyboy\\MedialibraryHelper\\ServiceProvider"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -8,8 +8,7 @@ class CollectionExtension
|
||||||
{
|
{
|
||||||
public function boot(): void
|
public function boot(): void
|
||||||
{
|
{
|
||||||
MediaCollection::mixin(new class()
|
MediaCollection::mixin(new class() {
|
||||||
{
|
|
||||||
public function forceFileName()
|
public function forceFileName()
|
||||||
{
|
{
|
||||||
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
||||||
|
@ -20,16 +19,6 @@ class CollectionExtension
|
||||||
return fn ($callback) => $this->registerCustomCallback('storing', $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()
|
public function withDefaultProperties()
|
||||||
{
|
{
|
||||||
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
|
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
|
||||||
|
@ -50,11 +39,6 @@ class CollectionExtension
|
||||||
return fn ($callback) => $this->registerCustomCallback('withFallback', $callback);
|
return fn ($callback) => $this->registerCustomCallback('withFallback', $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function maxWidth()
|
|
||||||
{
|
|
||||||
return fn ($callback) => $this->registerCustomCallback('maxWidth', $callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function runCallback()
|
public function runCallback()
|
||||||
{
|
{
|
||||||
return function (string $callback, ...$parameters) {
|
return function (string $callback, ...$parameters) {
|
||||||
|
@ -82,12 +66,9 @@ class CollectionExtension
|
||||||
}
|
}
|
||||||
$this->customCallbacks = collect([
|
$this->customCallbacks = collect([
|
||||||
'forceFileName' => fn ($model, $name) => $name,
|
'forceFileName' => fn ($model, $name) => $name,
|
||||||
'maxWidth' => fn ($size) => null,
|
|
||||||
'stored' => fn ($event) => true,
|
'stored' => fn ($event) => true,
|
||||||
'after' => fn ($event) => true,
|
|
||||||
'destroyed' => fn ($event) => true,
|
|
||||||
'storing' => fn ($adder, $name) => $adder,
|
'storing' => fn ($adder, $name) => $adder,
|
||||||
'withDefaultProperties' => fn ($path, $pathinfo) => [],
|
'withDefaultProperties' => fn ($path) => [],
|
||||||
'withPropertyValidation' => fn ($path) => [],
|
'withPropertyValidation' => fn ($path) => [],
|
||||||
'withFallback' => fn ($parent) => null,
|
'withFallback' => fn ($parent) => null,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -2,17 +2,14 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper;
|
namespace Zoomyboy\MedialibraryHelper;
|
||||||
|
|
||||||
use Imagick;
|
|
||||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Spatie\Image\Image;
|
|
||||||
use Spatie\LaravelData\DataCollection;
|
use Spatie\LaravelData\DataCollection;
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
|
|
||||||
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;
|
||||||
|
|
||||||
|
@ -49,12 +46,12 @@ class MediaController
|
||||||
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
||||||
$pathinfo = pathinfo($c['name']);
|
$pathinfo = pathinfo($c['name']);
|
||||||
$basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
|
$basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
|
||||||
$path = $basename . '.' . $pathinfo['extension'];
|
$path = $basename.'.'.$pathinfo['extension'];
|
||||||
|
$adder = $model
|
||||||
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
|
->addMediaFromBase64($c['content'])
|
||||||
->usingName($basename)
|
->usingName($basename)
|
||||||
->usingFileName($path)
|
->usingFileName($path)
|
||||||
->withCustomProperties($collection->runCallback('withDefaultProperties', $path, $pathinfo));
|
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
|
||||||
|
|
||||||
return tap(
|
return tap(
|
||||||
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
|
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
|
||||||
|
@ -62,8 +59,6 @@ class MediaController
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
$collection->runCallback('after', $model->fresh());
|
|
||||||
|
|
||||||
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +72,6 @@ class MediaController
|
||||||
$validated = $request->validate($rules);
|
$validated = $request->validate($rules);
|
||||||
|
|
||||||
$media->update(['custom_properties' => data_get($validated, 'properties', [])]);
|
$media->update(['custom_properties' => data_get($validated, 'properties', [])]);
|
||||||
$media->model->getMediaCollection($media->collection_name)->runCallback('after', $media->model->fresh());
|
|
||||||
|
|
||||||
return MediaData::from($media);
|
return MediaData::from($media);
|
||||||
}
|
}
|
||||||
|
@ -104,11 +98,7 @@ class MediaController
|
||||||
public function destroy(Media $media, Request $request): JsonResponse
|
public function destroy(Media $media, Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$this->authorize('destroyMedia', [$media->model, $media->collection_name]);
|
$this->authorize('destroyMedia', [$media->model, $media->collection_name]);
|
||||||
$model = $media->model->fresh();
|
|
||||||
$collection = $model->getMediaCollection($media->collection_name);
|
|
||||||
$media->delete();
|
$media->delete();
|
||||||
$collection->runCallback('destroyed', $media->model->fresh());
|
|
||||||
$collection->runCallback('after', $media->model->fresh());
|
|
||||||
|
|
||||||
return response()->json([]);
|
return response()->json([]);
|
||||||
}
|
}
|
||||||
|
@ -137,42 +127,4 @@ class MediaController
|
||||||
|
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fileAdderFromData($model, $data, $collection): FileAdder
|
|
||||||
{
|
|
||||||
$maxWidth = $collection->runCallback('maxWidth', 9);
|
|
||||||
if (str_contains($data, ';base64')) {
|
|
||||||
[$_, $data] = explode(';', $data);
|
|
||||||
[$_, $data] = explode(',', $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// strict mode filters for non-base64 alphabet characters
|
|
||||||
$binaryData = base64_decode($data, true);
|
|
||||||
|
|
||||||
if (false === $binaryData) {
|
|
||||||
throw InvalidBase64Data::create();
|
|
||||||
}
|
|
||||||
|
|
||||||
// decoding and then reencoding should not change the data
|
|
||||||
if (base64_encode($binaryData) !== $data) {
|
|
||||||
throw InvalidBase64Data::create();
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
|
|
||||||
file_put_contents($tmpFile, $binaryData);
|
|
||||||
|
|
||||||
$i = (new Imagick());
|
|
||||||
$i->readImage($tmpFile);
|
|
||||||
|
|
||||||
if ($i->getImageFormat() === 'HEIC') {
|
|
||||||
$i->setFormat('jpg');
|
|
||||||
$i->writeImage($tmpFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null !== $maxWidth && 'image/jpeg' === mime_content_type($tmpFile)) {
|
|
||||||
Image::load($tmpFile)->width($maxWidth)->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $model->addMedia($tmpFile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,9 @@ class MediaData extends Data
|
||||||
|
|
||||||
public static function fromMedia(Media $media): self
|
public static function fromMedia(Media $media): self
|
||||||
{
|
{
|
||||||
$conversions = collect($media->getMediaConversionNames())->flip()->map(fn ($integer, $conversion) => $media->hasGeneratedConversion($conversion)
|
$conversions = collect($media->getMediaConversionNames())->flip()->map(fn ($integer, $conversion) => [
|
||||||
? ['original_url' => $media->getFullUrl($conversion)]
|
'original_url' => $media->getFullUrl($conversion),
|
||||||
: null,
|
]);
|
||||||
);
|
|
||||||
|
|
||||||
return self::withoutMagicalCreationFrom([
|
return self::withoutMagicalCreationFrom([
|
||||||
...$media->toArray(),
|
...$media->toArray(),
|
||||||
|
|
|
@ -25,12 +25,10 @@ class OrderController
|
||||||
|
|
||||||
$model = app('media-library-helpers')->get($parentModel);
|
$model = app('media-library-helpers')->get($parentModel);
|
||||||
$model = $model::find($parentId);
|
$model = $model::find($parentId);
|
||||||
$this->authorize('listMedia', [$model, $collectionName]);
|
$this->authorize('updateMedia', [$model, $collectionName]);
|
||||||
|
|
||||||
Media::setNewOrder($request->order);
|
Media::setNewOrder($request->order);
|
||||||
|
|
||||||
$model->getMediaCollection($collectionName)->runCallback('after', $model->fresh());
|
|
||||||
|
|
||||||
return MediaData::collection($model->getMedia($collectionName));
|
return MediaData::collection($model->getMedia($collectionName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
|
||||||
|
|
||||||
class MediaChange
|
|
||||||
{
|
|
||||||
use Dispatchable;
|
|
||||||
use SerializesModels;
|
|
||||||
|
|
||||||
public function __construct(public HasMedia $model)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function broadcastOn()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
|
||||||
|
|
||||||
class MediaDestroyed
|
|
||||||
{
|
|
||||||
use Dispatchable;
|
|
||||||
use SerializesModels;
|
|
||||||
|
|
||||||
public function __construct(public HasMedia $model)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function broadcastOn()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,12 +2,8 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
|
|
||||||
|
|
||||||
test('it deletes multiple media', function () {
|
test('it deletes multiple media', function () {
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
|
||||||
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
|
||||||
|
@ -37,16 +33,3 @@ test('it needs authorization', function () {
|
||||||
|
|
||||||
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(403);
|
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(403);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it fires event', function () {
|
|
||||||
Event::fake();
|
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
|
||||||
$post = $this->newPost();
|
|
||||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('singleWithEvent');
|
|
||||||
$media = $post->getFirstMedia('singleWithEvent');
|
|
||||||
|
|
||||||
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200);
|
|
||||||
|
|
||||||
Event::assertDispatched(MediaDestroyed::class, fn ($event) => $event->model->is($post));
|
|
||||||
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
|
|
||||||
});
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Zoomyboy\MedialibraryHelper\Tests\TestCase;
|
||||||
|
|
||||||
|
class MiddlewareTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function testItReturns401WhenNotLoggedIn(): void
|
||||||
|
{
|
||||||
|
$this->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
|
||||||
|
$response = $this->postJson('/mediaupload', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'defaultSingleFile',
|
||||||
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
|
'name' => 'beispiel bild.jpg',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(401);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItReturns401WhenDestroying(): void
|
||||||
|
{
|
||||||
|
$this->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$media = $post->addMedia($this->pdfFile()->getPathname())->toMediaCollection('defaultSingleFile');
|
||||||
|
|
||||||
|
$response = $this->deleteJson("/mediaupload/{$media->id}");
|
||||||
|
|
||||||
|
$response->assertStatus(401);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defineEnvironment($app)
|
||||||
|
{
|
||||||
|
$app['config']->set('media-library.middleware', ['web', 'auth:web']);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,11 +2,7 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
|
||||||
|
|
||||||
test('it can reorder media', function () {
|
test('it can reorder media', function () {
|
||||||
Event::fake();
|
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||||
|
@ -24,7 +20,6 @@ test('it can reorder media', function () {
|
||||||
$response->assertJsonPath('1.id', $order->get(1));
|
$response->assertJsonPath('1.id', $order->get(1));
|
||||||
$response->assertJsonPath('2.id', $order->get(2));
|
$response->assertJsonPath('2.id', $order->get(2));
|
||||||
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
|
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
|
||||||
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('images should belong to same model', function () {
|
test('images should belong to same model', function () {
|
||||||
|
@ -43,16 +38,3 @@ test('images should belong to same model', function () {
|
||||||
|
|
||||||
$response->assertJsonValidationErrors('order');
|
$response->assertJsonValidationErrors('order');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it should authorize', function () {
|
|
||||||
$this->auth(['listMedia' => false])->registerModel();
|
|
||||||
|
|
||||||
$post = $this->newPost();
|
|
||||||
$media = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
|
||||||
|
|
||||||
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
|
|
||||||
'order' => [$media->id],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response->assertStatus(403);
|
|
||||||
});
|
|
||||||
|
|
|
@ -2,11 +2,7 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
|
||||||
|
|
||||||
test('it updates a single files properties', function () {
|
test('it updates a single files properties', function () {
|
||||||
Event::fake();
|
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
||||||
|
@ -24,7 +20,6 @@ test('it updates a single files properties', function () {
|
||||||
$this->assertEquals(null, $media->fresh()->getCustomProperty('missing'));
|
$this->assertEquals(null, $media->fresh()->getCustomProperty('missing'));
|
||||||
$response->assertJsonPath('properties.test', 'new');
|
$response->assertJsonPath('properties.test', 'new');
|
||||||
$response->assertJsonMissingPath('properties.missing');
|
$response->assertJsonMissingPath('properties.missing');
|
||||||
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it validates a single files properties', function () {
|
test('it validates a single files properties', function () {
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
test('it uploads a single file to a single file collection', function () {
|
test('it uploads a single file to a single file collection', function () {
|
||||||
|
@ -35,41 +34,6 @@ test('it uploads a single file to a single file collection', function () {
|
||||||
$response->assertJsonMissingPath('model_id');
|
$response->assertJsonMissingPath('model_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it uploads heig image', function () {
|
|
||||||
$this->auth()->registerModel();
|
|
||||||
$post = $this->newPost();
|
|
||||||
$content = base64_encode($this->getFile('heic.jpg', 'heic.jpg')->getContent());
|
|
||||||
|
|
||||||
$this->postJson('/mediaupload', [
|
|
||||||
'model' => 'post',
|
|
||||||
'id' => $post->id,
|
|
||||||
'collection' => 'conversionsWithDefault',
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
])->assertStatus(201);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it uploads a single image to a single file collection', function () {
|
|
||||||
$this->auth()->registerModel();
|
|
||||||
$post = $this->newPost();
|
|
||||||
$content = base64_encode($this->jpgFile()->getContent());
|
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
|
||||||
'model' => 'post',
|
|
||||||
'id' => $post->id,
|
|
||||||
'collection' => 'defaultSingleFile',
|
|
||||||
'payload' => [
|
|
||||||
'content' => $content,
|
|
||||||
'name' => 'beispiel bild.jpg',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response->assertStatus(201);
|
|
||||||
$this->assertCount(1, $post->getMedia('defaultSingleFile'));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it forces a filename for a single collection', function () {
|
test('it forces a filename for a single collection', function () {
|
||||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
|
@ -176,7 +140,6 @@ test('it throws event when file has been uploaded', function () {
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
|
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
|
||||||
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it throws event when multiple files uploaded', function () {
|
test('it throws event when multiple files uploaded', function () {
|
||||||
|
@ -249,14 +212,14 @@ test('it returns 403 when not authorized', function () {
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'model' => 'post',
|
'model' => 'post',
|
||||||
'id' => $post->id,
|
'id' => $post->id,
|
||||||
'collection' => 'defaultSingleFile',
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(403);
|
$response->assertStatus(403);
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,8 +8,6 @@ use Illuminate\Support\Str;
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
|
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
class Post extends Model implements HasMedia
|
class Post extends Model implements HasMedia
|
||||||
|
@ -20,23 +18,23 @@ class Post extends Model implements HasMedia
|
||||||
|
|
||||||
public function registerMediaCollections(): void
|
public function registerMediaCollections(): void
|
||||||
{
|
{
|
||||||
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
|
$this->addMediaCollection('defaultSingleFile')->singleFile();
|
||||||
|
|
||||||
$this->addMediaCollection('conversionsWithDefault')
|
$this->addMediaCollection('conversionsWithDefault')
|
||||||
->singleFile()
|
->singleFile()
|
||||||
->withFallback(fn ($parent) => ['default.jpg', 'public'])
|
->withFallback(fn ($parent) => ['default.jpg', 'public'])
|
||||||
->registerMediaConversions(function () {
|
->registerMediaConversions(function () {
|
||||||
$this->addMediaConversion('tiny')->width(200)->height(200);
|
$this->addMediaConversion('tiny')->width(200)->height(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('images')->after(fn ($model) => Event::dispatch(new MediaChange($model)));
|
$this->addMediaCollection('images');
|
||||||
|
|
||||||
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
|
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
|
||||||
return $name . ' ' . now()->format('Y-m-d');
|
return $name.' '.now()->format('Y-m-d');
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) {
|
$this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) {
|
||||||
return $name . ' ' . now()->format('Y-m-d');
|
return $name.' '.now()->format('Y-m-d');
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
|
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
|
||||||
|
@ -48,19 +46,16 @@ class Post extends Model implements HasMedia
|
||||||
|
|
||||||
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
|
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
|
||||||
Event::dispatch(new MediaStored($media));
|
Event::dispatch(new MediaStored($media));
|
||||||
})
|
});
|
||||||
->destroyed(fn ($model) => Event::dispatch(new MediaDestroyed($model)))
|
|
||||||
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
|
|
||||||
|
|
||||||
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
|
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
|
||||||
Event::dispatch(new MediaStored($media));
|
Event::dispatch(new MediaStored($media));
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path, $pathinfo) => [
|
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
|
||||||
'test' => Str::camel($path),
|
'test' => Str::camel($path),
|
||||||
])->withPropertyValidation(fn ($path) => [
|
])->withPropertyValidation(fn ($path) => [
|
||||||
'test' => 'string|max:10',
|
'test' => 'string|max:10',
|
||||||
])
|
]);
|
||||||
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ class TestCase extends BaseTestCase
|
||||||
*/
|
*/
|
||||||
protected function defineDatabaseMigrations(): void
|
protected function defineDatabaseMigrations(): void
|
||||||
{
|
{
|
||||||
$this->loadMigrationsFrom(__DIR__ . '/migrations');
|
$this->loadMigrationsFrom(__DIR__.'/migrations');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPackageProviders($app): array
|
protected function getPackageProviders($app): array
|
||||||
|
@ -44,8 +44,8 @@ class TestCase extends BaseTestCase
|
||||||
|
|
||||||
protected function getFile(string $location, string $as): File
|
protected function getFile(string $location, string $as): File
|
||||||
{
|
{
|
||||||
$path = __DIR__ . '/stubs/' . $location;
|
$path = __DIR__.'/stubs/'.$location;
|
||||||
$to = sys_get_temp_dir() . '/' . $as;
|
$to = sys_get_temp_dir().'/'.$as;
|
||||||
copy($path, $to);
|
copy($path, $to);
|
||||||
|
|
||||||
return new File($to);
|
return new File($to);
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue