Skip to content

Commit 155fbe3

Browse files
author
Goutam Lavudiya
committed
fixes JabRef#6068
- fixed downloading file with same name - updated changelog
1 parent 120a0af commit 155fbe3

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-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,49 @@
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 an absolute path to a file which does not match with any existing file names
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+
// String absoluteName = targetDirectory.resolve(fileName)
18+
// .toString();
19+
20+
Optional<String> extensionOptional = FileUtil.getFileExtension(fileName);
21+
22+
// the suffix include the '.' , if extension is present Eg: ".pdf"
23+
String extensionSuffix;
24+
String fileNameWithoutExtension;
25+
26+
if (extensionOptional.isPresent()) {
27+
extensionSuffix = '.' + extensionOptional.get();
28+
fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
29+
}
30+
else {
31+
extensionSuffix = "";
32+
fileNameWithoutExtension = fileName;
33+
}
34+
35+
// Path absolutePath = Paths.get(absoluteName);
36+
String newFileName = fileName;
37+
38+
int counter = 1;
39+
while ( Files.exists(
40+
targetDirectory.resolve(newFileName))
41+
) {
42+
newFileName = fileNameWithoutExtension +
43+
" (" + counter + ")" +
44+
extensionSuffix;
45+
counter++;
46+
}
47+
return newFileName;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.jabref.logic.util.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertFalse;
12+
13+
14+
public class FileNameUniquenessTest {
15+
16+
@TempDir
17+
protected Path tempDir;
18+
19+
@Test
20+
public void testGetNonOverWritingFileNameReturnsSameName(@TempDir Path tempDirectory) throws IOException {
21+
22+
assertFalse(
23+
Files.exists(tempDirectory.resolve("sameFile.txt"))
24+
);
25+
26+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDirectory, "sameFile.txt");
27+
assertEquals("sameFile.txt", outputFileName);
28+
29+
}
30+
31+
@Test
32+
public void testGetNonOverWritingFileNameReturnsUniqueNameOver1Conflict() throws IOException {
33+
Path dummyFilePath1 = tempDir.resolve("differentFile.txt");
34+
35+
Files.createFile(dummyFilePath1);
36+
37+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "differentFile.txt");
38+
assertEquals("differentFile (1).txt", outputFileName);
39+
40+
Files.delete(dummyFilePath1);
41+
}
42+
43+
@Test
44+
public void testGetNonOverWritingFileNameReturnsUniqueNameOverNConflicts() throws IOException {
45+
Path dummyFilePath1 = tempDir.resolve("manyfiles.txt");
46+
Path dummyFilePath2 = tempDir.resolve("manyfiles (1).txt");
47+
48+
Files.createFile(dummyFilePath1);
49+
Files.createFile(dummyFilePath2);
50+
51+
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "manyfiles.txt");
52+
assertEquals("manyfiles (2).txt", outputFileName);
53+
54+
Files.delete(dummyFilePath1);
55+
Files.delete(dummyFilePath2);
56+
}
57+
}

0 commit comments

Comments
 (0)