Search in sources :

Example 81 with MCRPath

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

the class MCRMETSGeneratorFactory method getOldMets.

private static Optional<Mets> getOldMets(MCRPath derivatePath) throws IOException, JDOMException {
    if (derivatePath == null) {
        return Optional.empty();
    }
    Path metsPath = derivatePath.resolve("mets.xml");
    if (!Files.exists(metsPath)) {
        return Optional.empty();
    }
    SAXBuilder builder = new SAXBuilder();
    try (InputStream is = Files.newInputStream(metsPath)) {
        Document metsDocument = builder.build(is);
        return Optional.of(new Mets(metsDocument));
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) SAXBuilder(org.jdom2.input.SAXBuilder) InputStream(java.io.InputStream) Document(org.jdom2.Document)

Example 82 with MCRPath

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

the class MCRMETSHierarchyGenerator method createFileSection.

/**
 * Creates the file section.
 *
 * @return generated file secion.
 */
protected FileSec createFileSection() throws IOException {
    FileSec fileSec = new FileSec();
    List<MCRPath> filePaths = MCRMetsSave.listFiles(getDerivatePath(), getIgnorePaths());
    List<FileGrp> fileGrps = MCRMetsSave.buildFileGroups(filePaths);
    fileGrps.forEach(fileSec::addFileGrp);
    for (MCRPath file : filePaths) {
        String contentType = MCRContentTypes.probeContentType(file);
        FileRef ref = new FileRef(file, contentType);
        this.files.add(ref);
    }
    for (FileRef ref : this.files) {
        MCRMetsFileUse use = MCRMetsFileUse.get(ref.path);
        FileGrp fileGrp = fileGrps.stream().filter(grp -> grp.getUse().equals(use.toString())).findFirst().orElse(null);
        if (fileGrp == null) {
            LOGGER.warn("Unable to add file '" + ref.toId() + "' because cannot find corresponding group " + " with @USE='" + use.toString() + "'. Ignore file and continue.");
            continue;
        }
        addFile(ref.toId(), fileGrp, ref.getPath(), ref.getContentType());
    }
    return fileSec;
}
Also used : FileSec(org.mycore.mets.model.files.FileSec) FileGrp(org.mycore.mets.model.files.FileGrp) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse)

Example 83 with MCRPath

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

the class MCRImageThumbnailGenerator method getThumbnail.

@Override
public Optional<BufferedImage> getThumbnail(MCRPath path, int size) throws IOException {
    Path iviewFile = MCRImage.getTiledFile(MCRIView2Tools.getTileDir(), path.getOwner(), path.getFileName().toString());
    MCRTiledPictureProps iviewFileProps = getIviewFileProps(iviewFile);
    final double width = iviewFileProps.getWidth();
    final double height = iviewFileProps.getHeight();
    final int newWidth = width > height ? (int) Math.ceil(size * width / height) : size;
    final int newHeight = width > height ? size : (int) Math.ceil(size * height / width);
    // this value determines the zoom level!
    final double scale = newWidth / width;
    // We always want to use the the best needed zoom level!
    int sourceZoomLevel = (int) Math.min(Math.max(0, Math.ceil(iviewFileProps.getZoomlevel() - Math.log(scale) / Math.log(1.0 / 2.0))), iviewFileProps.getZoomlevel());
    // scale is the real scale which is needed! zoomLevelScale is the scale of the nearest zoom level!
    double zoomLevelScale = Math.min(1.0, Math.pow(0.5, iviewFileProps.getZoomlevel() - sourceZoomLevel));
    // this is the scale which is needed from the nearest zoom level to the required size of image
    double drawScale = (newWidth / (width * zoomLevelScale));
    try (FileSystem zipFileSystem = MCRIView2Tools.getFileSystem(iviewFile)) {
        Path rootPath = zipFileSystem.getPath("/");
        ImageReader imageReader = MCRIView2Tools.getTileImageReader();
        BufferedImage testTile = MCRIView2Tools.readTile(rootPath, imageReader, sourceZoomLevel, 0, 0);
        BufferedImage targetImage = getTargetImage(newWidth, newHeight, testTile);
        Graphics2D graphics = targetImage.createGraphics();
        graphics.scale(drawScale, drawScale);
        for (int x = 0; x < Math.ceil(width * zoomLevelScale / 256); x++) {
            for (int y = 0; y < Math.ceil(height * zoomLevelScale / 256); y++) {
                BufferedImage tile = MCRIView2Tools.readTile(rootPath, imageReader, sourceZoomLevel, x, y);
                graphics.drawImage(tile, x * 256, y * 256, null);
            }
        }
        return Optional.of(targetImage);
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 84 with MCRPath

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

the class MCRMediaSourceProvider method getContentPath.

private static String getContentPath(String derivateId, String path) {
    try {
        MCRPath mcrPath = MCRPath.getPath(derivateId, path);
        MCRAbstractFileStore fileStore = (MCRAbstractFileStore) Files.getFileStore(mcrPath);
        java.nio.file.Path absolutePath = fileStore.getPhysicalPath(mcrPath);
        java.nio.file.Path relativePath = fileStore.getBaseDirectory().relativize(absolutePath);
        LogManager.getLogger().info("{} -> {} -> {}", mcrPath, absolutePath, relativePath);
        return relativePath.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MCRAbstractFileStore(org.mycore.datamodel.niofs.MCRAbstractFileStore) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 85 with MCRPath

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

the class MCRMetsCommands method addMetsFileForDerivate.

@MCRCommand(syntax = "add mets files for derivate {0}", order = 20)
public static void addMetsFileForDerivate(String derivateID) {
    MCRPath metsFile = MCRPath.getPath(derivateID, "/mets.xml");
    if (!Files.exists(metsFile)) {
        try {
            LOGGER.debug("Start MCRMETSGenerator for derivate {}", derivateID);
            Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivateID, "/")).generate().asDocument();
            MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivateID));
            LOGGER.debug("Stop MCRMETSGenerator for derivate {}", derivateID);
        } catch (Exception e) {
            LOGGER.error("Can't create mets file for derivate {}", derivateID);
        }
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) ValidationException(org.mycore.mets.validator.validators.ValidationException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

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