Skip to content

Commit 7ec1d0c

Browse files
gtam25Goutam Lavudiya
and
Goutam Lavudiya
authored
Fix overwritting downloaded files with same name (#6174)
- fixes #6068 Co-authored-by: Goutam Lavudiya <[email protected]>
1 parent 9d9394e commit 7ec1d0c

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
2525
- We fixed an issue where opening a library from the recent libraries menu was not possible. [#5939](https://github.com/JabRef/jabref/issues/5939)
2626
- We fixed an issue with inconsistent capitalization of file extensions when downloading files [#6115](https://github.com/JabRef/jabref/issues/6115)
2727
- We fixed the display of language and encoding in the preferences dialog. [#6130](https://github.com/JabRef/jabref/pull/6130)
28-
28+
- We fixed an issue where search full-text documents downloaded files with same name, overwriting existing files. [#6174](https://github.com/JabRef/jabref/pull/6174)
2929
### Removed
3030

3131
- We removed the obsolete `External programs / Open PDF` section in the preferences, as the default application to open PDFs is now set in the `Manage external file types` dialog. [#6130](https://github.com/JabRef/jabref/pull/6130)

src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.jabref.logic.externalfiles.LinkedFileHandler;
3939
import org.jabref.logic.l10n.Localization;
4040
import org.jabref.logic.net.URLDownload;
41+
import org.jabref.logic.util.io.FileNameUniqueness;
4142
import org.jabref.logic.xmp.XmpPreferences;
4243
import org.jabref.logic.xmp.XmpUtilWriter;
4344
import org.jabref.model.database.BibDatabaseContext;
@@ -419,6 +420,7 @@ public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownloa
419420
String suggestedTypeName = externalFileType.getName();
420421
linkedFile.setFileType(suggestedTypeName);
421422
String suggestedName = linkedFileHandler.getSuggestedFileName(externalFileType.getExtension());
423+
suggestedName = FileNameUniqueness.getNonOverWritingFileName(targetDirectory, suggestedName);
422424
return targetDirectory.resolve(suggestedName);
423425
})
424426
.then(destination -> new FileDownloadTask(urlDownload.getSource(), destination))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.jabref.logic.util.io;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.util.Optional;
6+
7+
public class FileNameUniqueness {
8+
9+
/**
10+
* Returns a file-name such that it does not match any existing files in targetDirectory
11+
*
12+
* @param targetDirectory The directory in which file name should be unique
13+
* @param fileName Suggested name for the file
14+
* @return a file-name such that it does not match any existing files in targetDirectory.
15+
* */
16+
public static String getNonOverWritingFileName(Path targetDirectory, String fileName) {
17+
18+
Optional<String> extensionOptional = FileUtil.getFileExtension(fileName);
19+
20+
// the suffix include the '.' , if extension is present Eg: ".pdf"
21+
String extensionSuffix;
22+
String fileNameWithoutExtension;
23+
24+
if (extensionOptional.isPresent()) {
25+
extensionSuffix = '.' + extensionOptional.get();
26+
fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
27+
}
28+
else {
29+
extensionSuffix = "";
30+
fileNameWithoutExtension = fileName;
31+
}
32+
33+
String newFileName = fileName;
34+
35+
int counter = 1;
36+
while ( Files.exists(
37+
targetDirectory.resolve(newFileName))
38+
) {
39+
newFileName = fileNameWithoutExtension +
40+
" (" + counter + ")" +
41+
extensionSuffix;
42+
counter++;
43+
}
44+
return newFileName;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jabref.logic.util.io;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.io.TempDir;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertFalse;
12+
13+
public class FileNameUniquenessTest {
14+
15+
@TempDir
16+
protected Path tempDir;
17+
18+
@Test
19+
public void testGetNonOverWritingFileNameReturnsSameName() throws IOException {
20+
21+
assertFalse(
22+
Files.exists(tempDir.resolve("sameFile.txt"))
23+
);
24+
25+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "sameFile.txt");
26+
assertEquals("sameFile.txt", outputFileName);
27+
}
28+
29+
@Test
30+
public void testGetNonOverWritingFileNameReturnsUniqueNameOver1Conflict() throws IOException {
31+
Path dummyFilePath1 = tempDir.resolve("differentFile.txt");
32+
33+
Files.createFile(dummyFilePath1);
34+
35+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "differentFile.txt");
36+
assertEquals("differentFile (1).txt", outputFileName);
37+
}
38+
39+
@Test
40+
public void testGetNonOverWritingFileNameReturnsUniqueNameOverNConflicts() throws IOException {
41+
Path dummyFilePath1 = tempDir.resolve("manyfiles.txt");
42+
Path dummyFilePath2 = tempDir.resolve("manyfiles (1).txt");
43+
44+
Files.createFile(dummyFilePath1);
45+
Files.createFile(dummyFilePath2);
46+
47+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "manyfiles.txt");
48+
assertEquals("manyfiles (2).txt", outputFileName);
49+
}
50+
}

0 commit comments

Comments
 (0)