Compare commits
No commits in common. "master" and "1.1.0" have entirely different histories.
|
@ -1,3 +1 @@
|
|||
/vendor/
|
||||
/.php-cs-fixer.cache
|
||||
/.phpunit.result.cache
|
||||
|
|
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": {
|
||||
"ext-imagick": ">=3.6.0",
|
||||
"spatie/laravel-medialibrary": "^10.7",
|
||||
"laravel/framework": "^9.50",
|
||||
"spatie/laravel-data": "^3.1",
|
||||
|
@ -36,12 +35,5 @@
|
|||
"allow-plugins": {
|
||||
"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
|
||||
{
|
||||
MediaCollection::mixin(new class()
|
||||
{
|
||||
MediaCollection::mixin(new class() {
|
||||
public function forceFileName()
|
||||
{
|
||||
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
||||
|
@ -20,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);
|
||||
|
@ -45,16 +34,6 @@ class CollectionExtension
|
|||
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) {
|
||||
|
@ -81,15 +60,11 @@ class CollectionExtension
|
|||
return;
|
||||
}
|
||||
$this->customCallbacks = collect([
|
||||
'forceFileName' => fn ($model, $name) => $name,
|
||||
'maxWidth' => fn ($size) => null,
|
||||
'forceFileName' => fn ($name) => $name,
|
||||
'stored' => fn ($event) => true,
|
||||
'after' => fn ($event) => true,
|
||||
'destroyed' => fn ($event) => true,
|
||||
'storing' => fn ($adder, $name) => $adder,
|
||||
'withDefaultProperties' => fn ($path, $pathinfo) => [],
|
||||
'withDefaultProperties' => fn ($path) => [],
|
||||
'withPropertyValidation' => fn ($path) => [],
|
||||
'withFallback' => fn ($parent) => null,
|
||||
]);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,17 +2,14 @@
|
|||
|
||||
namespace Zoomyboy\MedialibraryHelper;
|
||||
|
||||
use Imagick;
|
||||
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\Image\Image;
|
||||
use Spatie\LaravelData\DataCollection;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
|
||||
use Spatie\MediaLibrary\MediaCollections\FileAdder;
|
||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
|
@ -28,9 +25,9 @@ class MediaController
|
|||
]);
|
||||
|
||||
$model = $this->validateModel($request);
|
||||
$this->authorize('storeMedia', $model);
|
||||
$collection = $model->getMediaCollection($request->input('collection'));
|
||||
$isSingle = 1 === $collection->collectionSizeLimit;
|
||||
$this->authorize('storeMedia', [$model, $collection->name]);
|
||||
|
||||
$request->validate($isSingle ? [
|
||||
'payload' => 'array',
|
||||
|
@ -48,13 +45,11 @@ class MediaController
|
|||
|
||||
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
||||
$pathinfo = pathinfo($c['name']);
|
||||
$basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
|
||||
$path = $basename . '.' . $pathinfo['extension'];
|
||||
|
||||
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
|
||||
->usingName($basename)
|
||||
->usingFileName($path)
|
||||
->withCustomProperties($collection->runCallback('withDefaultProperties', $path, $pathinfo));
|
||||
$path = $collection->runCallback('forceFileName', $pathinfo['filename']).'.'.$pathinfo['extension'];
|
||||
Storage::disk('public')->put($path, base64_decode($c['content']));
|
||||
$adder = $model
|
||||
->addMedia(Storage::disk('public')->path($path))
|
||||
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
|
||||
|
||||
return tap(
|
||||
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
|
||||
|
@ -62,14 +57,12 @@ class MediaController
|
|||
);
|
||||
});
|
||||
|
||||
$collection->runCallback('after', $model->fresh());
|
||||
|
||||
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
||||
}
|
||||
|
||||
public function update(Request $request, Media $media): MediaData
|
||||
{
|
||||
$this->authorize('updateMedia', [$media->model, $media->collection_name]);
|
||||
$this->authorize('updateMedia', $media->model);
|
||||
|
||||
$rules = collect($media->model->getMediaCollection($media->collection_name)->runCallback('withPropertyValidation', $media->file_name))
|
||||
->mapWithKeys(fn ($rule, $key) => ["properties.{$key}" => $rule])->toArray();
|
||||
|
@ -77,38 +70,28 @@ 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);
|
||||
}
|
||||
|
||||
public function index(Request $request, $parentModel, int $parentId, string $collectionName): MediaData|DataCollection
|
||||
public function index(Request $request, $parentModel, int $parentId, string $collection): MediaData|DataCollection
|
||||
{
|
||||
$model = app('media-library-helpers')->get($parentModel);
|
||||
$model = $model::find($parentId);
|
||||
$this->authorize('listMedia', [$model, $collectionName]);
|
||||
$collection = $model->getMediaCollection($collectionName);
|
||||
$isSingle = 1 === $collection->collectionSizeLimit;
|
||||
$this->authorize('listMedia', $model);
|
||||
$isSingle = 1 === $model->getMediaCollection($collection)->collectionSizeLimit;
|
||||
|
||||
abort_if($isSingle && !$model->getFirstMedia($collectionName) && !MediaData::defaultFromCollection($model, $collection), 404);
|
||||
|
||||
if ($isSingle && !$model->getFirstMedia($collectionName)) {
|
||||
return MediaData::defaultFromCollection($model, $collection);
|
||||
}
|
||||
abort_if($isSingle && !$model->getFirstMedia($collection), 404);
|
||||
|
||||
return $isSingle
|
||||
? MediaData::from($model->getFirstMedia($collectionName))
|
||||
: MediaData::collection($model->getMedia($collectionName));
|
||||
? MediaData::from($model->getFirstMedia($collection))
|
||||
: MediaData::collection($model->getMedia($collection));
|
||||
}
|
||||
|
||||
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);
|
||||
$this->authorize('destroyMedia', $media->model);
|
||||
$media->delete();
|
||||
$collection->runCallback('destroyed', $media->model->fresh());
|
||||
$collection->runCallback('after', $media->model->fresh());
|
||||
|
||||
return response()->json([]);
|
||||
}
|
||||
|
@ -137,42 +120,4 @@ class MediaController
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,21 +2,17 @@
|
|||
|
||||
namespace Zoomyboy\MedialibraryHelper;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Attributes\MapOutputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
#[MapOutputName(SnakeCaseMapper::class)]
|
||||
class MediaData extends Data
|
||||
{
|
||||
public ?int $id;
|
||||
public int $id;
|
||||
|
||||
public string $originalUrl;
|
||||
|
||||
|
@ -28,55 +24,11 @@ class MediaData extends Data
|
|||
|
||||
public string $fileName;
|
||||
|
||||
public string $mimeType;
|
||||
|
||||
#[MapInputName('custom_properties')]
|
||||
public array $properties;
|
||||
|
||||
public array $conversions = [];
|
||||
|
||||
public bool $fallback = false;
|
||||
|
||||
public static function fromMedia(Media $media): self
|
||||
{
|
||||
$conversions = collect($media->getMediaConversionNames())->flip()->map(fn ($integer, $conversion) => $media->hasGeneratedConversion($conversion)
|
||||
? ['original_url' => $media->getFullUrl($conversion)]
|
||||
: null,
|
||||
);
|
||||
|
||||
return self::withoutMagicalCreationFrom([
|
||||
...$media->toArray(),
|
||||
'conversions' => $conversions->toArray(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
$mime = Str::slug($this->mimeType);
|
||||
|
||||
return [
|
||||
'icon' => Storage::disk('public')->url("filetypes/{$mime}.svg"),
|
||||
];
|
||||
}
|
||||
|
||||
public static function defaultFromCollection(HasMedia $parent, MediaCollection $collection): ?self
|
||||
{
|
||||
$default = $collection->runCallback('withFallback', $parent);
|
||||
|
||||
if (is_null($default)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return static::from([
|
||||
'id' => null,
|
||||
'originalUrl' => Storage::disk($default[1])->url($default[0]),
|
||||
'size' => -1,
|
||||
'collection_name' => $collection->name,
|
||||
'name' => pathinfo($default[0], PATHINFO_FILENAME),
|
||||
'file_name' => pathinfo($default[0], PATHINFO_BASENAME),
|
||||
'properties' => [],
|
||||
'fallback' => true,
|
||||
'mime_type' => Storage::disk($default[1])->mimeType($default[0]),
|
||||
]);
|
||||
return self::withoutMagicalCreationFrom($media);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\MedialibraryHelper;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class OrderController
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
public function __invoke(Request $request, $parentModel, int $parentId, string $collectionName)
|
||||
{
|
||||
$mediaCount = collect($request->order)->map(function ($media) {
|
||||
$media = Media::findOrFail($media);
|
||||
|
||||
return $media->model_id.'_'.$media->model_type;
|
||||
})->unique()->count();
|
||||
|
||||
if (1 !== $mediaCount) {
|
||||
throw ValidationException::withMessages(['order' => 'Sortierung von verschiedenen Medien nicht möglich.']);
|
||||
}
|
||||
|
||||
$model = app('media-library-helpers')->get($parentModel);
|
||||
$model = $model::find($parentId);
|
||||
$this->authorize('listMedia', [$model, $collectionName]);
|
||||
|
||||
Media::setNewOrder($request->order);
|
||||
|
||||
$model->getMediaCollection($collectionName)->runCallback('after', $model->fresh());
|
||||
|
||||
return MediaData::collection($model->getMedia($collectionName));
|
||||
}
|
||||
}
|
|
@ -18,8 +18,7 @@ class ServiceProvider extends BaseServiceProvider
|
|||
app(Router::class)->group($this->routeGroup(), function ($router) {
|
||||
$router->post('mediaupload', [MediaController::class, 'store'])->name('media.store');
|
||||
$router->delete('mediaupload/{media}', [MediaController::class, 'destroy'])->name('media.destroy');
|
||||
$router->get('mediaupload/{parent_model}/{parent_id}/{collection_name}', [MediaController::class, 'index'])->name('media.index');
|
||||
$router->patch('mediaupload/{parent_model}/{parent_id}/{collection_name}', OrderController::class)->name('media.order');
|
||||
$router->get('mediaupload/{parent_model}/{parent_id}/{collection}', [MediaController::class, 'index'])->name('media.index');
|
||||
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
||||
});
|
||||
|
||||
|
@ -32,7 +31,7 @@ class ServiceProvider extends BaseServiceProvider
|
|||
protected function routeGroup(): array
|
||||
{
|
||||
return [
|
||||
'middleware' => config('media-library.middleware'),
|
||||
'middleware' => config('medialibrary-helper.middleware'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
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));
|
||||
});
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
test('it gets all medias', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
|
@ -17,25 +14,6 @@ test('it gets all medias', function () {
|
|||
$response->assertJsonPath('0.id', $firstMedia->id);
|
||||
$response->assertJsonPath('1.id', $secondMedia->id);
|
||||
$response->assertJsonPath('1.properties.test', 'old');
|
||||
$response->assertJsonPath('1.icon', url('storage/filetypes/applicationpdf.svg'));
|
||||
});
|
||||
|
||||
test('it gets media in order', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
|
||||
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
|
||||
$thirdMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
|
||||
$order = $post->getMedia('images')->pluck('id');
|
||||
$order->prepend($order->pop());
|
||||
Media::setNewOrder($order->toArray());
|
||||
|
||||
$response = $this->getJson("/mediaupload/post/{$post->id}/images");
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('0.id', $thirdMedia->id);
|
||||
$response->assertJsonPath('1.id', $firstMedia->id);
|
||||
$response->assertJsonPath('2.id', $secondMedia->id);
|
||||
});
|
||||
|
||||
test('it gets media for single', function () {
|
||||
|
@ -48,8 +26,6 @@ test('it gets media for single', function () {
|
|||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('id', $media->id);
|
||||
$response->assertJsonPath('properties.test', 'old');
|
||||
$response->assertJsonPath('fallback', false);
|
||||
$response->assertJsonPath('mime_type', 'application/pdf');
|
||||
});
|
||||
|
||||
test('it checks for authorization', function () {
|
||||
|
@ -70,30 +46,3 @@ test('it returns 404 when media not found', function () {
|
|||
|
||||
$response->assertStatus(404);
|
||||
});
|
||||
|
||||
test('test it gets conversions for single media', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$media = $post->addMedia($this->jpgFile()->getPathname())->preservingOriginal()->toMediaCollection('conversionsWithDefault');
|
||||
|
||||
$response = $this->getJson("/mediaupload/post/{$post->id}/conversionsWithDefault");
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('conversions.tiny.original_url', $media->getFullUrl('tiny'));
|
||||
});
|
||||
|
||||
test('test it gets default single media', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
Storage::disk('public')->put('default.jpg', $this->jpgFile()->getContent());
|
||||
|
||||
$response = $this->getJson("/mediaupload/post/{$post->id}/conversionsWithDefault");
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('id', null);
|
||||
$response->assertJsonPath('original_url', Storage::disk('public')->url('default.jpg'));
|
||||
$response->assertJsonPath('name', 'default');
|
||||
$response->assertJsonPath('file_name', 'default.jpg');
|
||||
$response->assertJsonPath('fallback', true);
|
||||
$response->assertJsonPath('mime_type', 'image/jpeg');
|
||||
});
|
||||
|
|
|
@ -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('medialibrary-helper.middleware', ['web', 'auth:web']);
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
|
||||
|
||||
test('it can reorder media', function () {
|
||||
Event::fake();
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$order = $post->getMedia('images')->pluck('id');
|
||||
$order->prepend($order->pop());
|
||||
|
||||
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('0.id', $order->get(0));
|
||||
$response->assertJsonPath('1.id', $order->get(1));
|
||||
$response->assertJsonPath('2.id', $order->get(2));
|
||||
$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 () {
|
||||
$this->auth()->registerModel();
|
||||
|
||||
$post = $this->newPost();
|
||||
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
|
||||
$post = $this->newPost();
|
||||
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$thirdMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
|
||||
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
|
||||
'order' => [$firstMedia->id, $secondMedia->id, $thirdMedia->id],
|
||||
]);
|
||||
|
||||
$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;
|
||||
|
||||
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 () {
|
||||
|
|
|
@ -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 () {
|
||||
|
@ -35,41 +34,6 @@ test('it uploads a single file to a single file collection', function () {
|
|||
$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 () {
|
||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||
$this->auth()->registerModel();
|
||||
|
@ -176,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 () {
|
||||
|
@ -249,14 +212,14 @@ test('it returns 403 when not authorized', function () {
|
|||
$post = $this->newPost();
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
'model' => 'post',
|
||||
'id' => $post->id,
|
||||
'collection' => 'defaultSingleFile',
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
]);
|
||||
'model' => 'post',
|
||||
'id' => $post->id,
|
||||
'collection' => 'defaultSingleFile',
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
|
|
@ -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
|
||||
|
@ -20,23 +18,16 @@ class Post extends Model implements HasMedia
|
|||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
|
||||
$this->addMediaCollection('defaultSingleFile')->singleFile();
|
||||
|
||||
$this->addMediaCollection('conversionsWithDefault')
|
||||
->singleFile()
|
||||
->withFallback(fn ($parent) => ['default.jpg', 'public'])
|
||||
->registerMediaConversions(function () {
|
||||
$this->addMediaConversion('tiny')->width(200)->height(200);
|
||||
});
|
||||
$this->addMediaCollection('images');
|
||||
|
||||
$this->addMediaCollection('images')->after(fn ($model) => Event::dispatch(new MediaChange($model)));
|
||||
|
||||
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
|
||||
return $name . ' ' . now()->format('Y-m-d');
|
||||
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($name) {
|
||||
return $name.' '.now()->format('Y-m-d');
|
||||
});
|
||||
|
||||
$this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) {
|
||||
return $name . ' ' . now()->format('Y-m-d');
|
||||
$this->addMediaCollection('multipleForced')->forceFileName(function ($name) {
|
||||
return $name.' '.now()->format('Y-m-d');
|
||||
});
|
||||
|
||||
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
|
||||
|
@ -48,19 +39,16 @@ 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));
|
||||
});
|
||||
|
||||
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path, $pathinfo) => [
|
||||
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
|
||||
'test' => Str::camel($path),
|
||||
])->withPropertyValidation(fn ($path) => [
|
||||
'test' => 'string|max:10',
|
||||
])
|
||||
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ class TestCase extends BaseTestCase
|
|||
*/
|
||||
protected function defineDatabaseMigrations(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(__DIR__ . '/migrations');
|
||||
$this->loadMigrationsFrom(__DIR__.'/migrations');
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app): array
|
||||
|
@ -37,15 +37,10 @@ class TestCase extends BaseTestCase
|
|||
return $this->getFile('pdf.pdf', $filename ?: 'pdf.pdf');
|
||||
}
|
||||
|
||||
protected function jpgFile(?string $filename = null): File
|
||||
{
|
||||
return $this->getFile('jpg.jpg', $filename ?: 'jpg.jpg');
|
||||
}
|
||||
|
||||
protected function getFile(string $location, string $as): File
|
||||
{
|
||||
$path = __DIR__ . '/stubs/' . $location;
|
||||
$to = sys_get_temp_dir() . '/' . $as;
|
||||
$path = __DIR__.'/stubs/'.$location;
|
||||
$to = sys_get_temp_dir().'/'.$as;
|
||||
copy($path, $to);
|
||||
|
||||
return new File($to);
|
||||
|
@ -74,7 +69,7 @@ class TestCase extends BaseTestCase
|
|||
];
|
||||
|
||||
foreach ($policies as $ability => $result) {
|
||||
Gate::define($ability, fn (?string $user, string $collectionName) => $result);
|
||||
Gate::define($ability, fn (?string $whatever) => $result);
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@ -82,6 +77,6 @@ class TestCase extends BaseTestCase
|
|||
|
||||
protected function defineEnvironment($app)
|
||||
{
|
||||
$app['config']->set('media-library.middleware', ['web']);
|
||||
$app['config']->set('medialibrary-helper.middleware', ['web']);
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 177 KiB |
Loading…
Reference in New Issue