-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Conversation
c4c9aa8
to
10ee0ef
Compare
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.
Thank you for your contribution.
Some nitpick comments inside 😇
Two more things:
-
Please keep
FileNameCleaner
and introduceFileNameUniqueness
. Reason: There is no shared code between the two utility methods (no cohesion in the sence of data; only in the sense of the general setting). Small classes make it easier to maintain them. -
Can you try to include a check of
Files.mismatch
(see https://stackoverflow.com/a/56680991/873282 for details) at download -> before the same file is linked, just do not link it instead of creating... (1).pdf
.
Optional<String> extensionOptional = FileUtil.getFileExtension(fileName); | ||
|
||
// the suffix include the '.' , if extension is present Eg: ".pdf" | ||
String extensionSuffix = ""; |
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.
Please put that into an else path in the if of line 91. (Declaring the variables here is OK). I find it hard if the contents of the variables are overwritten: The default case is that there is an extension.
Path absolutePath = Paths.get(absoluteName); | ||
String newFileName = fileName; | ||
|
||
for (int counter = 1; Files.exists(absolutePath); ++counter) { |
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.
for (int counter = 1; Files.exists(absolutePath); ++counter) { | |
for (int counter = 1; Files.exists(absolutePath); counter++) { |
++
at the end to be inline with the usual i++
.
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.
After thinking longer, please rewrite to a while
loop. Not testing for the counter in the test of a for
loop is uncommon. Thus, hard to read when maintaining the code.
newFileName = fileNameWithoutExtension + | ||
" (" + counter + ")" + | ||
extensionSuffix; | ||
absolutePath = targetDirectory.resolve(newFileName); |
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.
No need for that variable. Can be tesed in the while condition.
public void testGetNonOverWritingFileNameReturnsUniqueName(@TempDir Path tempDirectory) throws IOException { | ||
String dummyFile1 = "default.txt"; | ||
String dummyFile2 = "default (1).txt"; | ||
String expectedFileName = "default (2).txt"; |
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.
Please include that in the assertEquals
directly.
|
||
@Test | ||
public void testGetNonOverWritingFileNameReturnsUniqueName(@TempDir Path tempDirectory) throws IOException { | ||
String dummyFile1 = "default.txt"; |
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.
Please introduce Path dummyFile1
variable. In both usages of dummyFileX
, you are resulting the name using the same path.
src/test/java/org/jabref/logic/util/io/FileNameHandlerTest.java
Outdated
Show resolved
Hide resolved
I think our problem is that different files are getting downloaded as "default.pdf" (when no BibTeX key exists). |
- fixed downloading file with same name - updated changelog
7f4af8c
to
155fbe3
Compare
Yeah.
Long solution would be:
I thought that one can omit step 3. - step 5c is necessary in all cases. I see that this is not strongly part of your PR, so can be left for future work. Submitted as #6197 |
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.
Thanks for your contribution! The code looks good to me. I've only a few very minor comments before we merge.
* @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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this comment.
fileNameWithoutExtension = fileName; | ||
} | ||
|
||
// Path absolutePath = Paths.get(absoluteName); |
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.
This one as well.
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 comment
The 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)
public class FileNameUniqueness { | ||
|
||
/** | ||
* Returns an absolute path to a file which does not match with any existing file names |
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?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting the file shouldn't be necessary as the TempDir
should take care of this.
Comments addressed. There are some code style issues, which I will quickly fix in |
Thank you for having kept up the work. Looking forward to more contributions! |
) b9302fd Update APA styles for "event" macro (#6174) d4daec6 remove DOI for printed articles organizational-studies.csl (#6176) acfc620 Create liver-transplantation.csl (#6167) 129a775 Change "event" to "event-title" (#6164) git-subtree-dir: buildres/csl/csl-styles git-subtree-split: b9302fd Co-authored-by: github actions <jabrefmail+webfeedback@gmail.com>
8d69f16 Create university-of-hull-harvard.csl (#6146) 139dfdd Create current organic synthesis.csl (#6139) bb006c8 Update acta-universitatis-agriculturae-sueciae.csl (#6143) 5815da0 Create food-science-and-biotechnology.csl (#6132) 2702a7c Update harvard-university-for-the-creative-arts.csl (#6104) ef34543 Update economic-geology.csl (#6128) 0adcd30 Bump mathieudutour/github-tag-action from 5.6 to 6.0 (#6141) 3c36e97 Create universite-du-quebec-a-montreal-prenoms.csl (#6073) 415bc05 Bump softprops/action-gh-release from 0.1.14 to 1 (#6142) ae8c5e4 Create politique-europeenne.csl (#6074) 09cbc09 Update cell-numeric-superscript.csl (#6188) 6ee1ace Update avian-conservation-and-ecology.csl (#6191) cb5c43f Update harvard-anglia-ruskin-university.csl (#6189) 5c4f4c0 Create anais-da-academia-brasileira-de-ciencias.csl (#6066) a60dfe9 Update cardiff-university-harvard.csl (#6190) 999a45c Create sociologia-urbana-e-rurale.csl (#6042) 1bc9d62 Bluebook (#6183) a4f2a72 Oxford Brookes (#6182) 88df8d5 Delete harvard-cardiff-university-old.csl (#6180) b9302fd Update APA styles for "event" macro (#6174) d4daec6 remove DOI for printed articles organizational-studies.csl (#6176) acfc620 Create liver-transplantation.csl (#6167) 129a775 Change "event" to "event-title" (#6164) git-subtree-dir: buildres/csl/csl-styles git-subtree-split: 8d69f16
Added auto-numbering for downloaded file names, which conflict with file names in the current directory. #6068