Search in sources :

Example 16 with MCRDirectory

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

the class MCRFileSystemProvider method newDirectoryStream.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#newDirectoryStream(java.nio.file.Path, java.nio.file.DirectoryStream.Filter)
     */
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
    MCRFilesystemNode node = resolvePath(mcrPath);
    if (node instanceof MCRDirectory) {
        return new MCRDirectoryStream((MCRDirectory) node, mcrPath);
    }
    throw new NotDirectoryException(dir.toString());
}
Also used : NotDirectoryException(java.nio.file.NotDirectoryException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 17 with MCRDirectory

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

the class MCRFileSystemProvider method createDirectory.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#createDirectory(java.nio.file.Path, java.nio.file.attribute.FileAttribute[])
     */
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
    if (attrs.length > 0) {
        throw new UnsupportedOperationException("Setting 'attrs' atomically is unsupported.");
    }
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
    MCRDirectory rootDirectory;
    if (mcrPath.isAbsolute() && mcrPath.getNameCount() == 0) {
        rootDirectory = MCRDirectory.getDirectory(mcrPath.getOwner());
        if (rootDirectory != null) {
            throw new FileAlreadyExistsException(mcrPath.toString());
        }
        rootDirectory = new MCRDirectory(mcrPath.getOwner());
        return;
    }
    rootDirectory = getRootDirectory(mcrPath);
    MCRPath parentPath = mcrPath.getParent();
    MCRPath absolutePath = getAbsolutePathFromRootComponent(parentPath);
    MCRFilesystemNode childByPath = rootDirectory.getChildByPath(absolutePath.toString());
    if (childByPath == null) {
        throw new NoSuchFileException(parentPath.toString(), dir.getFileName().toString(), "parent directory does not exist");
    }
    if (childByPath instanceof MCRFile) {
        throw new NotDirectoryException(parentPath.toString());
    }
    MCRDirectory parentDir = (MCRDirectory) childByPath;
    String dirName = mcrPath.getFileName().toString();
    if (parentDir.getChild(dirName) != null) {
        throw new FileAlreadyExistsException(mcrPath.toString());
    }
    new MCRDirectory(dirName, parentDir);
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) 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 18 with MCRDirectory

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

the class MCRFileSystemProvider method doResolvePath.

private static MCRFilesystemNode doResolvePath(MCRPath path) throws IOException {
    if (path.getNameCount() == 0) {
        // root components
        MCRDirectory rootDirectory = MCRDirectory.getRootDirectory(path.getOwner());
        if (rootDirectory == null) {
            throw new NoSuchFileException(path.toString());
        }
        // prepare cache
        rootDirectory.getChildren();
        return rootDirectory;
    }
    MCRDirectory parent = getParentDirectory(path);
    MCRFilesystemNode child;
    try {
        child = parent.getChild(path.getFileName().toString());
    } catch (RuntimeException e) {
        throw new IOException(e);
    }
    if (child == null) {
        throw new NoSuchFileException(parent.toPath().toString(), path.toString(), null);
    }
    return child;
}
Also used : MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 19 with MCRDirectory

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

the class MCRFileTypeDetector method probeContentType.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileTypeDetector#probeContentType(java.nio.file.Path)
     */
@Override
public String probeContentType(Path path) throws IOException {
    LOGGER.debug(() -> "Probing content type of: " + path);
    if (!(path.getFileSystem() instanceof MCRIFSFileSystem)) {
        return null;
    }
    MCRFilesystemNode resolvePath = MCRFileSystemProvider.resolvePath(MCRPath.toMCRPath(path));
    if (resolvePath == null) {
        throw new NoSuchFileException(path.toString());
    }
    if (resolvePath instanceof MCRDirectory) {
        throw new NoSuchFileException(path.toString());
    }
    MCRFile file = (MCRFile) resolvePath;
    String mimeType = file.getContentType().getMimeType();
    LOGGER.debug(() -> "IFS mime-type: " + mimeType);
    if (defaultMimeType.equals(mimeType)) {
        Path dummy = Paths.get(path.getFileName().toString());
        String dummyType = Files.probeContentType(dummy);
        if (dummyType != null) {
            LOGGER.debug(() -> "System mime-type #1: " + dummyType);
            return dummyType;
        }
        String systemMimeType = Files.probeContentType(file.getLocalFile().toPath());
        if (systemMimeType != null) {
            LOGGER.debug(() -> "System mime-type #2: " + systemMimeType);
            return systemMimeType;
        }
    }
    return mimeType;
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 20 with MCRDirectory

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

the class MCRHIBFileMetadataStore method storeNode.

@Override
public void storeNode(MCRFilesystemNode node) throws MCRPersistenceException {
    String ID = node.getID();
    String PID = node.getParentID();
    String OWNER = node.getOwnerID();
    String NAME = node.getName();
    String LABEL = node.getLabel();
    long SIZE = node.getSize();
    GregorianCalendar DATE = node.getLastModified();
    String TYPE = null;
    String STOREID = null;
    String STORAGEID = null;
    String FCTID = null;
    String MD5 = null;
    int NUMCHDD = 0;
    int NUMCHDF = 0;
    int NUMCHTD = 0;
    int NUMCHTF = 0;
    if (node instanceof MCRFile) {
        MCRFile file = (MCRFile) node;
        TYPE = "F";
        STOREID = file.getStoreID();
        STORAGEID = file.getStorageID();
        FCTID = file.getContentTypeID();
        MD5 = file.getMD5();
    } else if (node instanceof MCRDirectory) {
        MCRDirectory dir = (MCRDirectory) node;
        TYPE = "D";
        NUMCHDD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.HERE);
        NUMCHDF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.HERE);
        NUMCHTD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.TOTAL);
        NUMCHTF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.TOTAL);
    } else {
        throw new MCRPersistenceException("MCRFilesystemNode must be either MCRFile or MCRDirectory");
    }
    Session session = getSession();
    MCRFSNODES fs = session.get(MCRFSNODES.class, ID);
    if (fs == null) {
        fs = new MCRFSNODES();
        fs.setId(ID);
    }
    fs.setPid(PID);
    fs.setType(TYPE);
    fs.setOwner(OWNER);
    fs.setName(NAME);
    fs.setLabel(LABEL);
    fs.setSize(SIZE);
    fs.setDate(new Timestamp(DATE.getTime().getTime()));
    fs.setStoreid(STOREID);
    fs.setStorageid(STORAGEID);
    fs.setFctid(FCTID);
    fs.setMd5(MD5);
    fs.setNumchdd(NUMCHDD);
    fs.setNumchdf(NUMCHDF);
    fs.setNumchtd(NUMCHTD);
    fs.setNumchtf(NUMCHTF);
    if (!session.contains(fs)) {
        session.saveOrUpdate(fs);
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) GregorianCalendar(java.util.GregorianCalendar) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Timestamp(java.sql.Timestamp) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) Session(org.hibernate.Session)

Aggregations

MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)21 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)13 MCRPath (org.mycore.datamodel.niofs.MCRPath)13 MCRFile (org.mycore.datamodel.ifs.MCRFile)10 NoSuchFileException (java.nio.file.NoSuchFileException)9 IOException (java.io.IOException)7 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NotDirectoryException (java.nio.file.NotDirectoryException)4 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 Path (java.nio.file.Path)3 CopyOption (java.nio.file.CopyOption)2 FileSystemException (java.nio.file.FileSystemException)2 InvalidPathException (java.nio.file.InvalidPathException)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 GregorianCalendar (java.util.GregorianCalendar)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2