Search in sources :

Example 11 with MCRFile

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

the class MCRTransferPackageFileContainer method processNode.

/**
 * @param node the node to process
 */
private void processNode(MCRFilesystemNode node) {
    MCRDirectory dir = (MCRDirectory) node;
    MCRFilesystemNode[] children = dir.getChildren();
    for (MCRFilesystemNode child : children) {
        if (child instanceof MCRDirectory) {
            processNode(child);
        }
        if (child instanceof MCRFile) {
            this.fileList.add((MCRFile) child);
        }
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode)

Example 12 with MCRFile

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

the class MCRFileSystemProvider method newByteChannel.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#newByteChannel(java.nio.file.Path, java.util.Set, java.nio.file.attribute.FileAttribute[])
     */
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
    if (attrs.length > 0) {
        throw new UnsupportedOperationException("Atomically setting of file attributes is not supported.");
    }
    MCRPath ifsPath = MCRFileSystemUtils.checkPathAbsolute(path);
    Set<? extends OpenOption> fileOpenOptions = options.stream().filter(option -> !(option == StandardOpenOption.CREATE || option == StandardOpenOption.CREATE_NEW)).collect(Collectors.toSet());
    boolean create = options.contains(StandardOpenOption.CREATE);
    boolean createNew = options.contains(StandardOpenOption.CREATE_NEW);
    if (create || createNew) {
        for (OpenOption option : fileOpenOptions) {
            // check before we create any file instance
            MCRFile.checkOpenOption(option);
        }
    }
    MCRFile mcrFile = MCRFileSystemUtils.getMCRFile(ifsPath, create, createNew);
    if (mcrFile == null) {
        throw new NoSuchFileException(path.toString());
    }
    return mcrFile.getFileChannel(fileOpenOptions);
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) LoadingCache(com.google.common.cache.LoadingCache) FileTime(java.nio.file.attribute.FileTime) MCRFileAttributes(org.mycore.datamodel.niofs.MCRFileAttributes) DirectoryStream(java.nio.file.DirectoryStream) MCRMD5AttributeView(org.mycore.datamodel.niofs.MCRMD5AttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) MCRPath(org.mycore.datamodel.niofs.MCRPath) FileAttributeView(java.nio.file.attribute.FileAttributeView) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) FileAttribute(java.nio.file.attribute.FileAttribute) FileSystem(java.nio.file.FileSystem) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CacheLoader(com.google.common.cache.CacheLoader) SeekableByteChannel(java.nio.channels.SeekableByteChannel) FileSystemAlreadyExistsException(java.nio.file.FileSystemAlreadyExistsException) Logger(org.apache.logging.log4j.Logger) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) AccessDeniedException(java.nio.file.AccessDeniedException) MCRContentStoreFactory(org.mycore.datamodel.ifs.MCRContentStoreFactory) CopyOption(java.nio.file.CopyOption) HashMap(java.util.HashMap) MCRConfiguration(org.mycore.common.config.MCRConfiguration) FileSystemProvider(java.nio.file.spi.FileSystemProvider) MCRAbstractFileSystem(org.mycore.datamodel.niofs.MCRAbstractFileSystem) AccessMode(java.nio.file.AccessMode) StandardCopyOption(java.nio.file.StandardCopyOption) HashSet(java.util.HashSet) LinkOption(java.nio.file.LinkOption) InvalidPathException(java.nio.file.InvalidPathException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Filter(java.nio.file.DirectoryStream.Filter) MCRFile(org.mycore.datamodel.ifs.MCRFile) FileStore(java.nio.file.FileStore) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) Files(java.nio.file.Files) OpenOption(java.nio.file.OpenOption) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) StandardOpenOption(java.nio.file.StandardOpenOption) OpenOption(java.nio.file.OpenOption) MCRFile(org.mycore.datamodel.ifs.MCRFile) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 13 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile 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 14 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile 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 15 with MCRFile

use of org.mycore.datamodel.ifs.MCRFile 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

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