-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fixed downloading file with same name - updated changelog
- Loading branch information
Goutam Lavudiya
committed
Mar 25, 2020
1 parent
120a0af
commit 155fbe3
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/jabref/logic/util/io/FileNameUniqueness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jabref.logic.util.io; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
public class FileNameUniqueness { | ||
|
||
/** | ||
* 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; | ||
|
||
if (extensionOptional.isPresent()) { | ||
extensionSuffix = '.' + extensionOptional.get(); | ||
fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.')); | ||
} | ||
else { | ||
extensionSuffix = ""; | ||
fileNameWithoutExtension = fileName; | ||
} | ||
|
||
// Path absolutePath = Paths.get(absoluteName); | ||
String newFileName = fileName; | ||
|
||
int counter = 1; | ||
while ( Files.exists( | ||
targetDirectory.resolve(newFileName)) | ||
) { | ||
newFileName = fileNameWithoutExtension + | ||
" (" + counter + ")" + | ||
extensionSuffix; | ||
counter++; | ||
} | ||
return newFileName; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/jabref/logic/util/io/FileNameUniquenessTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
|
||
public class FileNameUniquenessTest { | ||
|
||
@TempDir | ||
protected Path tempDir; | ||
|
||
@Test | ||
public void testGetNonOverWritingFileNameReturnsSameName(@TempDir Path tempDirectory) throws IOException { | ||
|
||
assertFalse( | ||
Files.exists(tempDirectory.resolve("sameFile.txt")) | ||
); | ||
|
||
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDirectory, "sameFile.txt"); | ||
assertEquals("sameFile.txt", outputFileName); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetNonOverWritingFileNameReturnsUniqueNameOver1Conflict() throws IOException { | ||
Path dummyFilePath1 = tempDir.resolve("differentFile.txt"); | ||
|
||
Files.createFile(dummyFilePath1); | ||
|
||
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "differentFile.txt"); | ||
assertEquals("differentFile (1).txt", outputFileName); | ||
|
||
Files.delete(dummyFilePath1); | ||
} | ||
|
||
@Test | ||
public void testGetNonOverWritingFileNameReturnsUniqueNameOverNConflicts() throws IOException { | ||
Path dummyFilePath1 = tempDir.resolve("manyfiles.txt"); | ||
Path dummyFilePath2 = tempDir.resolve("manyfiles (1).txt"); | ||
|
||
Files.createFile(dummyFilePath1); | ||
Files.createFile(dummyFilePath2); | ||
|
||
String outputFileName = FileNameUniqueness.getNonOverWritingFileName(tempDir, "manyfiles.txt"); | ||
assertEquals("manyfiles (2).txt", outputFileName); | ||
|
||
Files.delete(dummyFilePath1); | ||
Files.delete(dummyFilePath2); | ||
} | ||
} |