Skip to content
Open
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
5 changes: 1 addition & 4 deletions resources/js/components/assets/Browser/Browser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,7 @@ export default {
},

existingUploadSelected(upload) {
const path = `${this.folder.path}/${upload.basename}`.replace(/^\/+/, '');
const id = `${this.container.id}::${path}`;

this.selectedAssets.push(id);
this.selectedAssets.push(upload.existingId);
this.$emit('selections-updated', this.selectedAssets);
},

Expand Down
1 change: 1 addition & 0 deletions resources/js/components/assets/Uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export default {

upload.errorMessage = msg;
upload.errorStatus = status;
upload.existingId = response?.existing;
this.$emit('error', upload, this.uploads);
this.processUploadQueue();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,7 @@ export default {
},

uploadSelected(upload) {
const path = `${this.folder}/${upload.basename}`.replace(/^\/+/, '');
const id = `${this.container.id}::${path}`;
const id = upload.existingId;

this.uploads.splice(this.uploads.indexOf(upload), 1);

Expand Down
15 changes: 3 additions & 12 deletions resources/js/tests/components/fieldtypes/AssetsFieldtype.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@ const makeField = ({ axiosPost = vi.fn(() => Promise.resolve({ data: [] })), pro
});
};

test('selecting the existing file references it by the container id', () => {
const post = vi.fn(() => Promise.resolve({ data: [] }));
const field = makeField({ axiosPost: post });

field.vm.uploadSelected({ basename: 'user.png' });

expect(post).toHaveBeenCalledWith('assets-fieldtype', { assets: ['main::user.png'] });
});

test('the existing file id includes the configured folder', () => {
test('selecting the existing file references the asset the server reported', () => {
const post = vi.fn(() => Promise.resolve({ data: [] }));
const field = makeField({ axiosPost: post, props: { config: { folder: 'sub' } } });

field.vm.uploadSelected({ basename: 'user.png' });
field.vm.uploadSelected({ basename: 'My File.PNG', existingId: 'main::sub/my-file.png' });

expect(post).toHaveBeenCalledWith('assets-fieldtype', { assets: ['main::sub/user.png'] });
expect(post).toHaveBeenCalledWith('assets-fieldtype', { assets: ['main::sub/my-file.png'] });
});

test('loading assets for a new value does not emit an update when the ids are unchanged', async () => {
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 @@ -124,7 +124,12 @@ public function store(Request $request)
try {
$validator->validate();
} catch (ValidationException $e) {
throw $e->status(409);
// The sanitized path can differ from the uploaded filename, so tell the uploader which asset exists.
return response()->json([
'message' => __('The given data was invalid.'),
'errors' => $e->errors(),
'existing' => $container->asset($path)->id(),
], 409);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Assets/StoreAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public function it_doesnt_upload_when_file_exists_with_different_casing()
'file' => UploadedFile::fake()->image('tEsT.jpg'),
])
->assertStatus(409)
->assertInvalid(['path' => 'A file already exists with this name.']);
->assertInvalid(['path' => 'A file already exists with this name.'])
->assertJson(['message' => 'The given data was invalid.', 'existing' => 'test_container::path/to/test.jpg']);
}

#[Test]
Expand Down
Loading