diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index d16e2b90712..d52a24b9f95 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -223,30 +223,22 @@ public void renameFileToName(String targetFileName) { } private void performRenameWithConflictCheck(String targetFileName) { - Optional fileConflictCheck = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName); - if (fileConflictCheck.isPresent()) { - boolean confirmOverwrite = dialogService.showConfirmationDialogAndWait( + Optional existingFile = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName); + boolean overwriteFile = false; + + if (existingFile.isPresent()) { + overwriteFile = dialogService.showConfirmationDialogAndWait( Localization.lang("File exists"), Localization.lang("'%0' exists. Overwrite file?", targetFileName), Localization.lang("Overwrite")); - if (confirmOverwrite) { - try { - Files.delete(fileConflictCheck.get()); - } catch (IOException e) { - dialogService.showErrorDialogAndWait( - Localization.lang("Rename failed"), - Localization.lang("JabRef cannot access the file because it is being used by another process."), - e); - return; - } - } else { + if (!overwriteFile) { return; } } try { - linkedFileHandler.renameToName(targetFileName); + linkedFileHandler.renameToName(targetFileName, overwriteFile); } catch (IOException e) { dialogService.showErrorDialogAndWait(Localization.lang("Rename failed"), Localization.lang("JabRef cannot access the file because it is being used by another process.")); } @@ -284,6 +276,7 @@ public void moveToDefaultDirectory() { /** * Gets the filename for the current linked file and compares it to the new suggested filename. + * * @return true if the suggested filename is same as current filename. */ public boolean isGeneratedNameSameAsOriginal() { @@ -296,6 +289,7 @@ public boolean isGeneratedNameSameAsOriginal() { /** * Compares suggested filepath of current linkedFile with existing filepath. + * * @return true if suggested filepath is same as existing filepath. */ public boolean isGeneratedPathSameAsOriginal() { diff --git a/src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java b/src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java index 4aaed7c5498..ec7c4153adc 100644 --- a/src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java +++ b/src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -71,35 +72,38 @@ public boolean moveToDefaultDirectory() throws IOException { } public boolean renameToSuggestedName() throws IOException { - return renameToName(getSuggestedFileName()); + return renameToName(getSuggestedFileName(), false); } - public boolean renameToName(String targetFileName) throws IOException { + public boolean renameToName(String targetFileName, boolean overwriteExistingFile) throws IOException { Optional oldFile = fileEntry.findIn(databaseContext, filePreferences); if (!oldFile.isPresent()) { - // Could not find file return false; } - Path newPath = oldFile.get().resolveSibling(targetFileName); + final Path oldPath = oldFile.get(); + final Path newPath = oldPath.resolveSibling(targetFileName); - String expandedOldFilePath = oldFile.get().toString(); + String expandedOldFilePath = oldPath.toString(); boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath) - && !newPath.toString().equals(expandedOldFilePath); + && !newPath.toString().equals(expandedOldFilePath); - if (Files.exists(newPath) && !pathsDifferOnlyByCase) { - // We do not overwrite files - // Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we - // nonetheless rename files to a new name which just differs by case. - LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldFile.get(), newPath); + // Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we + // nonetheless rename files to a new name which just differs by case. + if (Files.exists(newPath) && !pathsDifferOnlyByCase && !overwriteExistingFile) { + LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldPath, newPath); return false; + } + + if (Files.exists(newPath) && !pathsDifferOnlyByCase && overwriteExistingFile) { + Files.createDirectories(newPath.getParent()); + LOGGER.debug("Overwriting existing file {}", newPath); + Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING); } else { Files.createDirectories(newPath.getParent()); + Files.move(oldPath, newPath); } - // Rename - Files.move(oldFile.get(), newPath); - // Update path fileEntry.setLink(relativize(newPath));