Skip to content

Commit

Permalink
Fixes making paths of linked files relative (web urls will not be tou…
Browse files Browse the repository at this point in the history
…ched anymore) (#5879)

* 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.

* changelog updated

* refactoring: use existing method isOnlineLink() for checking whether a file link is an online link; improving isOnlineLink()
  • Loading branch information
systemoperator authored and koppor committed Jan 30, 2020
1 parent fdcae48 commit a68cd0c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/jabref/logic/cleanup/RelativePathsCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ public List<FieldChange> 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 (fileEntry.isOnlineLink()) {
// keep online link 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());
Expand All @@ -56,5 +63,4 @@ public List<FieldChange> cleanup(BibEntry entry) {

return Collections.emptyList();
}

}
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/model/entry/LinkedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>true</code>, if it starts with "http://", "https://" or contains "www."; <code>false</code> 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
Expand Down

0 comments on commit a68cd0c

Please sign in to comment.