Update media-library version

This commit is contained in:
philipp lang 2024-09-21 16:54:51 +02:00
parent 59d7647720
commit b7a617aa62
8 changed files with 2339 additions and 1568 deletions

View File

@ -22,15 +22,14 @@
} }
], ],
"require": { "require": {
"spatie/laravel-medialibrary": "^10.7", "spatie/laravel-medialibrary": "^11.0",
"laravel/framework": "^9.50", "laravel/framework": "^10.0",
"spatie/laravel-data": "^3.1", "spatie/laravel-data": "^3.1",
"pestphp/pest": "^1.22" "pestphp/pest": "^1.22"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.6", "orchestra/testbench": "^8.0",
"orchestra/testbench": "^7.0", "illuminate/console": "^10.0"
"illuminate/console": "^9.2"
}, },
"scripts": { "scripts": {
"post-autoload-dump": [ "post-autoload-dump": [

3619
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -82,7 +82,7 @@ class CollectionExtension
$this->convertTo = null; $this->convertTo = null;
$this->customCallbacks = collect([ $this->customCallbacks = collect([
'forceFileName' => fn ($model, $name) => $name, 'forceFileName' => fn ($model, $name) => $name,
'convert' => fn () => null, 'convert' => fn ($extension) => $extension,
'maxWidth' => fn ($size) => null, 'maxWidth' => fn ($size) => null,
'stored' => fn ($event) => true, 'stored' => fn ($event) => true,
'after' => fn ($event) => true, 'after' => fn ($event) => true,

View File

@ -13,6 +13,7 @@ use Spatie\MediaLibrary\MediaCollections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Zoomyboy\MedialibraryHelper\Rules\ModelRule; use Zoomyboy\MedialibraryHelper\Rules\ModelRule;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\File;
class MediaController class MediaController
{ {
@ -49,10 +50,9 @@ class MediaController
$medias = collect($content)->map(function ($c) use ($collection, $model) { $medias = collect($content)->map(function ($c) use ($collection, $model) {
$file = new MediaFile($c['name']); $file = new MediaFile($c['name']);
$file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename())); $file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename()));
$file->setExtension(MediaFile::extensionFromData($c['content']));
if ($collection->runCallback('convert') !== null) { $file->setExtension($collection->runCallback('convert', $file->getExtension()));
$file->setExtension($collection->runCallback('convert'));
}
$adder = $this->fileAdderFromData($model, $c['content'], $collection) $adder = $this->fileAdderFromData($model, $c['content'], $collection)
->usingName($file->getBasename()) ->usingName($file->getBasename())
->usingFileName($file->getFilename()) ->usingFileName($file->getFilename())
@ -90,11 +90,9 @@ class MediaController
$tempPaths = collect($content)->map(function ($c) use ($collection) { $tempPaths = collect($content)->map(function ($c) use ($collection) {
$file = new MediaFile($c['name']); $file = new MediaFile($c['name']);
$file->setExtension(MediaFile::extensionFromData($c['content']));
$file->setExtension($collection->runCallback('convert', $file->getExtension()));
$tmpFile = $this->storeTemporaryFile($c['content'], $collection); $tmpFile = $this->storeTemporaryFile($c['content'], $collection);
if ($collection->runCallback('convert') !== null) {
$file->setExtension($collection->runCallback('convert'));
}
Storage::disk(config('media-library.temp_disk'))->move($tmpFile, 'media-library/' . $file->getFilename()); Storage::disk(config('media-library.temp_disk'))->move($tmpFile, 'media-library/' . $file->getFilename());
return 'media-library/' . $file->getFilename(); return 'media-library/' . $file->getFilename();
}); });
@ -174,17 +172,15 @@ class MediaController
throw InvalidBase64Data::create(); throw InvalidBase64Data::create();
} }
$tmpFile = 'media-library/' . str()->uuid()->toString(); $tmpFile = 'media-library/' . str()->uuid()->toString() . '.' . $collection->runCallback('convert', MediaFile::extensionFromData($data));
Storage::disk(config('media-library.temp_disk'))->put($tmpFile, $binaryData); Storage::disk(config('media-library.temp_disk'))->put($tmpFile, $binaryData);
$image = Image::load(Storage::disk(config('media-library.temp_disk'))->path($tmpFile)); $imagePath = Storage::disk(config('media-library.temp_disk'))->path($tmpFile);
$image = Image::load($imagePath);
if (null !== $maxWidth && 'image/jpeg' === Storage::disk(config('media-library.temp_disk'))->mimeType($tmpFile)) { if (null !== $maxWidth && 'image/jpeg' === Storage::disk(config('media-library.temp_disk'))->mimeType($tmpFile)) {
$image->width($maxWidth); $image->width($maxWidth);
} }
if ($collection->runCallback('convert') !== null) {
$image->format($collection->runCallback('convert'));
}
$image->save(); $image->save();
return $tmpFile; return $tmpFile;

View File

@ -35,4 +35,17 @@ class MediaFile
{ {
return $this->file->{$method}(...$arguments); return $this->file->{$method}(...$arguments);
} }
public static function extensionFromData(string $data): string
{
$tempFile = sys_get_temp_dir() . '/' . str()->uuid()->toString();
file_put_contents($tempFile, base64_decode($data));
$extension = (new self($tempFile))->guessExtension();
unlink($tempFile);
return $extension;
}
} }

View File

@ -8,9 +8,9 @@ use Illuminate\Support\Facades\Storage;
test('it uploads a deferred file to a collection', function () { test('it uploads a deferred file to a collection', function () {
$this->auth()->registerModel()->withoutExceptionHandling(); $this->auth()->registerModel()->withoutExceptionHandling();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
'payload' => ['content' => base64_encode($this->pdfFile()->getContent()), 'name' => 'beispiel.pdf'], 'payload' => ['content' => base64_encode(test()->pdfFile()->getContent()), 'name' => 'beispiel.pdf'],
]) ])
->assertStatus(201) ->assertStatus(201)
->assertExactJson([ ->assertExactJson([
@ -18,7 +18,7 @@ test('it uploads a deferred file to a collection', function () {
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'), 'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel', 'name' => 'beispiel',
'collection_name' => 'defaultSingleFile', 'collection_name' => 'defaultSingleFile',
'size' => 3028, 'size' => 64576,
'file_name' => 'beispiel.pdf', 'file_name' => 'beispiel.pdf',
'mime_type' => 'application/pdf', 'mime_type' => 'application/pdf',
'icon' => url('storage/filetypes/applicationpdf.svg'), 'icon' => url('storage/filetypes/applicationpdf.svg'),
@ -27,11 +27,11 @@ test('it uploads a deferred file to a collection', function () {
}); });
test('it forces filename when uploading', function () { test('it forces filename when uploading', function () {
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => null],
'payload' => ['content' => base64_encode($this->pngFile()->getContent()), 'name' => 'beispiel.png'], 'payload' => ['content' => base64_encode(test()->pngFile()->getContent()), 'name' => 'beispiel.png'],
]) ])
->assertStatus(201) ->assertStatus(201)
->assertExactJson([ ->assertExactJson([
@ -39,7 +39,7 @@ test('it forces filename when uploading', function () {
'original_url' => Storage::disk('temp')->url('media-library/beispiel.jpg'), 'original_url' => Storage::disk('temp')->url('media-library/beispiel.jpg'),
'name' => 'beispiel', 'name' => 'beispiel',
'collection_name' => 'singleJpegFile', 'collection_name' => 'singleJpegFile',
'size' => 366471, 'size' => 490278,
'file_name' => 'beispiel.jpg', 'file_name' => 'beispiel.jpg',
'mime_type' => 'image/jpeg', 'mime_type' => 'image/jpeg',
'icon' => url('storage/filetypes/imagejpeg.svg'), 'icon' => url('storage/filetypes/imagejpeg.svg'),
@ -50,10 +50,10 @@ test('it forces filename when uploading', function () {
test('it stores a file to media library after deferred upload', function () { test('it stores a file to media library after deferred upload', function () {
Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00')); Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00'));
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent()); Storage::disk('temp')->put('media-library/beispiel.pdf', test()->pdfFile()->getContent());
$post = $this->newPost(); $post = test()->newPost();
$post->setDeferredUploads([ $post->setDeferredUploads([
'file_name' => 'beispiel.pdf', 'file_name' => 'beispiel.pdf',
@ -61,18 +61,18 @@ test('it stores a file to media library after deferred upload', function () {
]); ]);
$media = $post->getMedia('singleForced')->first(); $media = $post->getMedia('singleForced')->first();
$this->assertNotNull($media); test()->assertNotNull($media);
$this->assertEquals('beispiel 2023-05-06', $media->name); test()->assertEquals('beispiel 2023-05-06', $media->name);
Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); Storage::disk('temp')->assertMissing('media-library/beispiel.pdf');
}); });
test('it stores multiple files to media library after deferred upload', function () { test('it stores multiple files to media library after deferred upload', function () {
Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00')); Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00'));
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent()); Storage::disk('temp')->put('media-library/beispiel.pdf', test()->pdfFile()->getContent());
Storage::disk('temp')->put('media-library/beispiel2.pdf', $this->pdfFile()->getContent()); Storage::disk('temp')->put('media-library/beispiel2.pdf', test()->pdfFile()->getContent());
$post = $this->newPost(); $post = test()->newPost();
$post->setDeferredUploads([ $post->setDeferredUploads([
[ [
@ -86,16 +86,16 @@ test('it stores multiple files to media library after deferred upload', function
]); ]);
$medias = $post->getMedia('multipleForced'); $medias = $post->getMedia('multipleForced');
$this->assertCount(2, $medias); test()->assertCount(2, $medias);
$this->assertEquals('beispiel 2023-05-06', $medias->get(0)->name); test()->assertEquals('beispiel 2023-05-06', $medias->get(0)->name);
$this->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name); test()->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name);
Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); Storage::disk('temp')->assertMissing('media-library/beispiel.pdf');
Storage::disk('temp')->assertMissing('media-library/beispiel2.pdf'); Storage::disk('temp')->assertMissing('media-library/beispiel2.pdf');
}); });
test('it uploads multiple files', function () { test('it uploads multiple files', function () {
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$payload = [ $payload = [
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => null],
@ -105,7 +105,7 @@ test('it uploads multiple files', function () {
] ]
]; ];
$this->postJson('/mediaupload', $payload) test()->postJson('/mediaupload', $payload)
->assertStatus(201) ->assertStatus(201)
->assertJson([ ->assertJson([
[ [
@ -113,7 +113,7 @@ test('it uploads multiple files', function () {
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'), 'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel', 'name' => 'beispiel',
'collection_name' => 'multipleForced', 'collection_name' => 'multipleForced',
'size' => 3028, 'size' => 64576,
'file_name' => 'beispiel.pdf', 'file_name' => 'beispiel.pdf',
'mime_type' => 'application/pdf', 'mime_type' => 'application/pdf',
], ],
@ -122,7 +122,7 @@ test('it uploads multiple files', function () {
'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'), 'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'),
'name' => 'beispiel2', 'name' => 'beispiel2',
'collection_name' => 'multipleForced', 'collection_name' => 'multipleForced',
'size' => 3028, 'size' => 64576,
'file_name' => 'beispiel2.pdf', 'file_name' => 'beispiel2.pdf',
'mime_type' => 'application/pdf', 'mime_type' => 'application/pdf',
] ]
@ -132,24 +132,24 @@ test('it uploads multiple files', function () {
}); });
test('it reduces file size', function () { test('it reduces file size', function () {
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
'payload' => [ 'payload' => [
'content' => base64_encode($this->jpgFile()->getContent()), 'content' => base64_encode(test()->jpgFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
]); ]);
$size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg')); $size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg'));
$this->assertEquals(250, $size[0]); test()->assertEquals(250, $size[0]);
}); });
test('it handles authorization with collection', function () { test('it handles authorization with collection', function () {
$this->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel(); test()->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -159,10 +159,10 @@ test('it handles authorization with collection', function () {
}); });
test('it handles authorization with collection correctly', function () { test('it handles authorization with collection correctly', function () {
$this->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel(); test()->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -172,8 +172,8 @@ test('it handles authorization with collection correctly', function () {
}); });
test('it needs a collection', function ($key, $value) { test('it needs a collection', function ($key, $value) {
$this->auth()->registerModel(); test()->auth()->registerModel();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$payload = [ $payload = [
'parent' => ['model' => 'post', 'collection' => '', 'id' => null], 'parent' => ['model' => 'post', 'collection' => '', 'id' => null],
@ -185,7 +185,7 @@ test('it needs a collection', function ($key, $value) {
data_set($payload, $key, $value); data_set($payload, $key, $value);
$this->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key); test()->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key);
})->with(function () { })->with(function () {
yield ['parent.collection_name', '']; yield ['parent.collection_name', ''];
yield ['parent.collection_name', -1]; yield ['parent.collection_name', -1];

View File

@ -4,42 +4,44 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\Mime\MimeTypes;
use Workbench\App\Events\MediaChange; use Workbench\App\Events\MediaChange;
use Workbench\App\Events\MediaStored; use Workbench\App\Events\MediaStored;
test('it uploads a single file to a single file collection', function () { test('it uploads a single file to a single file collection', function () {
$this->auth()->registerModel(); test()->auth()->registerModel()->withoutExceptionHandling();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.pdf',
], ],
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$this->assertCount(1, $post->getMedia('defaultSingleFile')); test()->assertCount(1, $post->getMedia('defaultSingleFile'));
$media = $post->getFirstMedia('defaultSingleFile'); $media = $post->getFirstMedia('defaultSingleFile');
$response->assertJsonPath('id', $media->id); $response->assertJsonPath('id', $media->id);
$response->assertJsonPath('original_url', $media->getFullUrl()); $response->assertJsonPath('original_url', $media->getFullUrl());
$response->assertJsonPath('size', 3028); $response->assertJsonPath('size', 3028);
$response->assertJsonPath('name', 'beispiel bild'); $response->assertJsonPath('name', 'beispiel bild');
$response->assertJsonPath('collection_name', 'defaultSingleFile'); $response->assertJsonPath('collection_name', 'defaultSingleFile');
$response->assertJsonPath('file_name', 'beispiel-bild.jpg'); $response->assertJsonPath('file_name', 'beispiel-bild.pdf');
$response->assertJsonPath('is_deferred', false); $response->assertJsonPath('is_deferred', false);
$response->assertJsonMissingPath('model_type'); $response->assertJsonMissingPath('model_type');
$response->assertJsonMissingPath('model_id'); $response->assertJsonMissingPath('model_id');
}); });
test('it changes format', function () { test('it changes format', function () {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pngFile()->getContent()); $content = base64_encode(test()->pngFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -48,17 +50,18 @@ test('it changes format', function () {
]); ]);
$response->assertStatus(201) $response->assertStatus(201)
->assertJsonPath('size', 366471) ->assertJsonPath('size', 490278)
->assertJsonPath('file_name', 'beispiel-bild.jpg') ->assertJsonPath('file_name', 'beispiel-bild.jpg')
->assertJsonPath('mime_type', 'image/jpeg'); ->assertJsonPath('mime_type', 'image/jpeg');
$this->assertEquals('image/jpeg', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('singleJpegFile')->getPath()));
}); });
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()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->jpgFile()->getContent()); $content = base64_encode(test()->jpgFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -67,16 +70,16 @@ test('it uploads a single image to a single file collection', function () {
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$this->assertCount(1, $post->getMedia('defaultSingleFile')); test()->assertCount(1, $post->getMedia('defaultSingleFile'));
}); });
test('it forces a filename for a single collection', function () { test('it forces a filename for a single collection', function () {
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00')); Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->jpgFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleForced', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'singleForced', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -85,17 +88,17 @@ test('it forces a filename for a single collection', function () {
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name); test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
$response->assertJsonPath('name', 'beispiel bild 2023-04-04'); $response->assertJsonPath('name', 'beispiel bild 2023-04-04');
$response->assertJsonPath('file_name', 'beispiel-bild-2023-04-04.jpg'); $response->assertJsonPath('file_name', 'beispiel-bild-2023-04-04.jpg');
}); });
test('it sets custom title when storing', function () { test('it sets custom title when storing', function () {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleStoringHook', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'singleStoringHook', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -106,36 +109,36 @@ test('it sets custom title when storing', function () {
$response->assertStatus(201); $response->assertStatus(201);
$media = $post->getFirstMedia('singleStoringHook'); $media = $post->getFirstMedia('singleStoringHook');
$this->assertEquals('AAA', $media->getCustomProperty('use')); test()->assertEquals('AAA', $media->getCustomProperty('use'));
$this->assertEquals('beispiel bild', $media->getCustomProperty('ttt')); test()->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
}); });
test('it sets custom properties from properties method', function () { test('it sets custom properties from properties method', function () {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'multipleProperties', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'multipleProperties', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.pdf',
], ],
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$media = $post->getFirstMedia('multipleProperties'); $media = $post->getFirstMedia('multipleProperties');
$this->assertEquals('beispielBild.jpg', $media->getCustomProperty('test')); test()->assertEquals('beispielBild.pdf', $media->getCustomProperty('test'));
}); });
test('it forces a filename for multiple collections', function () { test('it forces a filename for multiple collections', function () {
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00')); Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->jpgFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => $post->id],
'payload' => [ 'payload' => [
[ [
@ -146,17 +149,17 @@ test('it forces a filename for multiple collections', function () {
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name); test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
}); });
test('it throws event when file has been uploaded', function () { test('it throws event when file has been uploaded', function () {
Event::fake(); Event::fake();
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00')); Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
@ -172,11 +175,11 @@ test('it throws event when file has been uploaded', function () {
test('it throws event when multiple files uploaded', function () { test('it throws event when multiple files uploaded', function () {
Event::fake(); Event::fake();
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00')); Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
$this->auth()->registerModel()->withoutExceptionHandling(); test()->auth()->registerModel()->withoutExceptionHandling();
$post = $this->newPost(); $post = test()->newPost();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode(test()->pdfFile()->getContent());
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'multipleFilesWithEvent', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'multipleFilesWithEvent', 'id' => $post->id],
'payload' => [ 'payload' => [
[ [
@ -196,104 +199,105 @@ test('it throws event when multiple files uploaded', function () {
}); });
test('it uploads multiple files', function () { test('it uploads multiple files', function () {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$file = $this->pdfFile(); $file = test()->pdfFile();
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images'); $post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
$response = $this->postJson('/mediaupload', [ $response = test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
'payload' => [ 'payload' => [
[ [
'content' => base64_encode($file->getContent()), 'content' => base64_encode($file->getContent()),
'name' => 'aaaa.jpg', 'name' => 'aaaa.pdf',
], ],
[ [
'content' => base64_encode($file->getContent()), 'content' => base64_encode($file->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.pdf',
], ],
], ],
]); ]);
$response->assertStatus(201); $response->assertStatus(201);
$this->assertCount(3, $post->getMedia('images')); test()->assertCount(3, $post->getMedia('images'));
$this->assertEquals('application/pdf', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('images')->getPath()));
$media = $post->getMedia('images')->skip(1)->values(); $media = $post->getMedia('images')->skip(1)->values();
$this->assertCount(2, $response->json()); test()->assertCount(2, $response->json());
$response->assertJsonPath('0.id', $media->get(0)->id); $response->assertJsonPath('0.id', $media->get(0)->id);
$response->assertJsonPath('1.id', $media->get(1)->id); $response->assertJsonPath('1.id', $media->get(1)->id);
$response->assertJsonPath('0.original_url', $media->first()->getFullUrl()); $response->assertJsonPath('0.original_url', $media->first()->getFullUrl());
$response->assertJsonPath('0.size', 3028); $response->assertJsonPath('0.size', 64576);
$response->assertJsonPath('0.name', 'aaaa'); $response->assertJsonPath('0.name', 'aaaa');
$response->assertJsonPath('0.collection_name', 'images'); $response->assertJsonPath('0.collection_name', 'images');
$response->assertJsonPath('0.file_name', 'aaaa.jpg'); $response->assertJsonPath('0.file_name', 'aaaa.pdf');
$response->assertJsonMissingPath('0.model_type'); $response->assertJsonMissingPath('0.model_type');
$response->assertJsonMissingPath('0.model_id'); $response->assertJsonMissingPath('0.model_id');
}); });
test('it returns 403 when not authorized', function () { test('it returns 403 when not authorized', function () {
$this->auth(['storeMedia' => false])->registerModel(); test()->auth(['storeMedia' => false])->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
])->assertStatus(403); ])->assertStatus(403);
}); });
test('it checks for model when running authorization', function () { test('it checks for model when running authorization', function () {
$otherPost = $this->newPost(); $otherPost = test()->newPost();
$post = $this->newPost(); $post = test()->newPost();
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel(); test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
])->assertStatus(201); ])->assertStatus(201);
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $otherPost->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $otherPost->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
])->assertStatus(403); ])->assertStatus(403);
}); });
test('it checks for collection when running authorization', function () { test('it checks for collection when running authorization', function () {
$post = $this->newPost(); $post = test()->newPost();
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel(); test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
])->assertStatus(201); ])->assertStatus(201);
$this->postJson('/mediaupload', [ test()->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
])->assertStatus(403); ])->assertStatus(403);
}); });
test('it needs validation for single files', function (array $payloadOverwrites, string $invalidFieldName) { test('it needs validation for single files', function (array $payloadOverwrites, string $invalidFieldName) {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$payload = [ $payload = [
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [ 'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
]; ];
@ -302,7 +306,7 @@ test('it needs validation for single files', function (array $payloadOverwrites,
data_set($payload, $key, $value); data_set($payload, $key, $value);
} }
$response = $this->postJson('/mediaupload', $payload); $response = test()->postJson('/mediaupload', $payload);
$response->assertStatus(422); $response->assertStatus(422);
$response->assertJsonValidationErrors($invalidFieldName); $response->assertJsonValidationErrors($invalidFieldName);
@ -324,14 +328,14 @@ test('it needs validation for single files', function (array $payloadOverwrites,
}); });
test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) { test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) {
$this->auth()->registerModel(); test()->auth()->registerModel();
$post = $this->newPost(); $post = test()->newPost();
$payload = [ $payload = [
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id], 'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
'payload' => [ 'payload' => [
[ [
'content' => base64_encode($this->pdfFile()->getContent()), 'content' => base64_encode(test()->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg', 'name' => 'beispiel bild.jpg',
], ],
], ],
@ -341,7 +345,7 @@ test('it needs validation for multiple files', function (array $payloadOverwrite
data_set($payload, $key, $value); data_set($payload, $key, $value);
} }
$response = $this->postJson('/mediaupload', $payload); $response = test()->postJson('/mediaupload', $payload);
$response->assertStatus(422); $response->assertStatus(422);

View File

@ -23,7 +23,7 @@ class Post extends Model implements HasMedia
public function registerMediaCollections(): void public function registerMediaCollections(): void
{ {
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile(); $this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
$this->addMediaCollection('singleJpegFile')->convert(fn () => 'jpg')->singleFile(); $this->addMediaCollection('singleJpegFile')->convert(fn ($extension) => 'jpg')->singleFile();
$this->addMediaCollection('conversionsWithDefault') $this->addMediaCollection('conversionsWithDefault')
->singleFile() ->singleFile()