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

[WIP] This relativizes the PDF's filepaths after importing through "Find Unlinked Files" #10582

Closed
wants to merge 13 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ protected List<ImportFilesResultItemViewModel> call() {

try {
if (FileUtil.isPDFFile(file)) {
var pdfImporterResult = contentImporter.importPDFContent(file);
var filePreferences = preferencesService.getFilePreferences();
Path workingDir = bibDatabaseContext.getFirstExistingFileDir(filePreferences)
.orElse(filePreferences.getWorkingDirectory());
Comment on lines +120 to +121
Copy link
Member

Choose a reason for hiding this comment

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

JabRef's logic of storing files is more complicated.

JabRef searchers in EACH directory, not in ONE.

See org.jabref.logic.util.io.FileUtil#getListOfLinkedFiles.

var pdfImporterResult = contentImporter.importPDFContent(file, workingDir);
List<BibEntry> pdfEntriesInFile = pdfImporterResult.getDatabase().getEntries();

if (pdfImporterResult.hasWarnings()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public ExternalFilesContentImporter(ImportFormatPreferences importFormatPreferen
this.importFormatPreferences = importFormatPreferences;
}

public ParserResult importPDFContent(Path file) {
public ParserResult importPDFContent(Path file, Path workingDir) {
try {
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file);
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file, workingDir);
} catch (IOException e) {
return ParserResult.fromError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public ParserResult importDatabase(String data) throws IOException {

@Override
public ParserResult importDatabase(Path filePath) throws IOException {
return importDatabase(filePath, null);
}

/**
* Same as {@link PdfMergeMetadataImporter#importDatabase(Path)}, but this
* relativizes the {@code filePath}, if {@code workingDir} is not null
* (otherwise no path modification happens).
*
* @param filePath the path to the file which should be imported
* @param workingDir one of the directories
* {@link BibDatabaseContext#getFileDirectories(FilePreferences) mentioned here}
* set up for this database
*/
public ParserResult importDatabase(Path filePath, Path workingDir) throws IOException {
List<BibEntry> candidates = new ArrayList<>();

for (Importer metadataImporter : metadataImporters) {
Expand Down Expand Up @@ -124,7 +138,9 @@ public ParserResult importDatabase(Path filePath) throws IOException {
}
}
}

if (workingDir != null) {
filePath = workingDir.relativize(filePath);
}
Comment on lines +141 to +143
Copy link
Member

Choose a reason for hiding this comment

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

Call org.jabref.logic.util.io.FileUtil#relativize here.

The required directories are provided by databaseContext.getFileDirectories(filePreferences)

entry.addFile(new LinkedFile("", filePath, StandardFileType.PDF.getName()));
return new ParserResult(List.of(entry));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,19 @@ void importWorksAsExpected() throws Exception {

assertEquals(Collections.singletonList(expected), result);
}

@Test
void pathShouldBeRelativized() throws Exception {
Path file = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/minimal.pdf").toURI());
Path workingDir = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/").toURI());
List<BibEntry> result = importer.importDatabase(file, workingDir).getDatabase().getEntries();

BibEntry expected = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "1 ")
.withField(StandardField.TITLE, "Hello World")
// Expecting relative path
.withField(StandardField.FILE, ":minimal.pdf:PDF");

assertEquals(List.of(expected), result);
}
}
Loading