55import  { basename , dirname , extname , join  } from  ' path' 
66import  { emit  } from  ' @nextcloud/event-bus' 
77import  { Node  } from  ' @nextcloud/files' 
8- import  { showError , showSuccess  } from  ' @nextcloud/dialogs' 
8+ import  { showError , showSuccess ,  DialogBuilder  } from  ' @nextcloud/dialogs' 
99import  axios  from  ' @nextcloud/axios' 
1010
1111import  logger  from  ' ../services/logger.js' 
@@ -149,7 +149,23 @@ export default {
149149			window .removeEventListener (' keydown' this .handleKeydown , true ) 
150150			this .$emit (' close'  
151151		}, 
152- 
152+ 		/**  
153+ 		 * Check if a file exists at the given URL 
154+ 		 * @param  {string}  url  The URL to check 
155+ 		 * @return  {Promise<boolean>}  True if the file exists, false otherwise 
156+ 		 */  
157+ 		 async  fileExists (url ) { 
158+ 			try  { 
159+ 				await  axios .head (url, { validateStatus :  status  =>  status ===  200  ||  status ===  404  }) 
160+ 				const  response  =  await  axios .head (url) 
161+ 				return  response .status  ===  200  
162+ 			} catch  (error) { 
163+ 				if  (error .response ? .status  ===  404 ) { 
164+ 					return  false  
165+ 				} 
166+ 				throw  error 
167+ 			} 
168+ 		}, 
153169		/**  
154170		 * User saved the image 
155171		 * 
@@ -165,6 +181,51 @@ export default {
165181			const  putUrl  =  origin +  join (dirname (pathname), fullName) 
166182			logger .debug (' Saving image...' :  this .src , fullName }) 
167183
184+ 			const  fileExists  =  await  this .fileExists (putUrl) 
185+ 			logger .debug (' File exists'  
186+ 			if  (fileExists) { 
187+ 				logger .debug (' File exists, showing confirmation dialog'  
188+ 				try  { 
189+ 					const  isOriginal  =  fullName ===  basename (this .src ) 
190+ 					const  message  =  isOriginal 
191+ 						?  t (' viewer' ' You are about to overwrite the original file. Are you sure you want to continue?'  
192+ 						:  t (' viewer' ' A file with this name already exists. Do you want to overwrite it?'  
193+ 
194+ 					let  confirmed =  false  
195+ 					const  dialog  =  (new  DialogBuilder ()) 
196+ 						.setName (t (' viewer' ' Confirm overwrite'  
197+ 						.setText (message) 
198+ 						.setButtons ([ 
199+ 							{ 
200+ 								label:  t (' viewer' ' Cancel'  
201+ 								type:  ' secondary'  
202+ 								callback :  () =>  { 
203+ 									confirmed =  false  
204+ 								}, 
205+ 							}, 
206+ 							{ 
207+ 								label:  t (' viewer' ' Overwrite'  
208+ 								type:  ' error'  
209+ 								callback :  () =>  { 
210+ 									confirmed =  true  
211+ 								}, 
212+ 							}, 
213+ 						]) 
214+ 						.build () 
215+ 
216+ 					await  dialog .show () 
217+ 
218+ 					if  (! confirmed) { 
219+ 						logger .debug (' User cancelled overwrite'  
220+ 						return  
221+ 					} 
222+ 				} catch  (error) { 
223+ 					logger .error (' Error showing confirmation dialog'  
224+ 					showError (t (' viewer' ' An error occurred while trying to confirm the file overwrite.'  
225+ 					return  
226+ 				} 
227+ 			} 
228+ 
168229			//  toBlob is not very smart... 
169230			mimeType =  mimeType .replace (' jpg' ' jpeg'  
170231
0 commit comments