@SuppressWarnings("PMD.CollapsibleIfStatements")
public final void execute(WorkItem workItem, WorkflowSession wfSession, MetaDataMap metaData)
throws WorkflowException {
final Asset asset = getAssetFromPayload(workItem, wfSession.getSession());
if (asset == null) {
String wfPayload = workItem.getWorkflowData().getPayload().toString();
String message = "execute: cannot process audio, asset [{" + wfPayload
+ "}] in payload doesn't exist for workflow [{" + workItem.getId() + "}].";
throw new WorkflowException(message);
}
final String assetMimeType = asset.getMimeType();
if (assetMimeType == null || !assetMimeType.startsWith("audio/")) {
if (!asset.getName().endsWith(".wav") || !asset.getName().endsWith(".mp3")
|| !asset.getName().endsWith(".ogg")) {
log.info("execute: asset [{}] is not of a audio mime type, asset ignored.", asset.getPath());
return;
}
}
File tmpDir = null;
File tmpWorkingDir = null;
FileOutputStream fos = null;
InputStream is = null;
FFMpegWrapper wrapper = null;
try {
// creating temp directory
tmpDir = createTempDir(null);
// creating temp working directory for ffmpeg
tmpWorkingDir = createTempDir(getWorkingDir());
// streaming file to temp directory
final File tmpFile = new File(tmpDir, asset.getName().replace(' ', '_'));
fos = new FileOutputStream(tmpFile);
is = asset.getOriginal().getStream();
IOUtils.copy(is, fos);
processAudio(metaData, asset, tmpFile, wfSession);
// get information about original audio file (size, video length,
// ...)
wrapper = new FFMpegWrapper(tmpFile, tmpWorkingDir);
wrapper.setExecutableLocator(locator);
final ResourceResolver resolver = getResourceResolver(wfSession.getSession());
final Resource assetResource = asset.adaptTo(Resource.class);
final Resource metadata = resolver.getResource(assetResource, JCR_CONTENT + "/" + METADATA_FOLDER);
if (null != metadata) {
final Node metadataNode = metadata.adaptTo(Node.class);
metadataNode.setProperty(DC_EXTENT, wrapper.getInputDuration());
metadataNode.getSession().save();
} else {
log.warn("execute: failed setting metdata for asset [{}] in workflow [{}], no metdata node found.",
asset.getPath(), workItem.getId());
}
} catch (IOException e) {
throw new WorkflowException(e);
} catch (RepositoryException e) {