Search in sources :

Example 1 with AssetProcessorPipeline

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;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream) IOException(java.io.IOException) AssetProcessingException(org.craftercms.studio.api.v1.exception.AssetProcessingException) IOException(java.io.IOException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) ProcessorPipelineConfiguration(org.craftercms.studio.api.v1.asset.processing.ProcessorPipelineConfiguration) AssetProcessorPipeline(org.craftercms.studio.api.v1.asset.processing.AssetProcessorPipeline) Asset(org.craftercms.studio.api.v1.asset.Asset) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Asset (org.craftercms.studio.api.v1.asset.Asset)1 AssetProcessorPipeline (org.craftercms.studio.api.v1.asset.processing.AssetProcessorPipeline)1 ProcessorPipelineConfiguration (org.craftercms.studio.api.v1.asset.processing.ProcessorPipelineConfiguration)1 AssetProcessingException (org.craftercms.studio.api.v1.exception.AssetProcessingException)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1