From 7b29fe39b63f6630b3e92bc67e5080531643a708 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 10 Aug 2018 11:46:38 +0200 Subject: [PATCH] Fix attach file does not relativize file path (#4252) * attempt at attach file relative file making * add method for making path relative * set new path when chosing another file * add changelog entry * Return new relatizived path --- CHANGELOG.md | 2 +- .../filelist/LinkedFilesEditDialogViewModel.java | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76d2ecfa489..279bbd14875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,7 +63,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an issue where the preview pane in entry preview in preferences wasn't showing the citation style selected [#3849](https://github.com/JabRef/jabref/issues/3849) - We fixed an issue where the default entry preview style still contained the field `review`. The field `review` in the style is now replaced with comment to be consistent with the entry editor [#4098](https://github.com/JabRef/jabref/issues/4098) - We fixed an issue where users were vulnerable to XXE attacks during parsing [#4229](https://github.com/JabRef/jabref/issues/4229) -- We fixed an issue where files added via the "Attach file" contextmenu of an entry were not made relative. [#4201](https://github.com/JabRef/jabref/issues/4201) +- We fixed an issue where files added via the "Attach file" contextmenu of an entry were not made relative. [#4201](https://github.com/JabRef/jabref/issues/4201) and [#4241](https://github.com/JabRef/jabref/issues/4241) - We fixed an issue where author list parser can't generate bibtex for Chinese author. [#4169](https://github.com/JabRef/jabref/issues/4169) diff --git a/src/main/java/org/jabref/gui/filelist/LinkedFilesEditDialogViewModel.java b/src/main/java/org/jabref/gui/filelist/LinkedFilesEditDialogViewModel.java index 492cda62592..1226082b3d8 100644 --- a/src/main/java/org/jabref/gui/filelist/LinkedFilesEditDialogViewModel.java +++ b/src/main/java/org/jabref/gui/filelist/LinkedFilesEditDialogViewModel.java @@ -83,19 +83,17 @@ public void openBrowseDialog() { dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(path -> { // Store the directory for next time: preferences.setWorkingDir(path); + link.set(relativize(path)); - // If the file is below the file directory, make the path relative: - List fileDirectories = database.getFileDirectoriesAsPaths(preferences.getFileDirectoryPreferences()); - path = FileUtil.shortenFileName(path, fileDirectories); - - link.set(path.toString()); setExternalFileTypeByExtension(link.getValueSafe()); }); } public void setValues(LinkedFile linkedFile) { description.set(linkedFile.getDescription()); - link.set(linkedFile.getLink()); + + Path linkPath = Paths.get(linkedFile.getLink()); + link.set(relativize(linkPath)); selectedExternalFileType.setValue(null); @@ -128,4 +126,9 @@ public LinkedFile getNewLinkedFile() { return new LinkedFile(description.getValue(), link.getValue(), monadicSelectedExternalFileType.map(ExternalFileType::toString).getOrElse("")); } + private String relativize(Path filePath) { + List fileDirectories = database.getFileDirectoriesAsPaths(preferences.getFileDirectoryPreferences()); + return FileUtil.shortenFileName(filePath, fileDirectories).toString(); + } + }