Compare commits

...

7 Commits

Author SHA1 Message Date
philipp lang 4a87b83b5a Add pathinfo information to default Properties 2024-10-30 15:25:43 +01:00
philipp lang 09919a1c29 Add heic support 2024-04-30 14:13:21 +02:00
philipp lang 820a725517 Lint 2024-01-02 01:28:49 +01:00
philipp lang 2d39816954 Lint 2024-01-02 01:28:16 +01:00
philipp lang 7e8f762885 Lint 2024-01-02 01:27:56 +01:00
philipp lang 9c33c8f128 Update tests 2024-01-02 00:41:22 +01:00
philipp lang cfb38ed792 Update README 2024-01-02 00:22:42 +01:00
10 changed files with 3083 additions and 1032 deletions

28
README.md Normal file
View File

@ -0,0 +1,28 @@
# 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,6 +16,7 @@
} }
], ],
"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",
@ -35,5 +36,12 @@
"allow-plugins": { "allow-plugins": {
"pestphp/pest-plugin": true "pestphp/pest-plugin": true
} }
},
"extra": {
"laravel": {
"providers": [
"Zoomyboy\\MedialibraryHelper\\ServiceProvider"
]
}
} }
} }

3957
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,8 @@ 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);
@ -86,7 +87,7 @@ class CollectionExtension
'after' => fn ($event) => true, 'after' => fn ($event) => true,
'destroyed' => fn ($event) => true, 'destroyed' => fn ($event) => true,
'storing' => fn ($adder, $name) => $adder, 'storing' => fn ($adder, $name) => $adder,
'withDefaultProperties' => fn ($path) => [], 'withDefaultProperties' => fn ($path, $pathinfo) => [],
'withPropertyValidation' => fn ($path) => [], 'withPropertyValidation' => fn ($path) => [],
'withFallback' => fn ($parent) => null, 'withFallback' => fn ($parent) => null,
]); ]);

View File

@ -2,6 +2,7 @@
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;
@ -53,7 +54,7 @@ class MediaController
$adder = $this->fileAdderFromData($model, $c['content'], $collection) $adder = $this->fileAdderFromData($model, $c['content'], $collection)
->usingName($basename) ->usingName($basename)
->usingFileName($path) ->usingFileName($path)
->withCustomProperties($collection->runCallback('withDefaultProperties', $path)); ->withCustomProperties($collection->runCallback('withDefaultProperties', $path, $pathinfo));
return tap( return tap(
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name), $collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
@ -160,6 +161,14 @@ class MediaController
$tmpFile = tempnam(sys_get_temp_dir(), 'media-library'); $tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
file_put_contents($tmpFile, $binaryData); 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)) { if (null !== $maxWidth && 'image/jpeg' === mime_content_type($tmpFile)) {
Image::load($tmpFile)->width($maxWidth)->save(); Image::load($tmpFile)->width($maxWidth)->save();
} }

View File

@ -1,44 +0,0 @@
<?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,6 +35,22 @@ 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 () { test('it uploads a single image to a single file collection', function () {
$this->auth()->registerModel(); $this->auth()->registerModel();
$post = $this->newPost(); $post = $this->newPost();

View File

@ -56,7 +56,7 @@ class Post extends Model implements HasMedia
Event::dispatch(new MediaStored($media)); Event::dispatch(new MediaStored($media));
}); });
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [ $this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path, $pathinfo) => [
'test' => Str::camel($path), 'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [ ])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10', 'test' => 'string|max:10',

BIN
tests/stubs/heic.jpg Normal file

Binary file not shown.