From 8ad6f233e647a0d6d246ce044c6760fe8ab73818 Mon Sep 17 00:00:00 2001 From: systemoperator Date: Mon, 27 Jan 2020 19:57:02 +0100 Subject: [PATCH 1/3] Fixes #5861 Description: - Cleanup entries: "Make paths of linked files relative (if possible)" broke web URLs and resulted in various other issues subsequently. This PR fixes this issue. It has been tested for local files and web urls. --- .../logic/cleanup/RelativePathsCleanup.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java b/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java index 9b18e939782..007787ef3d6 100644 --- a/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java +++ b/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java @@ -33,10 +33,17 @@ public List cleanup(BibEntry entry) { for (LinkedFile fileEntry : fileList) { String oldFileName = fileEntry.getLink(); - String newFileName = FileUtil - .relativize(Paths.get(oldFileName), databaseContext.getFileDirectoriesAsPaths(filePreferences)) - .toString(); - + String newFileName = null; + if (isWebUrl(oldFileName)) { + // let web url untouched + newFileName = oldFileName; + } + else { + // only try to transform local file path to relative one + newFileName = FileUtil + .relativize(Paths.get(oldFileName), databaseContext.getFileDirectoriesAsPaths(filePreferences)) + .toString(); + } LinkedFile newFileEntry = fileEntry; if (!oldFileName.equals(newFileName)) { newFileEntry = new LinkedFile(fileEntry.getDescription(), newFileName, fileEntry.getFileType()); @@ -57,4 +64,21 @@ public List cleanup(BibEntry entry) { return Collections.emptyList(); } + /** + * Checks, if the given file path is a well-formed web url + * + * @param filePath file path to check + * @return true, if the given file path is a web url, false otherwise + */ + private static boolean isWebUrl(String filePath) + { + if (filePath == null) { + return false; + } + String normalizedFilePath = filePath.trim().toLowerCase(); + if (normalizedFilePath.startsWith("http://") || normalizedFilePath.startsWith("https://")) { // INFO: can be extended, if needed + return true; + } + return false; + } } From f033dbfbba9d04165577493b2ad0ac9aaca30e37 Mon Sep 17 00:00:00 2001 From: systemoperator Date: Tue, 28 Jan 2020 11:38:50 +0100 Subject: [PATCH 2/3] changelog updated --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aacb7819b24..6ffa4e88747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an issue where the ampersand character wasn't rendering correctly on previews. [#3840](https://github.com/JabRef/jabref/issues/3840) - We fixed an issue where an erroneous "The library has been modified by another program" message was shown when saving. [#4877](https://github.com/JabRef/jabref/issues/4877) - We fixed an issue where the file extension was missing after downloading a file (we now fall-back to pdf). [#5816](https://github.com/JabRef/jabref/issues/5816) +- We fixed an issue where cleaning up entries broke web URLs, if "Make paths of linked files relative (if possible)" was enabled, which resulted in various other issues subsequently. [#5861](https://github.com/JabRef/jabref/issues/5861) ### Removed - Ampersands are no longer escaped by default in the `bib` file. If you want to keep the current behaviour, you can use the new "Escape Ampersands" formatter as a save action. From 4adfdc5b0833e2f3a4a131d0c3ec11087d208bd8 Mon Sep 17 00:00:00 2001 From: systemoperator Date: Tue, 28 Jan 2020 22:13:13 +0100 Subject: [PATCH 3/3] refactoring: use existing method isOnlineLink() for checking whether a file link is an online link; improving isOnlineLink() --- .../logic/cleanup/RelativePathsCleanup.java | 22 ++----------------- .../org/jabref/model/entry/LinkedFile.java | 5 +++-- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java b/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java index 007787ef3d6..1bbecb2f41c 100644 --- a/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java +++ b/src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java @@ -34,8 +34,8 @@ public List cleanup(BibEntry entry) { for (LinkedFile fileEntry : fileList) { String oldFileName = fileEntry.getLink(); String newFileName = null; - if (isWebUrl(oldFileName)) { - // let web url untouched + if (fileEntry.isOnlineLink()) { + // keep online link untouched newFileName = oldFileName; } else { @@ -63,22 +63,4 @@ public List cleanup(BibEntry entry) { return Collections.emptyList(); } - - /** - * Checks, if the given file path is a well-formed web url - * - * @param filePath file path to check - * @return true, if the given file path is a web url, false otherwise - */ - private static boolean isWebUrl(String filePath) - { - if (filePath == null) { - return false; - } - String normalizedFilePath = filePath.trim().toLowerCase(); - if (normalizedFilePath.startsWith("http://") || normalizedFilePath.startsWith("https://")) { // INFO: can be extended, if needed - return true; - } - return false; - } } diff --git a/src/main/java/org/jabref/model/entry/LinkedFile.java b/src/main/java/org/jabref/model/entry/LinkedFile.java index 62a71dac417..3c79ffd6812 100644 --- a/src/main/java/org/jabref/model/entry/LinkedFile.java +++ b/src/main/java/org/jabref/model/entry/LinkedFile.java @@ -135,10 +135,11 @@ private void readObject(ObjectInputStream in) throws IOException { /** * Checks if the given String is an online link * @param toCheck The String to check - * @return True if it starts with http://, https:// or contains www; false otherwise + * @return true, if it starts with "http://", "https://" or contains "www."; false otherwise */ private boolean isOnlineLink(String toCheck) { - return toCheck.startsWith("http://") || toCheck.startsWith("https://") || toCheck.contains("www."); + String normalizedFilePath = toCheck.trim().toLowerCase(); + return normalizedFilePath.startsWith("http://") || normalizedFilePath.startsWith("https://") || normalizedFilePath.contains("www."); } @Override