Skip to content

Commit f2d0271

Browse files
committed
Merge branch '783-replace-media' of https://github.com/Dammmien/netlify-cms into 783-replace-media
2 parents d847d94 + dc6807d commit f2d0271

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/actions/mediaLibrary.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,34 @@ export function loadMedia(opts = {}) {
7373
}
7474

7575
export function persistMedia(file, opts = {}) {
76-
const { privateUpload, existingFile } = opts;
76+
const { privateUpload } = opts;
7777
return async (dispatch, getState) => {
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 === file.name);
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(`${file.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

84100
try {
85101
const assetProxy = await createAssetProxy(file.name.toLowerCase(), file, false, privateUpload);
86102
dispatch(addAsset(assetProxy));
87103
if (!integration) {
88-
if (existingFile) {
89-
if (!window.confirm('This media already exist, do you want to replace it?')) {
90-
return dispatch(mediaPersistFailed({ privateUpload }));
91-
}
92-
93-
dispatch(deleteMedia(existingFile, { privateUpload }));
94-
}
95-
96104
const asset = await backend.persistMedia(assetProxy);
97105
return dispatch(mediaPersisted(asset));
98106
}

src/components/MediaLibrary/MediaLibrary.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,8 @@ class MediaLibrary extends React.Component {
120120
const { files: fileList } = event.dataTransfer || event.target;
121121
const files = [...fileList];
122122
const file = files[0];
123-
const existingFile = this.props.files.find((existingFile) => existingFile.name === file.name);
124123

125-
/**
126-
* Upload the selected file, then refresh the media library. This should be
127-
* improved in the future, but isn't currently resulting in noticeable
128-
* performance/load time issues.
129-
*/
130-
await persistMedia(file, { privateUpload, existingFile });
131-
132-
// Reset input value
133-
event.target.value = '';
124+
await persistMedia(file, { privateUpload });
134125

135126
this.scrollToTop();
136127
};

0 commit comments

Comments
 (0)