-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix overwritting downloaded files with same name #6174
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this comment. |
||
// .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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one as well. |
||
String newFileName = fileName; | ||
|
||
int counter = 1; | ||
while ( Files.exists( | ||
targetDirectory.resolve(newFileName)) | ||
) { | ||
newFileName = fileNameWithoutExtension + | ||
" (" + counter + ")" + | ||
extensionSuffix; | ||
counter++; | ||
} | ||
return newFileName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.jabref.logic.util.io; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to have the temporary directory as a variable as well as an argument? I would prefer if all tests use the same format (either variable or either paramater) |
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleting the file shouldn't be necessary as the |
||
} | ||
|
||
@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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description seems to be off as no absolute path is returned but only the file name, right?