Search in sources :

Example 1 with StoragePath

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

the class AbstractFileSystemAssetStoreTest method testCopy.

@Test
public void testCopy() throws Exception {
    StoragePath from = new StoragePath(ORG_ID, MP_ID, VERSION_2, MP_ELEM_ID);
    StoragePath to = new StoragePath(ORG_ID, MP_ID, VERSION_1, MP_ELEM_ID);
    assertTrue(repo.copy(from, to));
    File srcFile = new File(PathSupport.concat(new String[] { tmpRoot.toString(), ORG_ID, MP_ID, VERSION_2.toString(), MP_ELEM_ID + XML_EXTENSTION }));
    File copyFile = new File(PathSupport.concat(new String[] { tmpRoot.toString(), ORG_ID, MP_ID, VERSION_1.toString(), MP_ELEM_ID + XML_EXTENSTION }));
    assertTrue(srcFile.exists());
    assertTrue(copyFile.exists());
    FileInputStream srcIn = null;
    FileInputStream copyIn = null;
    try {
        srcIn = new FileInputStream(srcFile);
        copyIn = new FileInputStream(copyFile);
        byte[] bytesOriginal = IOUtils.toByteArray(srcIn);
        byte[] bytesCopy = IOUtils.toByteArray(copyIn);
        Assert.assertEquals(bytesCopy.length, bytesOriginal.length);
    } finally {
        IOUtils.closeQuietly(srcIn);
        IOUtils.closeQuietly(copyIn);
    }
}
Also used : StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with StoragePath

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

the class AbstractFileSystemAssetStoreTest method testPut.

@Test
public void testPut() throws Exception {
    StoragePath storagePath = new StoragePath(ORG_ID, MP_ID, VERSION_1, MP_ELEM_ID);
    repo.put(storagePath, Source.mk(getClass().getClassLoader().getResource(FILE_NAME).toURI()));
    File file = new File(PathSupport.concat(new String[] { tmpRoot.toString(), ORG_ID, MP_ID, VERSION_1.toString() }));
    assertTrue(file + " should be a directory", file.isDirectory());
    assertTrue(file.listFiles().length == 1);
    InputStream original = null;
    FileInputStream fileInput = null;
    try {
        fileInput = new FileInputStream(file.listFiles()[0]);
        original = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
        byte[] bytesFromRepo = IOUtils.toByteArray(fileInput);
        byte[] bytesFromClasspath = IOUtils.toByteArray(original);
        assertArrayEquals(bytesFromClasspath, bytesFromRepo);
    } finally {
        IOUtils.closeQuietly(original);
        IOUtils.closeQuietly(fileInput);
    }
}
Also used : StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with StoragePath

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

the class AbstractFileSystemAssetStoreTest method testCopyBad.

@Test
public void testCopyBad() throws Exception {
    StoragePath from = new StoragePath(ORG_ID, MP_ID, VERSION_1, MP_ELEM_ID);
    StoragePath to = new StoragePath(ORG_ID, MP_ID, VERSION_2, MP_ELEM_ID);
    assertFalse(repo.copy(from, to));
}
Also used : StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) Test(org.junit.Test)

Example 4 with StoragePath

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

the class AbstractAssetManager method storeAssets.

/**
 * Store all elements of <code>pmp</code> under the given version.
 */
private void storeAssets(final PartialMediaPackage pmp, final Version version) throws Exception {
    final String mpId = pmp.getMediaPackage().getIdentifier().toString();
    final String orgId = getCurrentOrgId();
    for (final MediaPackageElement e : pmp.getElements()) {
        logger.debug(format("Archiving %s %s %s", e.getFlavor(), e.getMimeType(), e.getURI()));
        final StoragePath storagePath = StoragePath.mk(orgId, mpId, version, e.getIdentifier());
        final Opt<StoragePath> existingAssetOpt = findAssetInVersions(e.getChecksum().toString());
        if (existingAssetOpt.isSome()) {
            final StoragePath existingAsset = existingAssetOpt.get();
            logger.debug("Content of asset {} with checksum {} has been archived before", existingAsset.getMediaPackageElementId(), e.getChecksum());
            if (!getAssetStore().copy(existingAsset, storagePath)) {
                throw new AssetManagerException(format("An asset with checksum %s has already been archived but trying to copy or link asset %s to it failed", e.getChecksum(), existingAsset));
            }
        } else {
            final Opt<Long> size = e.getSize() > 0 ? Opt.some(e.getSize()) : Opt.<Long>none();
            getAssetStore().put(storagePath, Source.mk(e.getURI(), size, Opt.nul(e.getMimeType())));
        }
    }
}
Also used : StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException)

Example 5 with StoragePath

use of org.opencastproject.assetmanager.impl.storage.StoragePath 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)

Aggregations

StoragePath (org.opencastproject.assetmanager.impl.storage.StoragePath)9 InputStream (java.io.InputStream)6 Test (org.junit.Test)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 AssetStore (org.opencastproject.assetmanager.impl.storage.AssetStore)3 DeletionSelector (org.opencastproject.assetmanager.impl.storage.DeletionSelector)3 Source (org.opencastproject.assetmanager.impl.storage.Source)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 AssetManagerException (org.opencastproject.assetmanager.api.AssetManagerException)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1