Compare commits

..

No commits in common. "master" and "1.11.0" have entirely different histories.

10 changed files with 1008 additions and 3059 deletions

View File

@ -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) {
....
})
```

View File

@ -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"
]
}
}
}

3909
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -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);
@ -87,7 +86,7 @@ class CollectionExtension
'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,
]);

View File

@ -2,7 +2,6 @@
namespace Zoomyboy\MedialibraryHelper;
use Imagick;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@ -49,12 +48,12 @@ 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'];
$path = $basename.'.'.$pathinfo['extension'];
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
->usingName($basename)
->usingFileName($path)
->withCustomProperties($collection->runCallback('withDefaultProperties', $path, $pathinfo));
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
return tap(
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
@ -161,14 +160,6 @@ class MediaController
$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();
}

View File

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

View File

@ -35,22 +35,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();
@ -249,14 +233,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);
});

View File

@ -23,20 +23,20 @@ class Post extends Model implements HasMedia
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
$this->addMediaCollection('conversionsWithDefault')
->singleFile()
->withFallback(fn ($parent) => ['default.jpg', 'public'])
->registerMediaConversions(function () {
$this->addMediaConversion('tiny')->width(200)->height(200);
});
->singleFile()
->withFallback(fn ($parent) => ['default.jpg', 'public'])
->registerMediaConversions(function () {
$this->addMediaConversion('tiny')->width(200)->height(200);
});
$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');
return $name.' '.now()->format('Y-m-d');
});
$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) {
@ -49,18 +49,18 @@ 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)));
->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)));
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
}
}

View File

@ -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
@ -44,8 +44,8 @@ class TestCase extends BaseTestCase
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);

Binary file not shown.