Search in sources :

Example 36 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMailEventHandler method handlePathEvent.

private void handlePathEvent(MCREvent evt, Path file, BasicFileAttributes attrs) {
    if (!(file instanceof MCRPath)) {
        return;
    }
    MCRPath path = MCRPath.toMCRPath(file);
    MCRContent xml;
    try {
        xml = new MCRJDOMContent(MCRPathXML.getFileXML(path, attrs));
        handleEvent(evt, xml, path.toString());
    } catch (IOException e) {
        LOGGER.error("Error while generating mail for {}", file, e);
    }
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 37 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRXMLFunctions method getSize.

/**
 * Method returns the amount of space consumed by the files contained in the
 * derivate container. The returned string is already formatted meaning it
 * has already the optimal measurement unit attached (e.g. 142 MB, ).
 *
 * @param derivateId
 *            the derivate id for which the size should be returned
 * @return the size as formatted string
 */
public static String getSize(String derivateId) throws IOException {
    MCRPath rootPath = MCRPath.getPath(derivateId, "/");
    final AtomicLong size = new AtomicLong();
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            size.addAndGet(attrs.size());
            return super.visitFile(file, attrs);
        }
    });
    return MCRUtils.getSizeFormatted(size.get());
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) XPath(javax.xml.xpath.XPath) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 38 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRCompressServlet method sendDerivate.

private void sendDerivate(MCRObjectID id, String path, T container) throws IOException {
    MCRPath resolvedPath = MCRPath.getPath(id.toString(), path == null ? "/" : path);
    if (!Files.exists(resolvedPath)) {
        throw new NoSuchFileException(id.toString(), path, "Could not find path " + resolvedPath);
    }
    if (Files.isRegularFile(resolvedPath)) {
        BasicFileAttributes attrs = Files.readAttributes(resolvedPath, BasicFileAttributes.class);
        sendCompressedFile(resolvedPath, attrs, container);
        LOGGER.debug("file {} zipped", resolvedPath);
        return;
    }
    // root is a directory
    Files.walkFileTree(resolvedPath, new CompressVisitor<>(this, container));
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 39 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRIViewZipResource method zip.

/**
 * Zips a derivate and its containing iview images as jpg's. All other files are ignored.
 *
 * @param derivateID the derivate to zip
 * @param zoom if undefined the base resolution is assumed
 * @return zip file
 */
@GET
@Produces("application/zip")
@Path("{derivateID}")
public Response zip(@PathParam("derivateID") String derivateID, @QueryParam("zoom") Integer zoom) throws Exception {
    if (!MCRAccessManager.checkPermissionForReadingDerivate(derivateID)) {
        throw new WebApplicationException(Status.UNAUTHORIZED);
    }
    MCRPath derivateRoot = MCRPath.getPath(derivateID, "/");
    if (!Files.exists(derivateRoot)) {
        throw new WebApplicationException(Status.NOT_FOUND);
    }
    ZipStreamingOutput stream = new ZipStreamingOutput(derivateRoot, zoom);
    return Response.ok(stream).header("Content-Disposition", "attachnment; filename=\"" + derivateID + ".zip\"").build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 40 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRIView2Tools method getFilePath.

public static String getFilePath(String derID, String derPath) throws IOException {
    MCRPath mcrPath = MCRPath.getPath(derID, derPath);
    Path physicalPath = mcrPath.toPhysicalPath();
    for (FileStore fs : mcrPath.getFileSystem().getFileStores()) {
        if (fs instanceof MCRAbstractFileStore) {
            Path basePath = ((MCRAbstractFileStore) fs).getBaseDirectory();
            if (physicalPath.startsWith(basePath)) {
                return basePath.relativize(physicalPath).toString();
            }
        }
    }
    return physicalPath.toString();
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRAbstractFileStore(org.mycore.datamodel.niofs.MCRAbstractFileStore) FileStore(java.nio.file.FileStore) MCRAbstractFileStore(org.mycore.datamodel.niofs.MCRAbstractFileStore) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10