Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Avoid showing asset upload fixes when inappropriate #10986

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading