Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement to move uploaded videos to same folder as within AEM #61

Open
wants to merge 1 commit into
base: release/6.0.1-cloud
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.coresecure.brightcove.wrapper.listeners;

import com.day.cq.replication.ReplicationAction;
import com.google.gson.JsonObject;

import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
Expand Down Expand Up @@ -32,6 +34,7 @@
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.*;
import org.apache.sling.settings.SlingSettingsService;

Expand Down Expand Up @@ -82,7 +85,7 @@ protected void activate(EventListenerPageActivationListenerConfiguration config)
LOG.info("Brightcove Distribution Event Listener is enabled: " + enabled);
}

private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) {
private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map, String assetFolderId) {

LOG.trace("brc_lastsync was null or zero : asset should be initialized");

Expand All @@ -109,7 +112,11 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod
// update the metadata to show the last sync time
brc_lastsync_map.put(DamConstants.DC_TITLE, video.name);
brc_lastsync_map.put(Constants.BRC_LASTSYNC, JcrUtil.now2calendar());


// Move video to same folder as in AEM
if(StringUtils.isNotBlank(assetFolderId)) {
serviceUtil.moveVideoToFolder(assetFolderId, api_resp.getString(Constants.VIDEOID));
}
} else {

// log the error
Expand All @@ -125,7 +132,7 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod

}

private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) {
private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map, String assetFolderId) {

LOG.info("Entering activateModified()");

Expand All @@ -145,7 +152,11 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video

long current_time_millisec = new Date().getTime();
brc_lastsync_map.put(Constants.BRC_LASTSYNC, current_time_millisec);


// Move video to same folder as in AEM
if(StringUtils.isNotBlank(assetFolderId)) {
serviceUtil.moveVideoToFolder(assetFolderId, api_resp.getString(Constants.VIDEOID));
}
} else {

// log the error
Expand All @@ -161,7 +172,7 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video

}

private void activateAsset(ResourceResolver rr, Asset _asset, String accountId) {
private void activateAsset(ResourceResolver rr, Asset _asset, String accountId, String assetFolderId) {

// need to either activate a new asset or an updated existing
ServiceUtil serviceUtil = new ServiceUtil(accountId);
Expand Down Expand Up @@ -193,13 +204,13 @@ private void activateAsset(ResourceResolver rr, Asset _asset, String accountId)

// we need to activate a new asset here
LOG.info("Activating New Brightcove Asset: {}", _asset.getPath());
activateNew(_asset, serviceUtil, video, brc_lastsync_map);
activateNew(_asset, serviceUtil, video, brc_lastsync_map, assetFolderId);

} else if (jcr_lastmod > brc_lastsync_time) {

// we need to modify an existing asset here
LOG.info("Activating Modified Brightcove Asset: {}", _asset.getPath());
activateModified(_asset, serviceUtil, video, brc_lastsync_map);
activateModified(_asset, serviceUtil, video, brc_lastsync_map, assetFolderId);

}

Expand Down Expand Up @@ -305,19 +316,35 @@ public void handleEvent(Event event) {

// get the account ID
String brightcoveAccountId = paths.get(key);
/**
*
* Get asset path substring after the account ID
* e.g. For /content/dam/brightcove_assets/accountID/folderID/video.mp4
* brightcoveAccountFolderPath => /content/dam/brightcove_assets/accountID
* assetFolders => folderID/video.mp4
* assetFolderId => folderID
*/
String brightcoveAccountFolderPath = new StringBuilder(key)
.append(Constants.DELIMITER_SLASH).append(brightcoveAccountId).toString();
LOG.info("Found Brightcove Account ID #{} for {}", brightcoveAccountId, asset);

String assetFolders = asset.substring(brightcoveAccountFolderPath.length() + 1);
LOG.info("Asset Folder {}", assetFolders);
String[] assetFolderArray = assetFolders.split("/");
String assetFolderId = StringUtils.EMPTY;
if (assetFolderArray.length > 1) {
assetFolderId = assetFolderArray[0];
}
LOG.info("Asset Folder ID {}", assetFolderId);
// get a proper Asset object from the path
Resource assetResource = rr.getResource(asset);
Asset _asset = assetResource.adaptTo(Asset.class);

// check the activation type
if ( "ACTIVATE".equals(event.getProperty("type")) ) {
if ("ACTIVATE".equals(event.getProperty("type"))) {

// upload or modify the asset
activateAsset(rr, _asset, brightcoveAccountId);

} else if ( "DEACTIVATE".equals(event.getProperty("type")) ) {
activateAsset(rr, _asset, brightcoveAccountId, assetFolderId);
} else if ("DEACTIVATE".equals(event.getProperty("type"))) {

// delete the asset
deactivateAsset(rr, _asset, brightcoveAccountId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public Constants(){/* default implementation ignored */}
public static final String EMPTY_URLPARAMS = "";
public static final String EMPTY_Q_PARAM = "";
public static final String EMPTY_SORT_PARAM = "";
public static final String DELIMITER_SLASH = "/";

public static final String AUTHENTICATION_HEADER = "Authorization";
public static final String ACCOUNTS_API_PATH = "/accounts/";
Expand Down