Search in sources :

Example 1 with AssetStore

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

the class SchedulerServiceImplTest method mkAssetStore.

AssetStore mkAssetStore() {
    return new AssetStore() {

        @Override
        public Option<Long> getUsedSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getUsableSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getTotalSpace() {
            return Option.none();
        }

        @Override
        public void put(StoragePath path, Source source) throws AssetStoreException {
            File destFile = new File(baseDir, UrlSupport.concat(path.getMediaPackageId(), path.getMediaPackageElementId(), path.getVersion().toString()));
            try {
                FileUtils.copyFile(workspace.get(source.getUri()), destFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (NotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Opt<InputStream> get(StoragePath path) throws AssetStoreException {
            File file = new File(baseDir, UrlSupport.concat(path.getMediaPackageId(), path.getMediaPackageElementId(), path.getVersion().toString()));
            InputStream inputStream;
            try {
                inputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
                return Opt.some(inputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public boolean delete(DeletionSelector sel) throws AssetStoreException {
            return false;
        }

        @Override
        public boolean copy(StoragePath from, StoragePath to) throws AssetStoreException {
            File file = new File(baseDir, UrlSupport.concat(from.getMediaPackageId(), from.getMediaPackageElementId(), from.getVersion().toString()));
            File destFile = new File(baseDir, UrlSupport.concat(to.getMediaPackageId(), to.getMediaPackageElementId(), to.getVersion().toString()));
            try {
                FileUtils.copyFile(file, destFile);
                return true;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public boolean contains(StoragePath path) throws AssetStoreException {
            return false;
        }
    };
}
Also used : DeletionSelector(org.opencastproject.assetmanager.impl.storage.DeletionSelector) StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) AssetStore(org.opencastproject.assetmanager.impl.storage.AssetStore) IOException(java.io.IOException) File(java.io.File) Source(org.opencastproject.assetmanager.impl.storage.Source)

Example 2 with AssetStore

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

the class AssetManagerTestBase method mkAbstractAssetManager.

/**
 * Create a new test asset manager.
 */
protected AbstractAssetManager mkAbstractAssetManager() throws Exception {
    penv = PersistenceEnvs.mkTestEnvFromSystemProperties(PERSISTENCE_UNIT);
    // empty database
    penv.tx(new Fn<EntityManager, Object>() {

        @Override
        public Object apply(EntityManager entityManager) {
            Queries.sql.update(entityManager, "delete from oc_assets_asset");
            Queries.sql.update(entityManager, "delete from oc_assets_properties");
            Queries.sql.update(entityManager, "delete from oc_assets_snapshot");
            Queries.sql.update(entityManager, "delete from oc_assets_version_claim");
            return null;
        }
    });
    final Database db = new Database(penv);
    // 
    final Workspace workspace = EasyMock.createNiceMock(Workspace.class);
    EasyMock.expect(workspace.get(EasyMock.anyObject(URI.class))).andReturn(IoSupport.classPathResourceAsFile("/dublincore-a.xml").get()).anyTimes();
    EasyMock.expect(workspace.get(EasyMock.anyObject(URI.class), EasyMock.anyBoolean())).andAnswer(() -> {
        File tmp = tempFolder.newFile();
        FileUtils.copyFile(new File(getClass().getResource("/dublincore-a.xml").toURI()), tmp);
        return tmp;
    }).anyTimes();
    EasyMock.replay(workspace);
    // 
    final AssetStore assetStore = mkAssetStore();
    // 
    return new AbstractAssetManager() {

        @Override
        public Database getDb() {
            return db;
        }

        @Override
        public AssetStore getAssetStore() {
            return assetStore;
        }

        @Override
        public HttpAssetProvider getHttpAssetProvider() {
            // identity provider
            return new HttpAssetProvider() {

                @Override
                public Snapshot prepareForDelivery(Snapshot snapshot) {
                    return snapshot;
                }
            };
        }

        @Override
        protected Workspace getWorkspace() {
            return workspace;
        }

        @Override
        protected String getCurrentOrgId() {
            return AssetManagerTestBase.this.getCurrentOrgId();
        }
    };
}
Also used : Snapshot(org.opencastproject.assetmanager.api.Snapshot) EntityManager(javax.persistence.EntityManager) Database(org.opencastproject.assetmanager.impl.persistence.Database) AssetStore(org.opencastproject.assetmanager.impl.storage.AssetStore) File(java.io.File) Workspace(org.opencastproject.workspace.api.Workspace)

Example 3 with AssetStore

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

the class AssetManagerTestBase method mkAssetStore.

/**
 * Create a test asset store.
 */
protected AssetStore mkAssetStore() {
    return new AssetStore() {

        private Set<StoragePath> store = new HashSet<>();

        private void logSize() {
            logger.debug(format("Store contains %d asset/s", store.size()));
        }

        @Override
        public void put(StoragePath path, Source source) throws AssetStoreException {
            store.add(path);
            logSize();
        }

        @Override
        public boolean copy(StoragePath from, StoragePath to) throws AssetStoreException {
            if (store.contains(from)) {
                store.add(to);
                logSize();
                return true;
            } else {
                return false;
            }
        }

        @Override
        public Opt<InputStream> get(StoragePath path) throws AssetStoreException {
            return IoSupport.openClassPathResource("/dublincore-a.xml").toOpt();
        }

        @Override
        public boolean contains(StoragePath path) throws AssetStoreException {
            return store.contains(path);
        }

        @Override
        public boolean delete(DeletionSelector sel) throws AssetStoreException {
            logger.info("Delete from asset store " + sel);
            final Set<StoragePath> newStore = new HashSet<>();
            boolean deleted = false;
            for (StoragePath s : store) {
                if (!(sel.getOrganizationId().equals(s.getOrganizationId()) && sel.getMediaPackageId().equals(s.getMediaPackageId()) && sel.getVersion().map(eq(s.getVersion())).getOr(true))) {
                    newStore.add(s);
                } else {
                    deleted = true;
                }
            }
            store = newStore;
            logSize();
            return deleted;
        }

        @Override
        public Option<Long> getTotalSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getUsableSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getUsedSpace() {
            return Option.some((long) store.size());
        }
    };
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DeletionSelector(org.opencastproject.assetmanager.impl.storage.DeletionSelector) StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) InputStream(java.io.InputStream) AssetStore(org.opencastproject.assetmanager.impl.storage.AssetStore) Source(org.opencastproject.assetmanager.impl.storage.Source) HashSet(java.util.HashSet)

Example 4 with AssetStore

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

the class TestTasksEndpoint method mkAssetStore.

AssetStore mkAssetStore(final Workspace workspace) {
    return new AssetStore() {

        @Override
        public Option<Long> getUsedSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getUsableSpace() {
            return Option.none();
        }

        @Override
        public Option<Long> getTotalSpace() {
            return Option.none();
        }

        @Override
        public void put(StoragePath path, Source source) throws AssetStoreException {
            File destFile = new File(baseDir, UrlSupport.concat(path.getMediaPackageId(), path.getMediaPackageElementId(), path.getVersion().toString()));
            try {
                FileUtils.copyFile(workspace.get(source.getUri()), destFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (NotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Opt<InputStream> get(StoragePath path) throws AssetStoreException {
            File file = new File(baseDir, UrlSupport.concat(path.getMediaPackageId(), path.getMediaPackageElementId(), path.getVersion().toString()));
            InputStream inputStream;
            try {
                inputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
                return Opt.some(inputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public boolean delete(DeletionSelector sel) throws AssetStoreException {
            return false;
        }

        @Override
        public boolean copy(StoragePath from, StoragePath to) throws AssetStoreException {
            File file = new File(baseDir, UrlSupport.concat(from.getMediaPackageId(), from.getMediaPackageElementId(), from.getVersion().toString()));
            File destFile = new File(baseDir, UrlSupport.concat(to.getMediaPackageId(), to.getMediaPackageElementId(), to.getVersion().toString()));
            try {
                FileUtils.copyFile(file, destFile);
                return true;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public boolean contains(StoragePath path) throws AssetStoreException {
            return false;
        }
    };
}
Also used : DeletionSelector(org.opencastproject.assetmanager.impl.storage.DeletionSelector) StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) AssetStore(org.opencastproject.assetmanager.impl.storage.AssetStore) IOException(java.io.IOException) File(java.io.File) Source(org.opencastproject.assetmanager.impl.storage.Source)

Aggregations

AssetStore (org.opencastproject.assetmanager.impl.storage.AssetStore)4 File (java.io.File)3 InputStream (java.io.InputStream)3 DeletionSelector (org.opencastproject.assetmanager.impl.storage.DeletionSelector)3 Source (org.opencastproject.assetmanager.impl.storage.Source)3 StoragePath (org.opencastproject.assetmanager.impl.storage.StoragePath)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 NotFoundException (org.opencastproject.util.NotFoundException)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 EntityManager (javax.persistence.EntityManager)1 Snapshot (org.opencastproject.assetmanager.api.Snapshot)1 Database (org.opencastproject.assetmanager.impl.persistence.Database)1 Workspace (org.opencastproject.workspace.api.Workspace)1