Search in sources :

Example 1 with AssetStoreException

use of org.opencastproject.assetmanager.impl.storage.AssetStoreException in project opencast by opencast.

the class AbstractFileSystemAssetStore method findStoragePathFile.

/**
 * Returns a file {@link Option} from a storage path if one is found or an empty {@link Option}
 *
 * @param storagePath
 *          the storage path
 * @return the file {@link Option}
 */
private Opt<File> findStoragePathFile(final StoragePath storagePath) {
    final FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return FilenameUtils.getBaseName(name).equals(storagePath.getMediaPackageElementId());
        }
    };
    final File containerDir = createFile(storagePath, Opt.none(String.class)).getParentFile();
    return nul(containerDir.listFiles(filter)).bind(new Fn<File[], Opt<File>>() {

        @Override
        public Opt<File> apply(File[] files) {
            switch(files.length) {
                case 0:
                    return none();
                case 1:
                    return some(files[0]);
                default:
                    throw new AssetStoreException("Storage path " + files[0].getParent() + "contains multiple files with the same element id!: " + storagePath.getMediaPackageElementId());
            }
        }
    });
}
Also used : FilenameFilter(java.io.FilenameFilter) Opt(com.entwinemedia.fn.data.Opt) File(java.io.File) AssetStoreException(org.opencastproject.assetmanager.impl.storage.AssetStoreException)

Example 2 with AssetStoreException

use of org.opencastproject.assetmanager.impl.storage.AssetStoreException in project opencast by opencast.

the class AbstractFileSystemAssetStore method copy.

@Override
public boolean copy(final StoragePath from, final StoragePath to) throws AssetStoreException {
    return findStoragePathFile(from).map(new Fn<File, Boolean>() {

        @Override
        public Boolean apply(File f) {
            final File t = createFile(to, f);
            mkParent(t);
            logger.debug("Copying {} to {}", f.getAbsolutePath(), t.getAbsolutePath());
            try {
                link(f, t, true);
            } catch (IOException e) {
                logger.error("Error copying archive file {} to {}", f, t);
                throw new AssetStoreException(e);
            }
            return true;
        }
    }).getOr(false);
}
Also used : Fn(com.entwinemedia.fn.Fn) IOException(java.io.IOException) File(java.io.File) AssetStoreException(org.opencastproject.assetmanager.impl.storage.AssetStoreException)

Example 3 with AssetStoreException

use of org.opencastproject.assetmanager.impl.storage.AssetStoreException in project opencast by opencast.

the class AbstractFileSystemAssetStore method delete.

@Override
public boolean delete(DeletionSelector sel) throws AssetStoreException {
    File dir = getDeletionSelectorDir(sel);
    try {
        FileUtils.deleteDirectory(dir);
        // also delete the media package directory if all versions have been deleted
        FileSupport.deleteHierarchyIfEmpty(file(path(getRootDirectory(), sel.getOrganizationId())), dir.getParentFile());
        return true;
    } catch (IOException e) {
        logger.error("Error deleting directory from archive {}", dir);
        throw new AssetStoreException(e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) AssetStoreException(org.opencastproject.assetmanager.impl.storage.AssetStoreException)

Example 4 with AssetStoreException

use of org.opencastproject.assetmanager.impl.storage.AssetStoreException in project opencast by opencast.

the class AbstractFileSystemAssetStore method put.

@Override
public void put(StoragePath storagePath, Source source) throws AssetStoreException {
    // Retrieving the file from the workspace has the advantage that in most cases the file already exists in the local
    // working file repository. In the very few cases where the file is not in the working file repository,
    // this strategy leads to a minor overhead because the file not only gets downloaded and stored in the file system
    // but also a hard link needs to be created (or if that's not possible, a copy of the file.
    final File origin = getUniqueFileFromWorkspace(source);
    final File destination = createFile(storagePath, source);
    try {
        mkParent(destination);
        link(origin, destination);
    } catch (IOException e) {
        logger.error("Error while linking/copying file {} to {}: {}", origin, destination, getMessage(e));
        throw new AssetStoreException(e);
    } finally {
        if (origin != null) {
            FileUtils.deleteQuietly(origin);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) AssetStoreException(org.opencastproject.assetmanager.impl.storage.AssetStoreException)

Aggregations

File (java.io.File)4 AssetStoreException (org.opencastproject.assetmanager.impl.storage.AssetStoreException)4 IOException (java.io.IOException)3 Fn (com.entwinemedia.fn.Fn)1 Opt (com.entwinemedia.fn.data.Opt)1 FilenameFilter (java.io.FilenameFilter)1