Skip to content

Commit

Permalink
fixes JabRef#6068
Browse files Browse the repository at this point in the history
fixed downloading file with same name
  • Loading branch information
Goutam Lavudiya committed Mar 25, 2020
1 parent 70b7058 commit 20d57e9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.io.FileNameHandler;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -419,6 +420,7 @@ public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownloa
String suggestedTypeName = externalFileType.getName();
linkedFile.setFileType(suggestedTypeName);
String suggestedName = linkedFileHandler.getSuggestedFileName(externalFileType.getExtension());
suggestedName = FileNameHandler.getNonOverWritingFileName(targetDirectory, suggestedName);
return targetDirectory.resolve(suggestedName);
})
.then(destination -> new FileDownloadTask(urlDownload.getSource(), destination))
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/jabref/logic/util/io/FileNameHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jabref.logic.util.io;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;

/**
* This class is based on http://stackoverflow.com/a/5626340/873282
Expand Down Expand Up @@ -67,6 +71,38 @@ public static String cleanDirectoryName(String badFileName) {
return cleanName.toString().trim();
}

/**
* Returns an absolute path to a file which does not match with any existing file names
*
* @param targetDirectory The directory in which file name should be unique
* @param fileName Suggested name for the file
* @return a file-name such that it does not match any existing files in targetDirectory.
* */
public static String getNonOverWritingFileName(Path targetDirectory, String fileName) {
String absoluteName = targetDirectory.resolve(fileName)
.toString();

Optional<String> extensionOptional = FileUtil.getFileExtension(fileName);

// the suffix include the '.' , if extension is present Eg: ".pdf"
String extensionSuffix = "";
String fileNameWithoutExtension = fileName;

if(extensionOptional.isPresent()) {
extensionSuffix = '.'+extensionOptional.get();
fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
}

Path absolutePath = Paths.get(absoluteName);
String newFileName = fileName;

for(int counter = 1; Files.exists(absolutePath); ++counter) {
newFileName = fileNameWithoutExtension+" ("+counter+")"+extensionSuffix;
absolutePath = targetDirectory.resolve(newFileName);
}
return newFileName;
}

private static boolean isCharLegal(char c) {
return Arrays.binarySearch(FileNameHandler.ILLEGAL_CHARS, c) < 0;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/jabref/logic/util/io/FileNameHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.jabref.logic.util.io;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -30,4 +35,20 @@ public void testCleanDirectoryNameForWindows() {
public void testCleanCurlyBracesAsWell() {
assertEquals("The Evolution of Sentiment_ Analysis_A Review of Research Topics, Venues, and Top Cited Papers.PDF", FileNameHandler.cleanFileName("The Evolution of Sentiment} Analysis}A Review of Research Topics, Venues, and Top Cited Papers.PDF"));
}

@Test
public void test_getNonOverWritingFileName_returnsUniqueName(@TempDir Path tempDirectory) throws IOException {
String dummyFile1 = "default.txt";
String dummyFile2 = "default (1).txt";
String expectedFileName = "default (2).txt";

Files.createFile(tempDirectory.resolve(dummyFile1));
Files.createFile(tempDirectory.resolve(dummyFile2));

String outputFileName = FileNameHandler.getNonOverWritingFileName(tempDirectory, "default.txt");
assertEquals(expectedFileName, outputFileName);

Files.delete(tempDirectory.resolve(dummyFile1));
Files.delete(tempDirectory.resolve(dummyFile2));
}
}

0 comments on commit 20d57e9

Please sign in to comment.