Add storing for multiple deferred uploads

This commit is contained in:
philipp lang 2024-04-13 22:30:45 +02:00
parent 91e5cc3e3b
commit 46d28877d9
2 changed files with 42 additions and 3 deletions

View File

@ -9,11 +9,23 @@ trait DefersUploads
{ {
public function setDeferredUploads(array $uploads): void public function setDeferredUploads(array $uploads): void
{ {
$collection = $this->getMediaCollection($uploads['collection_name']); if (array_key_exists('collection_name', $uploads)) {
$file = new MediaFile($this->storage()->path($uploads['file_name'])); $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())); $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()) ->usingName($file->getBasename())
->usingFileName($file->getFilename()) ->usingFileName($file->getFilename())
->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename())); ->withCustomProperties($collection->runCallback('withDefaultProperties', $file->getFilename()));

View File

@ -44,6 +44,33 @@ test('it stores a file to media library after deferred upload', function () {
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 () {
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 () { test('it uploads multiple files', function () {
$this->auth()->registerModel()->withoutExceptionHandling(); $this->auth()->registerModel()->withoutExceptionHandling();
$content = base64_encode($this->pdfFile()->getContent()); $content = base64_encode($this->pdfFile()->getContent());