Skip to content

Commit

Permalink
[5.x] Avoid showing asset upload fixes when inappropriate (#10986)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Oct 21, 2024
1 parent 6712ee1 commit 2bb4ae1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion resources/js/components/assets/Upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<div class="ml-4 px-2 flex items-center gap-2" v-if="status === 'error'">
{{ error }}
<dropdown-list v-if="errorStatus === 422">
<dropdown-list v-if="errorStatus === 409">
<template #trigger>
<button class="ml-4 btn btn-xs" v-text="`${__('Fix')}...`" />
</template>
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/assets/Uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controllers/CP/Assets/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Assets/StoreAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
}

Expand All @@ -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.']);
}

Expand Down

0 comments on commit 2bb4ae1

Please sign in to comment.