Compare commits

...

2 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
7 changed files with 1488 additions and 700 deletions

View File

@ -16,6 +16,7 @@
}
],
"require": {
"ext-imagick": ">=3.6.0",
"spatie/laravel-medialibrary": "^10.7",
"laravel/framework": "^9.50",
"spatie/laravel-data": "^3.1",

2153
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
{
MediaCollection::mixin(new class() {
MediaCollection::mixin(new class()
{
public function forceFileName()
{
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
@ -86,7 +87,7 @@ class CollectionExtension
'after' => fn ($event) => true,
'destroyed' => fn ($event) => true,
'storing' => fn ($adder, $name) => $adder,
'withDefaultProperties' => fn ($path) => [],
'withDefaultProperties' => fn ($path, $pathinfo) => [],
'withPropertyValidation' => fn ($path) => [],
'withFallback' => fn ($parent) => null,
]);

View File

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

@ -35,6 +35,22 @@ 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();

View File

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

BIN
tests/stubs/heic.jpg Normal file

Binary file not shown.