|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.nio.file.Files;
|
5 | 5 | import java.nio.file.Path;
|
| 6 | +import java.nio.file.StandardCopyOption; |
6 | 7 | import java.util.List;
|
7 | 8 | import java.util.Objects;
|
8 | 9 | import java.util.Optional;
|
@@ -71,35 +72,38 @@ public boolean moveToDefaultDirectory() throws IOException {
|
71 | 72 | }
|
72 | 73 |
|
73 | 74 | public boolean renameToSuggestedName() throws IOException {
|
74 |
| - return renameToName(getSuggestedFileName()); |
| 75 | + return renameToName(getSuggestedFileName(), false); |
75 | 76 | }
|
76 | 77 |
|
77 |
| - public boolean renameToName(String targetFileName) throws IOException { |
| 78 | + public boolean renameToName(String targetFileName, boolean overwriteExistingFile) throws IOException { |
78 | 79 | Optional<Path> oldFile = fileEntry.findIn(databaseContext, filePreferences);
|
79 | 80 | if (!oldFile.isPresent()) {
|
80 |
| - // Could not find file |
81 | 81 | return false;
|
82 | 82 | }
|
83 | 83 |
|
84 |
| - Path newPath = oldFile.get().resolveSibling(targetFileName); |
| 84 | + final Path oldPath = oldFile.get(); |
| 85 | + final Path newPath = oldPath.resolveSibling(targetFileName); |
85 | 86 |
|
86 |
| - String expandedOldFilePath = oldFile.get().toString(); |
| 87 | + String expandedOldFilePath = oldPath.toString(); |
87 | 88 | boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath)
|
88 |
| - && !newPath.toString().equals(expandedOldFilePath); |
| 89 | + && !newPath.toString().equals(expandedOldFilePath); |
89 | 90 |
|
90 |
| - if (Files.exists(newPath) && !pathsDifferOnlyByCase) { |
91 |
| - // We do not overwrite files |
92 |
| - // Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we |
93 |
| - // nonetheless rename files to a new name which just differs by case. |
94 |
| - 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); |
| 91 | + // Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we |
| 92 | + // nonetheless rename files to a new name which just differs by case. |
| 93 | + if (Files.exists(newPath) && !pathsDifferOnlyByCase && !overwriteExistingFile) { |
| 94 | + LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldPath, newPath); |
95 | 95 | return false;
|
| 96 | + } |
| 97 | + |
| 98 | + if (Files.exists(newPath) && !pathsDifferOnlyByCase && overwriteExistingFile) { |
| 99 | + Files.createDirectories(newPath.getParent()); |
| 100 | + LOGGER.debug("Overwriting existing file {}", newPath); |
| 101 | + Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING); |
96 | 102 | } else {
|
97 | 103 | Files.createDirectories(newPath.getParent());
|
| 104 | + Files.move(oldPath, newPath); |
98 | 105 | }
|
99 | 106 |
|
100 |
| - // Rename |
101 |
| - Files.move(oldFile.get(), newPath); |
102 |
| - |
103 | 107 | // Update path
|
104 | 108 | fileEntry.setLink(relativize(newPath));
|
105 | 109 |
|
|
0 commit comments