use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRSwordUtil method getZippedDerivateMediaResource.
public static MediaResource getZippedDerivateMediaResource(String object) {
final Path tempFile;
try {
tempFile = Files.createTempFile("swordv2_", ".temp.zip");
} catch (IOException e) {
throw new MCRException("Could not create temp file!", e);
}
try (final OutputStream tempFileStream = Files.newOutputStream(tempFile)) {
final ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFileStream);
zipOutputStream.setLevel(Deflater.BEST_COMPRESSION);
final MCRPath root = MCRPath.getPath(object, "/");
addDirectoryToZip(zipOutputStream, root);
zipOutputStream.close();
} catch (IOException e) {
throw new MCRException(e);
}
MediaResource resultRessource;
InputStream is;
try {
is = new MCRDeleteFileOnCloseFilterInputStream(Files.newInputStream(tempFile), tempFile);
resultRessource = new MediaResource(is, MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP, UriRegistry.PACKAGE_SIMPLE_ZIP);
} catch (IOException e) {
throw new MCRException("could not read from temp file!", e);
}
return resultRessource;
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRDOIRegistrationService method getMediaList.
/**
* Builds a list with with right content types and media urls assigned of a specific Object
* @param obj the object
* @return a list of entrys Media-Type, URL
*/
public List<Map.Entry<String, URI>> getMediaList(MCRObject obj) {
List<Map.Entry<String, URI>> entryList = new ArrayList<>();
Optional<MCRObjectID> derivateIdOptional = MCRMetadataManager.getDerivateIds(obj.getId(), 1, TimeUnit.MINUTES).stream().findFirst();
derivateIdOptional.ifPresent(derivateId -> {
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
String mainDoc = derivate.getDerivate().getInternals().getMainDoc();
MCRPath mainDocumentPath = MCRPath.getPath(derivateId.toString(), mainDoc);
try {
String contentType = Optional.ofNullable(MCRContentTypes.probeContentType(mainDocumentPath)).orElse("application/octet-stream");
entryList.add(new AbstractMap.SimpleEntry<>(contentType, new URI(this.registerURL + MCRXMLFunctions.encodeURIPath("/servlets/MCRFileNodeServlet/" + derivateId + "/" + mainDoc))));
} catch (IOException | URISyntaxException e) {
LOGGER.error("Error while detecting the file to register!", e);
}
});
return entryList;
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMetsIIIFPresentationImpl method getMets.
public Document getMets(String id) throws IOException, JDOMException, SAXException {
String objectid = MCRLinkTableManager.instance().getSourceOf(id).iterator().next();
MCRContentTransformer transformer = getTransformer();
MCRParameterCollector parameter = new MCRParameterCollector();
if (objectid != null && objectid.length() != 0) {
MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(id));
MCRObjectID ownerID = derObj.getOwnerID();
objectid = ownerID.toString();
parameter.setParameter("objectID", objectid);
parameter.setParameter("derivateID", id);
}
MCRPath metsPath = MCRPath.getPath(id, "mets.xml");
if (!Files.exists(metsPath)) {
throw new MCRException("File not found: " + id);
}
MCRPathContent source = new MCRPathContent(metsPath);
MCRContent content = transformer instanceof MCRParameterizedTransformer ? ((MCRParameterizedTransformer) transformer).transform(source, parameter) : transformer.transform(source);
return content.asXML();
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMETSDefaultGenerator method structureMets.
private void structureMets(MCRPath dir, Set<MCRPath> ignoreNodes, FileSec fileSec, PhysicalDiv physicalDiv, LogicalDiv logicalDiv, StructLink structLink, int logOrder) throws IOException {
SortedMap<MCRPath, BasicFileAttributes> files = new TreeMap<>(), directories = new TreeMap<>();
fillFileMap(ignoreNodes, files, directories, dir);
for (Map.Entry<MCRPath, BasicFileAttributes> file : files.entrySet()) {
createStructure(dir, fileSec, physicalDiv, logicalDiv, structLink, file);
}
for (Map.Entry<MCRPath, BasicFileAttributes> directory : directories.entrySet()) {
String dirName = directory.getKey().getFileName().toString();
if (isInExcludedRootFolder(directory.getKey())) {
structureMets(directory.getKey(), ignoreNodes, fileSec, physicalDiv, logicalDiv, structLink, logOrder);
} else {
LogicalDiv section = new LogicalDiv("log_" + Integer.toString(++logOrder), "section", dirName);
logicalDiv.add(section);
structureMets(directory.getKey(), ignoreNodes, fileSec, physicalDiv, section, structLink, logOrder);
}
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMETSDefaultGenerator method fillFileMap.
private void fillFileMap(Set<MCRPath> ignoreNodes, SortedMap<MCRPath, BasicFileAttributes> files, SortedMap<MCRPath, BasicFileAttributes> directories, Path dir) throws IOException {
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir)) {
for (Path child : dirStream) {
MCRPath path = MCRPath.toMCRPath(child);
if (ignoreNodes.contains(path)) {
continue;
}
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
if (attrs.isDirectory()) {
directories.put(path, attrs);
} else {
files.put(path, attrs);
}
}
}
}
Aggregations