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

Fixes making paths of linked files relative (web urls will not be touched anymore) #5879

Merged
merged 3 commits into from
Jan 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 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 (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());
Expand All @@ -57,4 +64,21 @@ public List<FieldChange> 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 <code>true</code>, if the given file path is a web url, <code>false</code> otherwise
*/
private static boolean isWebUrl(String filePath)
Copy link
Member

Choose a reason for hiding this comment

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

The class LinkedFile already contains a method that does this job:

public boolean isOnlineLink() {
return isOnlineLink(link.get());
}

Can you please use it. I would also suggest to add the normalization trim().toLowerCase() there, which I find a nice addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Concerning isOnlineLink(): I would recommend removing "toCheck.contains("www.")", because a path to a local file might contain this pattern coincidentally. What do you say?

{
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;
}
}