Search in sources :

Example 6 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method getParentDirectory.

private static MCRDirectory getParentDirectory(MCRPath mcrPath) throws NoSuchFileException, NotDirectoryException {
    if (mcrPath.getNameCount() == 0) {
        throw new IllegalArgumentException("Root component has no parent: " + mcrPath);
    }
    MCRDirectory rootDirectory = getRootDirectory(mcrPath);
    if (mcrPath.getNameCount() == 1) {
        return rootDirectory;
    }
    MCRPath parentPath = mcrPath.getParent();
    MCRFilesystemNode parentNode = rootDirectory.getChildByPath(getAbsolutePathFromRootComponent(parentPath).toString());
    if (parentNode == null) {
        throw new NoSuchFileException(rootDirectory.toPath().toString(), getAbsolutePathFromRootComponent(mcrPath).toString(), "Parent directory does not exists.");
    }
    if (parentNode instanceof MCRFile) {
        throw new NotDirectoryException(parentNode.toPath().toString());
    }
    MCRDirectory parentDir = (MCRDirectory) parentNode;
    // warm-up cache
    parentDir.getChildren();
    return parentDir;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) NotDirectoryException(java.nio.file.NotDirectoryException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 7 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method getFileStore.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#getFileStore(java.nio.file.Path)
     */
@Override
public FileStore getFileStore(Path path) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    if (mcrPath.getNameCount() > 0) {
        MCRFilesystemNode node = resolvePath(mcrPath);
        if (node instanceof MCRFile) {
            MCRFile file = (MCRFile) node;
            String storeID = file.getStoreID();
            return MCRFileStore.getInstance(storeID);
        }
    }
    return MCRFileStore.getInstance(MCRContentStoreFactory.getDefaultStore().getID());
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 8 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile in project mycore by MyCoRe-Org.

the class MCRFileSystemUtils method getMCRFile.

static MCRFile getMCRFile(MCRPath ifsPath, boolean create, boolean createNew) throws IOException {
    if (!ifsPath.isAbsolute()) {
        throw new IllegalArgumentException("'path' needs to be absolute.");
    }
    MCRFile file;
    MCRDirectory root = null;
    boolean rootCreated = false;
    try {
        try {
            root = MCRFileSystemProvider.getRootDirectory(ifsPath);
        } catch (NoSuchFileException e) {
            if (create || createNew) {
                root = new MCRDirectory(ifsPath.getOwner());
                rootCreated = true;
            } else {
                throw e;
            }
        }
        MCRPath relativePath = root.toPath().relativize(ifsPath);
        file = getMCRFile(root, relativePath, create, createNew);
    } catch (Exception e) {
        if (rootCreated) {
            LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
            try {
                root.delete();
            } catch (Exception de) {
                LOGGER.fatal("Error while deleting file system node: {}", root.getName(), de);
            }
        }
        throw e;
    }
    return file;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath) NoSuchFileException(java.nio.file.NoSuchFileException) ProviderMismatchException(java.nio.file.ProviderMismatchException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InvalidPathException(java.nio.file.InvalidPathException)

Example 9 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile in project mycore by MyCoRe-Org.

the class MCRFileSystemUtils method getMCRFile.

static MCRFile getMCRFile(MCRDirectory baseDir, MCRPath relativePath, boolean create, boolean createNew) throws IOException {
    MCRPath ifsPath = relativePath;
    if (relativePath.isAbsolute()) {
        if (baseDir.getOwnerID().equals(relativePath.getOwner())) {
            ifsPath = baseDir.toPath().relativize(relativePath);
        } else
            throw new IOException(relativePath + " is absolute does not fit to " + baseDir.toPath());
    }
    Deque<MCRFilesystemNode> created = new LinkedList<>();
    MCRFile file;
    try {
        file = (MCRFile) baseDir.getChildByPath(ifsPath.toString());
        if (file != null && createNew) {
            throw new FileAlreadyExistsException(baseDir.toPath().resolve(ifsPath).toString());
        }
        if (file == null & (create || createNew)) {
            Path normalized = ifsPath.normalize();
            MCRDirectory parent = baseDir;
            int nameCount = normalized.getNameCount();
            int directoryCount = nameCount - 1;
            int i = 0;
            while (i < directoryCount) {
                String curName = normalized.getName(i).toString();
                MCRDirectory curDir = (MCRDirectory) parent.getChild(curName);
                if (curDir == null) {
                    curDir = new MCRDirectory(curName, parent);
                    created.addFirst(curDir);
                }
                i++;
                parent = curDir;
            }
            String fileName = normalized.getFileName().toString();
            file = new MCRFile(fileName, parent);
            // false -> no event handler
            file.setContentFrom(new ByteArrayInputStream(new byte[0]), false);
            created.addFirst(file);
        }
    } catch (Exception e) {
        if (create || createNew) {
            LOGGER.error("Exception while getting MCRFile {}. Removing created filesystem nodes.", ifsPath);
            while (created.peekFirst() != null) {
                MCRFilesystemNode node = created.pollFirst();
                try {
                    node.delete();
                } catch (Exception de) {
                    LOGGER.fatal("Error while deleting file system node: {}", node.getName(), de);
                }
            }
        }
        throw e;
    }
    return file;
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) IOException(java.io.IOException) LinkedList(java.util.LinkedList) NoSuchFileException(java.nio.file.NoSuchFileException) ProviderMismatchException(java.nio.file.ProviderMismatchException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InvalidPathException(java.nio.file.InvalidPathException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) ByteArrayInputStream(java.io.ByteArrayInputStream) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 10 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile in project mycore by MyCoRe-Org.

the class MCRAVExtRealHelix method getPlayerStarter.

@Override
public MCRContent getPlayerStarter(String startPos, String stopPos) throws IOException {
    MCRURLContent content = new MCRURLContent(new URL(getURL(startPos, stopPos)));
    content.setMimeType(getPlayerStarterContentType());
    if (file instanceof MCRFile) {
        content.setName(file.getName() + startPos + "-" + stopPos);
    }
    return content;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRURLContent(org.mycore.common.content.MCRURLContent) URL(java.net.URL)

Aggregations

MCRFile (org.mycore.datamodel.ifs.MCRFile)16 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)11 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)9 NoSuchFileException (java.nio.file.NoSuchFileException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 IOException (java.io.IOException)3 InvalidPathException (java.nio.file.InvalidPathException)3 Path (java.nio.file.Path)3 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 File (java.io.File)2 CopyOption (java.nio.file.CopyOption)2 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)2 LinkOption (java.nio.file.LinkOption)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 FileTime (java.nio.file.attribute.FileTime)2 HashMap (java.util.HashMap)2 CacheBuilder (com.google.common.cache.CacheBuilder)1