Skip to content

Commit 515dde1

Browse files
Dammmienerquhart
authored andcommitted
Fix #783 duplicate uploading same asset name (#853)
1 parent 43a6c6f commit 515dde1

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/actions/mediaLibrary.js

+16
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ export function persistMedia(file, opts = {}) {
7878
const state = getState();
7979
const backend = currentBackend(state.config);
8080
const integration = selectIntegration(state, null, 'assetStore');
81+
const files = state.mediaLibrary.get('files');
82+
const existingFile = files.find(existingFile => existingFile.name.toLowerCase() === file.name.toLowerCase());
83+
84+
/**
85+
* Check for existing files of the same name before persisting. If no asset
86+
* store integration is used, files are being stored in Git, so we can
87+
* expect file names to be unique. If an asset store is in use, file names
88+
* may not be unique, so we forego this check.
89+
*/
90+
if (!integration && existingFile) {
91+
if (!window.confirm(`${existingFile.name} already exists. Do you want to replace it?`)) {
92+
return;
93+
} else {
94+
await dispatch(deleteMedia(existingFile, { privateUpload }));
95+
}
96+
}
8197

8298
dispatch(mediaPersisting());
8399

src/components/MediaLibrary/MediaLibrary.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,21 @@ class MediaLibrary extends React.Component {
112112
handlePersist = async event => {
113113
/**
114114
* Stop the browser from automatically handling the file input click, and
115-
* get the file for upload.
115+
* get the file for upload, and retain the synthetic event for access after
116+
* the asynchronous persist operation.
116117
*/
117118
event.stopPropagation();
118119
event.preventDefault();
120+
event.persist();
119121
const { persistMedia, privateUpload } = this.props;
120122
const { files: fileList } = event.dataTransfer || event.target;
121123
const files = [...fileList];
122124
const file = files[0];
123125

124-
/**
125-
* Upload the selected file, then refresh the media library. This should be
126-
* improved in the future, but isn't currently resulting in noticeable
127-
* performance/load time issues.
128-
*/
129126
await persistMedia(file, { privateUpload });
127+
128+
event.target.value = null;
129+
130130
this.scrollToTop();
131131
};
132132

0 commit comments

Comments
 (0)