From 528fe8a207e3809a8c9538c9d825650e5236d265 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 21 Oct 2024 11:45:39 -0400 Subject: [PATCH 1/2] only show fixes when its a duplicate --- resources/js/components/assets/Upload.vue | 2 +- resources/js/components/assets/Uploader.vue | 2 +- src/Http/Controllers/CP/Assets/AssetsController.php | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/resources/js/components/assets/Upload.vue b/resources/js/components/assets/Upload.vue index 5e36e0d159..8fb5b5a3b7 100644 --- a/resources/js/components/assets/Upload.vue +++ b/resources/js/components/assets/Upload.vue @@ -21,7 +21,7 @@
{{ error }} - + diff --git a/resources/js/components/assets/Uploader.vue b/resources/js/components/assets/Uploader.vue index ea8536ed5c..7a5439e12d 100644 --- a/resources/js/components/assets/Uploader.vue +++ b/resources/js/components/assets/Uploader.vue @@ -213,7 +213,7 @@ export default { msg = __('Upload failed. The file might be larger than is allowed by your server.'); } } else { - if (status === 422) { + if ([422, 409].includes(status)) { msg = Object.values(response.errors)[0][0]; // Get first validation message. } } diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index d755a5cc7f..2fb941d661 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\ValidationException; use Statamic\Assets\AssetUploader; use Statamic\Assets\UploadedReplacementFile; use Statamic\Contracts\Assets\Asset as AssetContract; @@ -97,7 +98,11 @@ public function store(Request $request) $validator = Validator::make(['path' => $path], ['path' => new UploadableAssetPath($container)]); if (! in_array($request->option, ['timestamp', 'overwrite'])) { - $validator->validate(); + try { + $validator->validate(); + } catch (ValidationException $e) { + throw $e->status(409); + } } $asset = $container->asset($path) ?? $container->makeAsset($path); From 84a00c3e0b014aca97785c63ccb7c89a25e7dc7a Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 21 Oct 2024 13:17:35 -0400 Subject: [PATCH 2/2] fix test --- tests/Feature/Assets/StoreAssetTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Assets/StoreAssetTest.php b/tests/Feature/Assets/StoreAssetTest.php index a48dac5079..392988fa7a 100644 --- a/tests/Feature/Assets/StoreAssetTest.php +++ b/tests/Feature/Assets/StoreAssetTest.php @@ -115,7 +115,7 @@ public function it_doesnt_upload_when_file_exists() $this ->actingAs($this->userWithPermission()) ->submit() - ->assertStatus(422) + ->assertStatus(409) ->assertInvalid(['path' => 'A file already exists with this name.']); } @@ -130,7 +130,7 @@ public function it_doesnt_upload_when_file_exists_with_different_casing() ->submit([ 'file' => UploadedFile::fake()->image('tEsT.jpg'), ]) - ->assertStatus(422) + ->assertStatus(409) ->assertInvalid(['path' => 'A file already exists with this name.']); }