Search in sources :

Example 1 with MCRDevNull

use of org.mycore.common.content.streams.MCRDevNull in project mycore by MyCoRe-Org.

the class MCRUtils method getMD5Sum.

/**
 * Calculates md5 sum of InputStream. InputStream is consumed after calling this method and automatically closed.
 */
public static String getMD5Sum(InputStream inputStream) throws IOException {
    MCRMD5InputStream md5InputStream = null;
    try {
        md5InputStream = new MCRMD5InputStream(inputStream);
        IOUtils.copy(md5InputStream, new MCRDevNull());
        return md5InputStream.getMD5String();
    } finally {
        if (md5InputStream != null) {
            md5InputStream.close();
        }
    }
}
Also used : MCRDevNull(org.mycore.common.content.streams.MCRDevNull) MCRMD5InputStream(org.mycore.common.content.streams.MCRMD5InputStream)

Example 2 with MCRDevNull

use of org.mycore.common.content.streams.MCRDevNull in project mycore by MyCoRe-Org.

the class MCRFileImportExport method importFiles.

/**
 * Imports the contents of a local file or directory into the MyCoRe Internal Filesystem. If the local object is a file, a MCRFile with the same name will
 * be created or updated in the given MCRDirectory. If the local object is a directory, all contained subdirectories and files will be imported into the
 * given MCRDirectory. That means that after finishing this method, the complete directory structure will have been imported and mapped from the local
 * filesystem's structure. The method checks the contents of each local file to be imported. If the file's content has not changed for existing files, the
 * internal MCRFile will not be updated. If an internal directory is updated from a local directory, new files will be added, existing files will be updated
 * if necessary, but files that already exist in the given MCRDirectory but not in the local filesystem will be kept and will not be deleted.
 *
 * @param local
 *            the local file or directory
 * @param dir
 *            an existing MCRDirectory where to store the imported contents of the local filesystem.
 */
public static void importFiles(File local, MCRDirectory dir) throws IOException {
    Objects.requireNonNull(local, "local file is null");
    String path = local.getPath();
    String name = local.getName();
    if (!local.exists()) {
        throw new MCRUsageException("Not found: " + path);
    }
    if (!local.canRead()) {
        throw new MCRUsageException("Not readable: " + path);
    }
    // Import a local file
    if (local.isFile()) {
        MCRFilesystemNode existing = dir.getChild(name);
        MCRFile file = null;
        // If internal directory with same name exists
        if (existing instanceof MCRDirectory) {
            // delete it
            existing.delete();
            existing = null;
        }
        if (existing == null) {
            // Create new, empty MCRFile
            file = new MCRFile(name, dir, false);
        } else {
            // Update existing MCRFile
            file = (MCRFile) existing;
            // Determine MD5 checksum of local file
            try (FileInputStream fin = new FileInputStream(local);
                MCRMD5InputStream cis = new MCRMD5InputStream(fin)) {
                IOUtils.copy(cis, new MCRDevNull());
                String local_md5 = cis.getMD5String();
                // If file content of local file has not changed, do not load it again
                if (file.getMD5().equals(local_md5)) {
                    return;
                }
            }
        }
        // Store file content
        file.setContentFrom(local);
    } else {
        File[] files = local.listFiles();
        // For each local child node
        for (File file : files) {
            local = file;
            name = local.getName();
            MCRDirectory internalDir = dir;
            if (local.isDirectory()) {
                MCRFilesystemNode existing = dir.getChild(name);
                if (existing instanceof MCRFile) {
                    // If there is an
                    // existing MCRFile with
                    // same name
                    // delete that existing MCRFile
                    existing.delete();
                    existing = null;
                }
                if (existing == null) {
                    // Create new directory
                    internalDir = new MCRDirectory(name, dir, false);
                } else {
                    internalDir = (MCRDirectory) existing;
                }
            }
            // Recursively import
            importFiles(local, internalDir);
        }
    }
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRDevNull(org.mycore.common.content.streams.MCRDevNull) MCRMD5InputStream(org.mycore.common.content.streams.MCRMD5InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

MCRDevNull (org.mycore.common.content.streams.MCRDevNull)2 MCRMD5InputStream (org.mycore.common.content.streams.MCRMD5InputStream)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 MCRUsageException (org.mycore.common.MCRUsageException)1