Search in sources :

Example 1 with AssetManager

use of com.day.cq.dam.api.AssetManager in project acs-aem-commons by Adobe-Consulting-Services.

the class S3AssetIngestorTest method setup.

@Before
public void setup() throws PersistenceException {
    context.registerAdapter(ResourceResolver.class, AssetManager.class, new Function<ResourceResolver, AssetManager>() {

        @Nullable
        @Override
        public AssetManager apply(@Nullable ResourceResolver input) {
            return assetManager;
        }
    });
    context.create().resource("/content/dam", JcrConstants.JCR_PRIMARYTYPE, "sling:Folder");
    context.resourceResolver().commit();
    ingestor = new S3AssetIngestor(context.getService(MimeTypeService.class));
    ingestor.jcrBasePath = "/content/dam";
    ingestor.ignoreFileList = Collections.emptyList();
    ingestor.ignoreExtensionList = Collections.emptyList();
    ingestor.ignoreFolderList = Arrays.asList(".ds_store");
    ingestor.existingAssetAction = AssetIngestor.AssetAction.skip;
    int port = FreePortFinder.findFreeLocalPort();
    s3Mock = new S3Mock.Builder().withPort(port).withInMemoryBackend().build();
    s3Mock.start();
    S3ClientOptions options = S3ClientOptions.builder().setPathStyleAccess(true).build();
    s3Client = new AmazonS3Client(new AnonymousAWSCredentials());
    s3Client.setS3ClientOptions(options);
    s3Client.setEndpoint("http://localhost:" + port);
    ingestor.s3Client = s3Client;
    ingestor.bucket = TEST_BUCKET;
    s3Client.createBucket(TEST_BUCKET);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            CheckedConsumer<ResourceResolver> method = (CheckedConsumer<ResourceResolver>) invocation.getArguments()[0];
            method.accept(context.resourceResolver());
            return null;
        }
    }).when(actionManager).deferredWithResolver(any(CheckedConsumer.class));
}
Also used : AssetManager(com.day.cq.dam.api.AssetManager) CheckedConsumer(com.adobe.acs.commons.functions.CheckedConsumer) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) Answer(org.mockito.stubbing.Answer) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3ClientOptions(com.amazonaws.services.s3.S3ClientOptions) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Nullable(javax.annotation.Nullable) Before(org.junit.Before)

Example 2 with AssetManager

use of com.day.cq.dam.api.AssetManager in project acs-aem-commons by Adobe-Consulting-Services.

the class AssetIngestor method createAsset.

@SuppressWarnings("squid:S00112")
private void createAsset(Source source, String assetPath, ResourceResolver r, boolean versioning) throws Exception {
    r.adaptTo(Session.class).getWorkspace().getObservationManager().setUserData(CHANGED_BY_WORKFLOW);
    AssetManager assetManager = r.adaptTo(AssetManager.class);
    String type = mimetypeService.getMimeType(source.getName());
    if (versioning) {
        // is asset is null, no version gets created
        Asset asset = r.getResource(assetPath).adaptTo(Asset.class);
        // once you are past this first version, default behavior is to start numbering 1.0, 1.1 and so on
        assetManager.createRevision(asset, "initial version of asset", asset.getName());
        r.commit();
        r.refresh();
    // once version is committed we are safe to create, which only replaces the original version
    }
    assetManager.createAsset(assetPath, source.getStream(), type, false);
    r.commit();
    r.refresh();
    totalImportedData.accumulateAndGet(source.getLength(), (p, x) -> p + x);
    assetCount.incrementAndGet();
}
Also used : AssetManager(com.day.cq.dam.api.AssetManager) Asset(com.day.cq.dam.api.Asset) Session(javax.jcr.Session)

Example 3 with AssetManager

use of com.day.cq.dam.api.AssetManager in project acs-aem-commons by Adobe-Consulting-Services.

the class FileAssetIngestorTest method setup.

@Before
public void setup() throws PersistenceException {
    context.registerAdapter(ResourceResolver.class, AssetManager.class, new Function<ResourceResolver, AssetManager>() {

        @Nullable
        @Override
        public AssetManager apply(@Nullable ResourceResolver input) {
            return assetManager;
        }
    });
    context.create().resource("/content/dam", JcrConstants.JCR_PRIMARYTYPE, "sling:Folder");
    context.resourceResolver().commit();
    ingestor = new FileAssetIngestor(context.getService(MimeTypeService.class));
    ingestor.jcrBasePath = "/content/dam";
    ingestor.ignoreFileList = Collections.emptyList();
    ingestor.ignoreExtensionList = Collections.emptyList();
    ingestor.ignoreFolderList = Arrays.asList(".ds_store");
    ingestor.existingAssetAction = AssetIngestor.AssetAction.skip;
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            CheckedConsumer<ResourceResolver> method = (CheckedConsumer<ResourceResolver>) invocation.getArguments()[0];
            method.accept(context.resourceResolver());
            return null;
        }
    }).when(actionManager).deferredWithResolver(any(CheckedConsumer.class));
    tempDirectory = Files.createTempDir();
    ingestor.fileBasePath = tempDirectory.getAbsolutePath();
}
Also used : Answer(org.mockito.stubbing.Answer) AssetManager(com.day.cq.dam.api.AssetManager) CheckedConsumer(com.adobe.acs.commons.functions.CheckedConsumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Nullable(javax.annotation.Nullable) Before(org.junit.Before)

Aggregations

AssetManager (com.day.cq.dam.api.AssetManager)3 CheckedConsumer (com.adobe.acs.commons.functions.CheckedConsumer)2 Nullable (javax.annotation.Nullable)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Before (org.junit.Before)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 S3ClientOptions (com.amazonaws.services.s3.S3ClientOptions)1 Asset (com.day.cq.dam.api.Asset)1 Session (javax.jcr.Session)1