Skip to content
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

Merged
merged 3 commits into from
Mar 29, 2020

Conversation

gtam25
Copy link
Contributor

@gtam25 gtam25 commented Mar 25, 2020

Added auto-numbering for downloaded file names, which conflict with file names in the current directory. #6068

  • Change in CHANGELOG.md described (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)

Copy link
Member

@koppor koppor left a 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:

  1. Please keep FileNameCleaner and introduce FileNameUniqueness. 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.

  2. 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 = "";
Copy link
Member

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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++.

Copy link
Member

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);
Copy link
Member

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";
Copy link
Member

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";
Copy link
Member

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.

@gtam25
Copy link
Contributor Author

gtam25 commented Mar 25, 2020

Thank you for your contribution.

Some nitpick comments inside 😇

Two more things:

  1. Please keep FileNameCleaner and introduce FileNameUniqueness. 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.
  2. 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.

I think our problem is that different files are getting downloaded as "default.pdf" (when no BibTeX key exists).
If we use Files.mismatch (as you mentioned in number 2 above), since both paths refer to same file path, it would return a match and hence not download the other files (which we do not want I guess ?)

- fixed downloading file with same name
- updated changelog
@koppor
Copy link
Member

koppor commented Mar 25, 2020

  1. Can you try to include a check of Files.mismatch (see 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.
    I think our problem is that different files are getting downloaded as "default.pdf" (when no BibTeX key exists).

Yeah.

If we use Files.mismatch (as you mentioned in number 2 above), since both paths refer to same file path, it would return a match and hence not download the other files (which we do not want I guess ?)

Long solution would be:

  1. default.pdf exists
  2. Download as default (1).pdf.
  3. Store it in the linked files
  4. Check if default.pdf equals default (1).pdf.
  5. If default.pdf equals default (1).pdf then a) remote default (1).pdf from the file system, b) remove default (1).pdf from the list of linked files, c) add a notification that the file already exists.

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

Copy link
Member

@tobiasdiez tobiasdiez left a 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)
Copy link
Member

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);
Copy link
Member

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 {
Copy link
Member

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
Copy link
Member

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);
Copy link
Member

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.

@koppor
Copy link
Member

koppor commented Mar 29, 2020

Comments addressed. There are some code style issues, which I will quickly fix in master after merge.

@koppor koppor merged commit 7ec1d0c into JabRef:master Mar 29, 2020
@koppor
Copy link
Member

koppor commented Mar 29, 2020

Thank you for having kept up the work. Looking forward to more contributions!

koppor pushed a commit that referenced this pull request Aug 15, 2022
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
calixtus pushed a commit that referenced this pull request Aug 15, 2022
)

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>
koppor pushed a commit that referenced this pull request Sep 1, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants