Compare commits

..

No commits in common. "fb15678d4e4acc3abb61d2c4b248310aeeb4377d" and "ff3516ca4f2e28ca799401741802a1563f5d492e" have entirely different histories.

8 changed files with 4 additions and 95 deletions

View File

@ -19,16 +19,6 @@ class CollectionExtension
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);
@ -77,8 +67,6 @@ class CollectionExtension
$this->customCallbacks = collect([
'forceFileName' => fn ($model, $name) => $name,
'stored' => fn ($event) => true,
'after' => fn ($event) => true,
'destroyed' => fn ($event) => true,
'storing' => fn ($adder, $name) => $adder,
'withDefaultProperties' => fn ($path) => [],
'withPropertyValidation' => fn ($path) => [],

View File

@ -5,6 +5,7 @@ namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Spatie\LaravelData\DataCollection;
@ -58,8 +59,6 @@ class MediaController
);
});
$collection->runCallback('after', $model->fresh());
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
}
@ -73,7 +72,6 @@ class MediaController
$validated = $request->validate($rules);
$media->update(['custom_properties' => data_get($validated, 'properties', [])]);
$media->model->getMediaCollection($media->collection_name)->runCallback('after', $media->model->fresh());
return MediaData::from($media);
}
@ -100,11 +98,7 @@ class MediaController
public function destroy(Media $media, Request $request): JsonResponse
{
$this->authorize('destroyMedia', [$media->model, $media->collection_name]);
$model = $media->model->fresh();
$collection = $model->getMediaCollection($media->collection_name);
$media->delete();
$collection->runCallback('destroyed', $media->model->fresh());
$collection->runCallback('after', $media->model->fresh());
return response()->json([]);
}

View File

@ -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 [];
}
}

View File

@ -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 [];
}
}

View File

@ -2,12 +2,8 @@
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 () {
$this->auth()->registerModel()->withoutExceptionHandling();
$this->auth()->registerModel();
$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');
@ -37,16 +33,3 @@ test('it needs authorization', function () {
$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));
});

View File

@ -2,11 +2,7 @@
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
test('it updates a single files properties', function () {
Event::fake();
$this->auth()->registerModel();
$post = $this->newPost();
$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'));
$response->assertJsonPath('properties.test', 'new');
$response->assertJsonMissingPath('properties.missing');
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
});
test('it validates a single files properties', function () {

View File

@ -4,7 +4,6 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Carbon\Carbon;
use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
test('it uploads a single file to a single file collection', function () {
@ -141,7 +140,6 @@ test('it throws event when file has been uploaded', function () {
$response->assertStatus(201);
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 () {

View File

@ -8,8 +8,6 @@ use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
class Post extends Model implements HasMedia
@ -48,9 +46,7 @@ class Post extends Model implements HasMedia
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $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) {
Event::dispatch(new MediaStored($media));
@ -60,7 +56,6 @@ class Post extends Model implements HasMedia
'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10',
])
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
]);
}
}