use of org.craftercms.studio.api.v1.asset.processing.AssetProcessorPipeline in project studio by craftercms.
the class AssetProcessingServiceImpl method processAsset.
@Override
public Map<String, Object> processAsset(String site, String folder, String assetName, InputStream in, String isImage, String allowedWidth, String allowedHeight, String allowLessSize, String draft, String unlock, String systemAsset) {
String repoPath = UrlUtils.concat(folder, assetName);
InputStream configIn;
try {
try {
configIn = contentService.getContent(site, configPath);
} catch (ContentNotFoundException e) {
// Ignore if file couldn't be found
configIn = null;
}
if (configIn != null) {
List<ProcessorPipelineConfiguration> pipelinesConfig = configReader.readConfig(configIn);
if (CollectionUtils.isNotEmpty(pipelinesConfig)) {
Asset input = createAssetFromInputStream(repoPath, in);
try {
Set<Asset> finalOutputs = new LinkedHashSet<>();
for (ProcessorPipelineConfiguration pipelineConfig : pipelinesConfig) {
AssetProcessorPipeline pipeline = pipelineResolver.getPipeline(pipelineConfig);
List<Asset> outputs = pipeline.processAsset(pipelineConfig, input);
if (CollectionUtils.isNotEmpty(outputs)) {
finalOutputs.addAll(outputs);
}
}
if (CollectionUtils.isNotEmpty(finalOutputs)) {
List<Map<String, Object>> results = writeOutputs(site, finalOutputs, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
// one main result specified by config -- Alfonso
if (CollectionUtils.isNotEmpty(results)) {
return results.get(0);
} else {
return Collections.emptyMap();
}
} else {
// No outputs mean that the input wasn't matched by any pipeline and processing was skipped
logger.debug("No pipeline matched for {0}. Skipping asset processing...", repoPath);
// We already read input so open the temp file
try (InputStream assetIn = Files.newInputStream(input.getFilePath())) {
return contentService.writeContentAsset(site, folder, assetName, assetIn, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
}
}
} finally {
try {
Files.delete(input.getFilePath());
} catch (IOException e) {
// delete silently
}
}
} else {
// Ignore if no pipelines config
logger.debug("No asset processing pipelines config found at {0}. Skipping asset processing...", repoPath);
return contentService.writeContentAsset(site, folder, assetName, in, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
}
} else {
logger.debug("No asset processing config found at {0}. Skipping asset processing...", repoPath);
return contentService.writeContentAsset(site, folder, assetName, in, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
}
} catch (Exception e) {
logger.error("Error processing asset", e);
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("message", e.getMessage());
result.put("error", e);
return result;
}
}
Aggregations