Add multiple files deferred

This commit is contained in:
philipp lang 2024-01-03 16:45:02 +01:00
parent 8bf17784cc
commit 7c33160b13
2 changed files with 64 additions and 8 deletions

View File

@ -3,14 +3,13 @@
namespace Zoomyboy\MedialibraryHelper; namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName; use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Data; use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper; use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\MediaLibrary\HasMedia; use Illuminate\Support\Collection;
use Spatie\MediaLibrary\MediaCollections\MediaCollection; use Spatie\MediaLibrary\MediaCollections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\LaravelData\DataCollection;
#[MapInputName(SnakeCaseMapper::class)] #[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)] #[MapOutputName(SnakeCaseMapper::class)]
@ -47,4 +46,9 @@ class DeferredMediaData extends Data
'path' => $path, 'path' => $path,
]); ]);
} }
public static function collectionFromPaths(Collection $paths, MediaCollection $collection): DataCollection
{
return static::collection($paths->map(fn ($path) => static::fromPath($path, $collection)));
}
} }

View File

@ -12,7 +12,7 @@ test('it uploads a deferred file to a collection', function () {
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null], 'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null],
'payload' => [ 'payload' => [
'content' => $content, 'content' => $content,
'name' => 'beispiel bild.jpg', 'name' => 'beispiel.pdf',
], ],
]; ];
@ -20,14 +20,66 @@ test('it uploads a deferred file to a collection', function () {
->assertStatus(201) ->assertStatus(201)
->assertJson([ ->assertJson([
'is_deferred' => true, 'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel bild.jpg'), 'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel bild', 'name' => 'beispiel',
'collection_name' => 'defaultSingleFile', 'collection_name' => 'defaultSingleFile',
'size' => 3028, 'size' => 3028,
'file_name' => 'beispiel bild.jpg', 'file_name' => 'beispiel.pdf',
'mime_type' => 'application/pdf', 'mime_type' => 'application/pdf',
]); ]);
Storage::disk('temp')->assertExists('media-library/beispiel bild.jpg'); Storage::disk('temp')->assertExists('media-library/beispiel.pdf');
});
test('it uploads multiple files', function () {
$this->auth()->registerModel()->withoutExceptionHandling();
$content = base64_encode($this->pdfFile()->getContent());
$payload = [
'parent' => ['model' => 'post', 'collection' => 'multipleForced', 'id' => null],
'payload' => [
['content' => $content, 'name' => 'beispiel.pdf'],
['content' => $content, 'name' => 'beispiel2.pdf'],
]
];
$this->postJson('/mediaupload', $payload)
->assertStatus(201)
->assertJson([
[
'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel',
'collection_name' => 'multipleForced',
'size' => 3028,
'file_name' => 'beispiel.pdf',
'mime_type' => 'application/pdf',
],
[
'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'),
'name' => 'beispiel2',
'collection_name' => 'multipleForced',
'size' => 3028,
'file_name' => 'beispiel2.pdf',
'mime_type' => 'application/pdf',
]
]);
Storage::disk('temp')->assertExists('media-library/beispiel.pdf');
Storage::disk('temp')->assertExists('media-library/beispiel2.pdf');
});
test('it reduces file size', function () {
$this->auth()->registerModel()->withoutExceptionHandling();
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => null],
'payload' => [
'content' => base64_encode($this->jpgFile()->getContent()),
'name' => 'beispiel bild.jpg',
],
]);
$size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg'));
$this->assertEquals(250, $size[0]);
}); });
test('it handles authorization with collection', function () { test('it handles authorization with collection', function () {