Skip to content

Commit

Permalink
Fix data corruption bug when storage disk becomes invalid (#3139)
Browse files Browse the repository at this point in the history
* Limit blast radius and not skip last line of test

* Fix bug removing all media not just current
  • Loading branch information
Pilskalns authored Dec 30, 2022
1 parent 9e1e1ee commit e2f1f76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/MediaCollections/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected function processMediaItem(HasMedia $model, Media $media, self $fileAdd
}

if (! $addedMediaSuccessfully) {
$model->media()->delete($media->id);
$media->delete();

throw DiskCannotBeAccessed::create($media->disk);
}
Expand Down
19 changes: 14 additions & 5 deletions tests/Feature/FileAdder/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,13 @@
});

it('will throw and exception and not create a record in database if file cannot be added', function () {

$this->testModel
->addMedia($this->getTestPng())
->toMediaCollection();

expect(Media::count())->toBe(1);

config()->set('filesystems.disks.invalid_disk', [
'driver' => 's3',
'secret' => 'test',
Expand All @@ -606,9 +613,11 @@
'bucket' => 'test',
]);

$this->testModel
->addMedia($this->getTestJpg())
->toMediaCollection('default', 'invalid_disk');
expect(
fn () => $this->testModel
->addMedia($this->getTestJpg())
->toMediaCollection('default', 'invalid_disk')
)->toThrow(DiskCannotBeAccessed::class);

expect(Media::count())->toBe(0);
})->throws(DiskCannotBeAccessed::class);
expect(Media::count())->toBe(1);
});

0 comments on commit e2f1f76

Please sign in to comment.