Skip to content

Commit 1319bab

Browse files
Kaan0029Krishna Kumar Parthasarathy
authored and
Krishna Kumar Parthasarathy
committed
Fix: Allow pasting PDF URLs into main table to create entries (JabRef#12911)
* Improve handling of PDF file import behavior * Add support for handling PDF URLs when pasted * Revert submodule changes * Replace Collections.emptyList() with List.of() and improve exception handling * Change log level from debug to trace for PDF URL * Registered fix in changelog * Used getValidFileName for helper method deriveFileNameFromUrl * Refactor PDF URL detection using FileUtil.isPDFFile * Move getFileNameFromURL helper method to URLUtil class
1 parent 2057925 commit 1319bab

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
105105
- 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)
106106
- We fixed an issue where the tooltip only displayed the first linked file when hovering. [#12470](https://github.com/JabRef/jabref/issues/12470)
107107
- 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)
108+
- 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)
108109
- 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)
109110

110111
### Removed

src/main/java/org/jabref/gui/externalfiles/ImportHandler.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838
import org.jabref.logic.importer.ParseException;
3939
import org.jabref.logic.importer.ParserResult;
4040
import org.jabref.logic.importer.fileformat.BibtexParser;
41+
import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter;
4142
import org.jabref.logic.l10n.Localization;
43+
import org.jabref.logic.net.URLDownload;
4244
import org.jabref.logic.util.BackgroundTask;
45+
import org.jabref.logic.util.StandardFileType;
4346
import org.jabref.logic.util.TaskExecutor;
47+
import org.jabref.logic.util.URLUtil;
4448
import org.jabref.logic.util.UpdateField;
4549
import org.jabref.logic.util.io.FileUtil;
4650
import org.jabref.model.FieldChange;
@@ -371,7 +375,7 @@ public List<BibEntry> handleBibTeXData(String entries) {
371375
return result;
372376
} catch (ParseException ex) {
373377
LOGGER.error("Could not paste", ex);
374-
return Collections.emptyList();
378+
return List.of();
375379
}
376380
}
377381

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

398402
public List<BibEntry> handleStringData(String data) throws FetcherException {
399403
if ((data == null) || data.isEmpty()) {
400-
return Collections.emptyList();
404+
return List.of();
405+
}
406+
LOGGER.trace("Checking if URL is a PDF: {}", data);
407+
408+
if (URLUtil.isURL(data)) {
409+
String fileName = data.substring(data.lastIndexOf('/') + 1);
410+
if (FileUtil.isPDFFile(Path.of(fileName))) {
411+
try {
412+
return handlePdfUrl(data);
413+
} catch (IOException ex) {
414+
LOGGER.error("Could not handle PDF URL", ex);
415+
return List.of();
416+
}
417+
}
401418
}
402419

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

@@ -444,4 +461,44 @@ public void importEntriesWithDuplicateCheck(BibDatabaseContext database, List<Bi
444461
}
445462
}
446463
}
464+
465+
private List<BibEntry> handlePdfUrl(String pdfUrl) throws IOException {
466+
Optional<Path> targetDirectory = bibDatabaseContext.getFirstExistingFileDir(preferences.getFilePreferences());
467+
if (targetDirectory.isEmpty()) {
468+
LOGGER.warn("File directory not available while downloading {}.", pdfUrl);
469+
return List.of();
470+
}
471+
URLDownload urlDownload = new URLDownload(pdfUrl);
472+
String filename = URLUtil.getFileNameFromUrl(pdfUrl);
473+
Path targetFile = targetDirectory.get().resolve(filename);
474+
try {
475+
urlDownload.toFile(targetFile);
476+
} catch (FetcherException fe) {
477+
LOGGER.error("Error downloading PDF from URL", fe);
478+
return List.of();
479+
}
480+
try {
481+
PdfMergeMetadataImporter importer = new PdfMergeMetadataImporter(preferences.getImportFormatPreferences());
482+
ParserResult parserResult = importer.importDatabase(targetFile, bibDatabaseContext, preferences.getFilePreferences());
483+
if (parserResult.hasWarnings()) {
484+
LOGGER.warn("PDF import had warnings: {}", parserResult.getErrorMessage());
485+
}
486+
List<BibEntry> entries = parserResult.getDatabase().getEntries();
487+
if (!entries.isEmpty()) {
488+
entries.forEach(entry -> {
489+
if (entry.getFiles().isEmpty()) {
490+
entry.addFile(new LinkedFile("", targetFile, StandardFileType.PDF.getName()));
491+
}
492+
});
493+
} else {
494+
BibEntry emptyEntry = new BibEntry();
495+
emptyEntry.addFile(new LinkedFile("", targetFile, StandardFileType.PDF.getName()));
496+
entries.add(emptyEntry);
497+
}
498+
return entries;
499+
} catch (IOException ex) {
500+
LOGGER.error("Error importing PDF from URL - IO issue", ex);
501+
return List.of();
502+
}
503+
}
447504
}

src/main/java/org/jabref/logic/util/URLUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Objects;
1010
import java.util.regex.Pattern;
1111

12+
import org.jabref.logic.util.io.FileUtil;
13+
1214
/**
1315
* URL utilities for URLs in the JabRef logic.
1416
* <p>
@@ -122,4 +124,19 @@ public static URI createUri(String url) {
122124
throw new IllegalArgumentException(e);
123125
}
124126
}
127+
128+
/**
129+
* Extracts the filename from a URL.
130+
* If the URL doesn't have a filename (ends with '/'), returns a default name.
131+
*
132+
* @param url the URL string to extract the filename from
133+
* @return the extracted filename or a default name if none found
134+
*/
135+
public static String getFileNameFromUrl(String url) {
136+
String fileName = url.substring(url.lastIndexOf('/') + 1);
137+
if (fileName.isBlank()) {
138+
fileName = "downloaded.pdf";
139+
}
140+
return FileUtil.getValidFileName(fileName);
141+
}
125142
}

0 commit comments

Comments
 (0)