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

Remove download when html file #11227

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
### Changed

- We replaced the word "Key bindings" with "Keyboard shortcuts" in the Preferences tab. [#11153](https://github.com/JabRef/jabref/pull/11153)
- We slightly improved the duplicate check if ISBN numbers are present. [#8885](https://github.com/JabRef/jabref/issues/8885)
- We slightly improved the duplicate check if ISBNs are present. [#8885](https://github.com/JabRef/jabref/issues/8885)
- JabRef no longer downloads HTML files of websites when a PDF was not found [#10149](https://github.com/JabRef/jabref/issues/10149)

### Fixed

- We fixed an issue where entry type with duplicate fields prevented opening existing libraries with custom entry types [#11127](https://github.com/JabRef/jabref/issues/11127)
- We fixed an issue when the file was flagged as changed on disk in the case of content selectors or groups. [#9064](https://github.com/JabRef/jabref/issues/9064)
- We fixed crash on opening the entry editor when auto completion is enabled. [#11188](https://github.com/JabRef/jabref/issues/11188)
- We fixed crash on opening the entry editor when auto-completion is enabled. [#11188](https://github.com/JabRef/jabref/issues/11188)
- We fixed the usage of the key binding for "Clear search" (default: <kbd>Escape</kbd>). [#10764](https://github.com/JabRef/jabref/issues/10764)
- We fixed an issue where library shown as unsaved and marked (*) after accepting changes made externally to the file. [#11027](https://github.com/JabRef/jabref/issues/11027)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -93,7 +95,7 @@ public DownloadLinkedFileAction(BibDatabaseContext databaseContext,

@Override
public void execute() {
LOGGER.info("Downloading file from " + downloadUrl);
LOGGER.info("Downloading file from {}", downloadUrl);
if (downloadUrl.isEmpty() || !LinkedFile.isOnlineLink(downloadUrl)) {
throw new UnsupportedOperationException("In order to download the file, the url has to be an online link");
}
Expand Down Expand Up @@ -164,19 +166,22 @@ private void onSuccess(Path targetDirectory, Path destination) {
} else {
newLinkedFile.setSourceURL(linkedFile.getSourceUrl());
}
entry.replaceDownloadedFile(linkedFile.getLink(), newLinkedFile);
isHtml = newLinkedFile.getFileType().equals(StandardExternalFileType.URL.getName());
if (!isHtml) {
entry.replaceDownloadedFile(linkedFile.getLink(), newLinkedFile);
}
}

// Notify in bar when the file type is HTML.
if (isHtml) {
dialogService.notify(Localization.lang("Downloaded website as an HTML file."));
LOGGER.debug("Downloaded website {} as an HTML file at {}", linkedFile.getLink(), destination);
dialogService.notify(Localization.lang("Download was a HTML file. Not adding it."));
List<LinkedFile> newFiles = new ArrayList<>(entry.getFiles());
newFiles.remove(linkedFile);
entry.setFiles(newFiles);
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure about this one if there are multiple files attached and only one leads to HTML. Need to see the full source.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm only removing the current linked File from he current download.
I am getting all existing files from the entry and just remove the current one
To trigger change detection I have to reassign the list

}
}

private void onFailure(URLDownload urlDownload, Exception ex) {
LOGGER.error("Error downloading from URL: " + urlDownload, ex);
LOGGER.error("Error downloading from URL: {}", urlDownload, ex);
String fetcherExceptionMessage = ex.getMessage();
String failedTitle = Localization.lang("Failed to download from URL");
int statusCode;
Expand Down Expand Up @@ -261,7 +266,7 @@ private Optional<ExternalFileType> inferFileTypeFromMimeType(URLDownload urlDown
String mimeType = urlDownload.getMimeType();

if (mimeType != null) {
LOGGER.debug("MIME Type suggested: " + mimeType);
LOGGER.debug("MIME Type suggested: {}", mimeType);
return ExternalFileTypes.getExternalFileTypeByMimeType(mimeType, filePreferences);
} else {
return Optional.empty();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Donate\ to\ JabRef=Donate to JabRef

Download\ file=Download file

Downloaded\ website\ as\ an\ HTML\ file.=Downloaded website as an HTML file.
Download\ was\ a\ HTML\ file.\ Not\ adding\ it.=Download was a HTML file. Not adding it.

duplicate\ removal=duplicate removal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,37 +196,7 @@ void downloadHtmlFileCausesWarningDisplay() throws MalformedURLException {

viewModel.download();

verify(dialogService, atLeastOnce()).notify("Downloaded website as an HTML file.");
}

@FetcherTest
@Test
void downloadHtmlWhenLinkedFilePointsToHtml() throws MalformedURLException {
// use google as test url, wiley is protected by CloudFlare
String url = "https://google.com";
String fileType = StandardExternalFileType.URL.getName();
linkedFile = new LinkedFile(new URL(url), fileType);

when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(true);
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]");
when(filePreferences.getFileDirectoryPattern()).thenReturn("[entrytype]");

databaseContext.setDatabasePath(tempFile);

LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences);

viewModel.download();

List<LinkedFile> linkedFiles = entry.getFiles();

for (LinkedFile file: linkedFiles) {
if ("Misc/asdf.html".equalsIgnoreCase(file.getLink())) {
assertEquals("URL", file.getFileType());
return;
}
}
// If the file was not found among the linked files to the entry
fail();
verify(dialogService, atLeastOnce()).notify("Download was a HTML file. Not adding it.");
}

@Test
Expand Down
Loading