Skip to content

Fix: Allow pasting PDF URLs into main table to create entries #12911

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

Merged
merged 11 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where valid DOI could not be imported if it had special characters like `<` or `>`. [#12434](https://github.com/JabRef/jabref/issues/12434)
- We fixed an issue where the tooltip only displayed the first linked file when hovering. [#12470](https://github.com/JabRef/jabref/issues/12470)
- We fixed an issue where some texts in the "Citation Information" tab and the "Preferences" dialog could not be translated. [#12883](https://github.com/JabRef/jabref/pull/12883)
- We fixed an issue where pasting a PDF URL into the main table caused an import error instead of creating a new entry. [#12911](https://github.com/JabRef/jabref/pull/12911)
- We fixed an issue where libraries would sometimes be hidden when closing tabs with the Welcome tab open. [#12894](https://github.com/JabRef/jabref/issues/12894)

### Removed
Expand Down
63 changes: 60 additions & 3 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BackgroundTask;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.logic.util.URLUtil;
import org.jabref.logic.util.UpdateField;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.FieldChange;
Expand Down Expand Up @@ -371,7 +375,7 @@ public List<BibEntry> handleBibTeXData(String entries) {
return result;
} catch (ParseException ex) {
LOGGER.error("Could not paste", ex);
return Collections.emptyList();
return List.of();
}
}

Expand All @@ -397,7 +401,20 @@ public void importStringConstantsWithDuplicateCheck(Collection<BibtexString> str

public List<BibEntry> handleStringData(String data) throws FetcherException {
if ((data == null) || data.isEmpty()) {
return Collections.emptyList();
return List.of();
}
LOGGER.trace("Checking if URL is a PDF: {}", data);

if (URLUtil.isURL(data)) {
String fileName = data.substring(data.lastIndexOf('/') + 1);
if (FileUtil.isPDFFile(Path.of(fileName))) {
try {
return handlePdfUrl(data);
} catch (IOException ex) {
LOGGER.error("Could not handle PDF URL", ex);
return List.of();
}
}
}

if (!CompositeIdFetcher.containsValidId(data)) {
Expand All @@ -421,7 +438,7 @@ private List<BibEntry> tryImportFormats(String data) {
return unknownFormatImport.parserResult().getDatabase().getEntries();
} catch (ImportException ex) { // ex is already localized
dialogService.showErrorDialogAndWait(Localization.lang("Import error"), ex);
return Collections.emptyList();
return List.of();
}
}

Expand All @@ -444,4 +461,44 @@ public void importEntriesWithDuplicateCheck(BibDatabaseContext database, List<Bi
}
}
}

private List<BibEntry> handlePdfUrl(String pdfUrl) throws IOException {
Optional<Path> targetDirectory = bibDatabaseContext.getFirstExistingFileDir(preferences.getFilePreferences());
if (targetDirectory.isEmpty()) {
LOGGER.warn("File directory not available while downloading {}.", pdfUrl);
return List.of();
}
URLDownload urlDownload = new URLDownload(pdfUrl);
String filename = URLUtil.getFileNameFromUrl(pdfUrl);
Path targetFile = targetDirectory.get().resolve(filename);
try {
urlDownload.toFile(targetFile);
} catch (FetcherException fe) {
LOGGER.error("Error downloading PDF from URL", fe);
return List.of();
}
try {
PdfMergeMetadataImporter importer = new PdfMergeMetadataImporter(preferences.getImportFormatPreferences());
ParserResult parserResult = importer.importDatabase(targetFile, bibDatabaseContext, preferences.getFilePreferences());
if (parserResult.hasWarnings()) {
LOGGER.warn("PDF import had warnings: {}", parserResult.getErrorMessage());
}
List<BibEntry> entries = parserResult.getDatabase().getEntries();
if (!entries.isEmpty()) {
entries.forEach(entry -> {
if (entry.getFiles().isEmpty()) {
entry.addFile(new LinkedFile("", targetFile, StandardFileType.PDF.getName()));
}
});
} else {
BibEntry emptyEntry = new BibEntry();
emptyEntry.addFile(new LinkedFile("", targetFile, StandardFileType.PDF.getName()));
entries.add(emptyEntry);
}
return entries;
} catch (IOException ex) {
LOGGER.error("Error importing PDF from URL - IO issue", ex);
return List.of();
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/jabref/logic/util/URLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Objects;
import java.util.regex.Pattern;

import org.jabref.logic.util.io.FileUtil;

/**
* URL utilities for URLs in the JabRef logic.
* <p>
Expand Down Expand Up @@ -122,4 +124,19 @@ public static URI createUri(String url) {
throw new IllegalArgumentException(e);
}
}

/**
* Extracts the filename from a URL.
* If the URL doesn't have a filename (ends with '/'), returns a default name.
*
* @param url the URL string to extract the filename from
* @return the extracted filename or a default name if none found
*/
public static String getFileNameFromUrl(String url) {
String fileName = url.substring(url.lastIndexOf('/') + 1);
if (fileName.isBlank()) {
fileName = "downloaded.pdf";
}
return FileUtil.getValidFileName(fileName);
}
}