@@ -73,26 +73,34 @@ export function loadMedia(opts = {}) {
73
73
}
74
74
75
75
export function persistMedia ( file , opts = { } ) {
76
- const { privateUpload, existingFile } = opts ;
76
+ const { privateUpload } = opts ;
77
77
return async ( dispatch , getState ) => {
78
78
const state = getState ( ) ;
79
79
const backend = currentBackend ( state . config ) ;
80
80
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
+ }
81
97
82
98
dispatch ( mediaPersisting ( ) ) ;
83
99
84
100
try {
85
101
const assetProxy = await createAssetProxy ( file . name . toLowerCase ( ) , file , false , privateUpload ) ;
86
102
dispatch ( addAsset ( assetProxy ) ) ;
87
103
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
-
96
104
const asset = await backend . persistMedia ( assetProxy ) ;
97
105
return dispatch ( mediaPersisted ( asset ) ) ;
98
106
}
0 commit comments