From 46d28877d99fa3656f8decb934a6476e1bcee2b2 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 13 Apr 2024 22:30:45 +0200 Subject: [PATCH] Add storing for multiple deferred uploads --- src/DefersUploads.php | 18 +++++++++++++++--- tests/Feature/DeferredUploadTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/DefersUploads.php b/src/DefersUploads.php index 8022175..9bfed02 100644 --- a/src/DefersUploads.php +++ b/src/DefersUploads.php @@ -9,11 +9,23 @@ trait DefersUploads { public function setDeferredUploads(array $uploads): void { - $collection = $this->getMediaCollection($uploads['collection_name']); - $file = new MediaFile($this->storage()->path($uploads['file_name'])); + if (array_key_exists('collection_name', $uploads)) { + $uploads = [$uploads]; + } + + foreach ($uploads as $upload) { + $this->runMediaUploadForSingleFile($upload); + } + } + + protected function runMediaUploadForSingleFile(array $fileData): void + { + $collection = $this->getMediaCollection($fileData['collection_name']); + + $file = new MediaFile($this->storage()->path($fileData['file_name'])); $file->setBasename($collection->runCallback('forceFileName', $this, $file->getBasename())); - $adder = $this->addMediaFromDisk('media-library/' . $uploads['file_name'], config('media-library.temp_disk')) + $adder = $this->addMediaFromDisk('media-library/' . $fileData['file_name'], config('media-library.temp_disk')) ->usingName($file->getBasename()) ->usingFileName($file->getFilename()) ->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename())); diff --git a/tests/Feature/DeferredUploadTest.php b/tests/Feature/DeferredUploadTest.php index 7d5a1b3..03948df 100644 --- a/tests/Feature/DeferredUploadTest.php +++ b/tests/Feature/DeferredUploadTest.php @@ -44,6 +44,33 @@ test('it stores a file to media library after deferred upload', function () { Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); }); +test('it stores multiple files to media library after deferred upload', function () { + Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00')); + $this->auth()->registerModel()->withoutExceptionHandling(); + Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent()); + Storage::disk('temp')->put('media-library/beispiel2.pdf', $this->pdfFile()->getContent()); + + $post = $this->newPost(); + + $post->setDeferredUploads([ + [ + 'file_name' => 'beispiel.pdf', + 'collection_name' => 'multipleForced', + ], + [ + 'file_name' => 'beispiel2.pdf', + 'collection_name' => 'multipleForced', + ] + ]); + + $medias = $post->getMedia('multipleForced'); + $this->assertCount(2, $medias); + $this->assertEquals('beispiel 2023-05-06', $medias->get(0)->name); + $this->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name); + Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); + Storage::disk('temp')->assertMissing('media-library/beispiel2.pdf'); +}); + test('it uploads multiple files', function () { $this->auth()->registerModel()->withoutExceptionHandling(); $content = base64_encode($this->pdfFile()->getContent());