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

MIR-1249 DeepGreen wrong pdf uploaded #912

Merged
merged 1 commit into from
Oct 30, 2023
Merged
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 @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
Expand Down Expand Up @@ -50,26 +51,37 @@ public MCRObjectID ingestMetadata(Deposit deposit) throws SwordError, SwordServe
.createTempFileFromStream("dgZip", deposit.getInputStream(), deposit.getMd5());

LOGGER.info("Zip File is : " + dgZip.toAbsolutePath().toString());
LOGGER.info("Deposit Filename is : " + deposit.getFilename());
try (SeekableByteChannel sbc = Files.newByteChannel(dgZip)) {
final ZipFile zipFile = new ZipFile(sbc);
final List<ZipArchiveEntry> entriesInPhysicalOrder = Collections
.list(zipFile.getEntriesInPhysicalOrder());

final Optional<ZipArchiveEntry> metadataEntryOpt = entriesInPhysicalOrder.stream()
.filter(e -> e.getName().endsWith(".xml")).findFirst();
final Optional<ZipArchiveEntry> pdfEntryOpt = entriesInPhysicalOrder.stream()
.filter(e -> e.getName().endsWith(".pdf")).findFirst();

if (metadataEntryOpt.isEmpty()) {
throw new SwordServerException("No Metadata File Found!");
}
final ZipArchiveEntry metadataEntry = metadataEntryOpt.get();
String filenameWithoutExt = FilenameUtils.getBaseName(metadataEntry.getName());
LOGGER.info("Metadata Filename is : " + filenameWithoutExt);

Optional<ZipArchiveEntry> pdfEntryOpt = entriesInPhysicalOrder.stream()
.filter(e -> e.getName().contains(filenameWithoutExt) && e.getName().endsWith(".pdf")).findFirst();
if (pdfEntryOpt.isEmpty()) {
throw new SwordServerException("No PDF File Found!");
LOGGER.info("No PDF File Found, with Filename " + filenameWithoutExt + "! Using first PDF File!");
pdfEntryOpt = entriesInPhysicalOrder.stream()
.filter(e -> e.getName().endsWith(".pdf")).findFirst();
if (pdfEntryOpt.isEmpty()) {
throw new SwordServerException("No PDF File Found!");
}
}

final ZipArchiveEntry metadataEntry = metadataEntryOpt.get();
final ZipArchiveEntry pdfEntry = pdfEntryOpt.get();

List<ZipArchiveEntry> otherEntryList = entriesInPhysicalOrder.stream()
.filter(e -> !e.getName().equals(metadataEntry.getName())
&& !e.getName().equals(pdfEntry.getName())).collect(Collectors.toList());

final MCRThrowFunction<ZipArchiveEntry, InputStream, IOException> getIS = zipFile::getInputStream;
try (InputStream metadataIS = getIS.apply(metadataEntry); InputStream pdfIS = getIS.apply(pdfEntry)) {
final SAXBuilder saxBuilder = new SAXBuilder();
Expand Down Expand Up @@ -102,6 +114,16 @@ public MCRObjectID ingestMetadata(Deposit deposit) throws SwordError, SwordServe
);
derivate.getDerivate().getClassifications().add(derivateTypeClassification);
}
if (!otherEntryList.isEmpty()) {
otherEntryList.forEach(e -> {
try (InputStream otherDataIS = getIS.apply(e)) {
Files.copy(otherDataIS, MCRPath.getPath(derivate.getId().toString(),
FilenameUtils.getName(e.getName())));
} catch (IOException ex) {
LOGGER.error("Error while processing File " + e.getName());
}
});
}
MCRMetadataManager.update(derivate);
return newObjectId;
} catch (SAXException | JDOMException | MCRAccessException e) {
Expand Down
Loading