Search in sources :

Example 1 with RenditionDefinition2

use of org.alfresco.repo.rendition2.RenditionDefinition2 in project alfresco-remote-api by Alfresco.

the class RenditionsImpl method createRenditions.

@Override
public void createRenditions(NodeRef nodeRef, String versionLabelId, List<Rendition> renditions, Parameters parameters) throws NotFoundException, ConstraintViolatedException {
    if (renditions.isEmpty()) {
        return;
    }
    if (!renditionService2.isEnabled()) {
        throw new DisabledServiceException("Rendition generation has been disabled.");
    }
    final NodeRef sourceNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId(), versionLabelId, parameters);
    RenditionDefinitionRegistry2 renditionDefinitionRegistry2 = renditionService2.getRenditionDefinitionRegistry2();
    // So that POST /nodes/{nodeId}/renditions can specify rendition names as a comma separated list just like
    // POST /nodes/{nodId}/children can specify a comma separated list, the following code checks to see if the
    // supplied Rendition names are actually comma separated lists. The following example shows it is possible to
    // use both approaches.
    // [
    // { "id": "doclib" },
    // { "id": "avatar,avatar32" }
    // ]
    Set<String> renditionNames = new HashSet<>();
    for (Rendition rendition : renditions) {
        String name = getName(rendition);
        Set<String> requestedRenditions = NodesImpl.getRequestedRenditions(name);
        if (requestedRenditions == null) {
            renditionNames.add(null);
        } else {
            renditionNames.addAll(requestedRenditions);
        }
    }
    StringJoiner renditionNamesAlreadyExist = new StringJoiner(",");
    StringJoiner renditionNamesNotRegistered = new StringJoiner(",");
    List<String> renditionNamesToCreate = new ArrayList<>();
    for (String renditionName : renditionNames) {
        if (renditionName == null) {
            // 400
            throw new IllegalArgumentException(("Null rendition name supplied"));
        }
        RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(renditionName);
        if (renditionDefinition == null) {
            renditionNamesNotRegistered.add(renditionName);
        }
        final NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, renditionName, parameters);
        if (renditionNodeRef == null) {
            renditionNamesToCreate.add(renditionName);
        } else {
            renditionNamesAlreadyExist.add(renditionName);
        }
    }
    if (renditionNamesNotRegistered.length() != 0) {
        // 404
        throw new NotFoundException("Renditions not registered: " + renditionNamesNotRegistered);
    }
    if (renditionNamesToCreate.size() == 0) {
        // 409
        throw new ConstraintViolatedException("All renditions requested already exist: " + renditionNamesAlreadyExist);
    }
    for (String renditionName : renditionNamesToCreate) {
        try {
            renditionService2.render(sourceNodeRef, renditionName);
        } catch (UnsupportedOperationException e) {
            // 400
            throw new IllegalArgumentException((e.getMessage()));
        } catch (IllegalStateException e) {
            // 409
            throw new StaleEntityException(e.getMessage());
        }
    }
}
Also used : DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) Rendition(org.alfresco.rest.api.model.Rendition) ArrayList(java.util.ArrayList) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) RenditionDefinitionRegistry2(org.alfresco.repo.rendition2.RenditionDefinitionRegistry2) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 2 with RenditionDefinition2

use of org.alfresco.repo.rendition2.RenditionDefinition2 in project alfresco-remote-api by Alfresco.

the class RenditionsImpl method toApiRendition.

protected Rendition toApiRendition(String renditionName) {
    RenditionDefinitionRegistry2 renditionDefinitionRegistry2 = renditionService2.getRenditionDefinitionRegistry2();
    RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(renditionName);
    ContentInfo contentInfo = new ContentInfo(renditionDefinition.getTargetMimetype(), getMimeTypeDisplayName(renditionDefinition.getTargetMimetype()), null, null);
    Rendition apiRendition = new Rendition();
    apiRendition.setId(renditionName);
    apiRendition.setContent(contentInfo);
    apiRendition.setStatus(RenditionStatus.NOT_CREATED);
    return apiRendition;
}
Also used : ContentInfo(org.alfresco.rest.api.model.ContentInfo) Rendition(org.alfresco.rest.api.model.Rendition) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) RenditionDefinitionRegistry2(org.alfresco.repo.rendition2.RenditionDefinitionRegistry2)

Example 3 with RenditionDefinition2

use of org.alfresco.repo.rendition2.RenditionDefinition2 in project alfresco-remote-api by Alfresco.

the class RenditionsImpl method getRendition.

@Override
public Rendition getRendition(NodeRef nodeRef, String versionLabelId, String renditionId, Parameters parameters) {
    final NodeRef validatedNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId(), versionLabelId, parameters);
    NodeRef renditionNodeRef = getRenditionByName(validatedNodeRef, renditionId, parameters);
    boolean includeNotCreated = true;
    String status = getStatus(parameters);
    if (status != null) {
        includeNotCreated = !RenditionStatus.CREATED.equals(RenditionStatus.valueOf(status));
    }
    // if there is no rendition, then try to find the available/registered rendition (yet to be created).
    if (renditionNodeRef == null && includeNotCreated) {
        ContentData contentData = getContentData(validatedNodeRef, true);
        String sourceMimetype = contentData.getMimetype();
        long size = contentData.getSize();
        RenditionDefinitionRegistry2 renditionDefinitionRegistry2 = renditionService2.getRenditionDefinitionRegistry2();
        RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(renditionId);
        if (renditionDefinition == null) {
            throw new NotFoundException(renditionId + " is not registered.");
        } else {
            Set<String> renditionNames = renditionDefinitionRegistry2.getRenditionNamesFrom(sourceMimetype, size);
            boolean found = false;
            for (String renditionName : renditionNames) {
                // Check the registered renditionId is applicable for the node's mimeType
                if (renditionId.equals(renditionName)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new NotFoundException(renditionId + " is not applicable for the node's mimeType " + sourceMimetype);
            }
        }
        return toApiRendition(renditionId);
    }
    if (renditionNodeRef == null) {
        throw new NotFoundException("The rendition with id: " + renditionId + " was not found.");
    }
    return toApiRendition(renditionNodeRef);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) RenditionDefinitionRegistry2(org.alfresco.repo.rendition2.RenditionDefinitionRegistry2)

Example 4 with RenditionDefinition2

use of org.alfresco.repo.rendition2.RenditionDefinition2 in project alfresco-repository by Alfresco.

the class ScriptNode method createThumbnail.

/**
 * Creates a thumbnail for the content property of the node.
 *
 * The thumbnail name corresponds to pre-set thumbnail details stored in the
 * repository.
 *
 * If the thumbnail is created asynchronously then the result will be null and creation
 * of the thumbnail will occure at some point in the background.
 *
 * If foce param specified system.thumbnail.generate is ignoring. Could be used for preview creation
 *
 * @param  thumbnailName    the name of the thumbnail
 * @param  async            indicates whether the thumbnail is create asynchronously or not
 * @param  force            ignore system.thumbnail.generate=false
 * @return ScriptThumbnail  the newly create thumbnail node or null if async creation occures
 *
 * @deprecated The async flag in the method signature will not be applicable as all of
 * the future transformations will be asynchronous
 */
@Deprecated
public ScriptThumbnail createThumbnail(String thumbnailName, boolean async, boolean force) {
    final ThumbnailService thumbnailService = services.getThumbnailService();
    ScriptThumbnail result = null;
    // We need to create preview for node even if system.thumbnail.generate=false
    if (force || thumbnailService.getThumbnailsEnabled()) {
        // Use the thumbnail registy to get the details of the thumbail
        ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
        ThumbnailDefinition details = registry.getThumbnailDefinition(thumbnailName);
        if (details == null) {
            // Throw exception
            throw new ScriptException("The thumbnail name '" + thumbnailName + "' is not registered");
        }
        // If there's nothing currently registered to generate thumbnails for the
        // specified mimetype, then log a message and bail out
        String nodeMimeType = getMimetype();
        Serializable value = this.nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
        ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, value);
        if (!ContentData.hasContent(contentData)) {
            if (logger.isDebugEnabled())
                logger.debug("Unable to create thumbnail '" + details.getName() + "' as there is no content");
            return null;
        }
        if (!registry.isThumbnailDefinitionAvailable(contentData.getContentUrl(), nodeMimeType, getSize(), nodeRef, details)) {
            logger.info("Unable to create thumbnail '" + details.getName() + "' for " + nodeMimeType + " as no transformer is currently available.");
            return null;
        }
        // Have the thumbnail created
        if (async == false) {
            try {
                // Create the thumbnail
                NodeRef thumbnailNodeRef = thumbnailService.createThumbnail(this.nodeRef, ContentModel.PROP_CONTENT, details.getMimetype(), details.getTransformationOptions(), details.getName());
                // Create the thumbnail script object
                result = new ScriptThumbnail(thumbnailNodeRef, this.services, this.scope);
            } catch (AlfrescoRuntimeException e) {
                Throwable rootCause = e.getRootCause();
                if (rootCause instanceof UnimportantTransformException) {
                    logger.debug("Unable to create thumbnail '" + details.getName() + "' as " + rootCause.getMessage());
                    return null;
                }
                throw e;
            }
        } else {
            RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(thumbnailName);
            if (renditionDefinition != null) {
                renditionService2.render(nodeRef, thumbnailName);
            } else {
                Action action = ThumbnailHelper.createCreateThumbnailAction(details, services);
                // Queue async creation of thumbnail
                this.services.getActionService().executeAction(action, this.nodeRef, true, true);
            }
        }
    }
    return result;
}
Also used : Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) ScriptException(org.alfresco.scripts.ScriptException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) ContentData(org.alfresco.service.cmr.repository.ContentData) ScriptThumbnail(org.alfresco.repo.thumbnail.script.ScriptThumbnail) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 5 with RenditionDefinition2

use of org.alfresco.repo.rendition2.RenditionDefinition2 in project alfresco-repository by Alfresco.

the class ThumbnailRegistry method getMaxSourceSizeBytes.

/**
 * Returns the maximum source size of any content that may transformed between the supplied
 * sourceMimetype and thumbnailDefinition's targetMimetype using its transformation options.
 * @param sourceMimetype String
 * @param thumbnailDefinition ThumbnailDefinition
 * @return 0 if there are no transformers, -1 if there is no limit or if positive the size in bytes.
 */
public long getMaxSourceSizeBytes(String sourceMimetype, ThumbnailDefinition thumbnailDefinition) {
    // Use RenditionService2 if it knows about the definition, otherwise use localTransformServiceRegistry.
    // Needed as disabling local transforms should not disable thumbnails if they can be done remotely.
    long maxSize = 0;
    String targetMimetype = thumbnailDefinition.getMimetype();
    RenditionDefinition2 renditionDefinition = getEquivalentRenditionDefinition2(thumbnailDefinition);
    if (renditionDefinition != null) {
        Map<String, String> options = renditionDefinition.getTransformOptions();
        String renditionName = renditionDefinition.getRenditionName();
        maxSize = transformServiceRegistry.findMaxSize(sourceMimetype, targetMimetype, options, renditionName);
    } else {
        boolean orig = TransformerDebug.setDebugOutput(false);
        try {
            TransformationOptions transformationOptions = thumbnailDefinition.getTransformationOptions();
            String renditionName = thumbnailDefinition.getName();
            Map<String, String> options = converter.getOptions(transformationOptions, sourceMimetype, targetMimetype);
            maxSize = localTransformServiceRegistry.findMaxSize(sourceMimetype, targetMimetype, options, renditionName);
        } finally {
            TransformerDebug.setDebugOutput(orig);
        }
    }
    return maxSize;
}
Also used : RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Aggregations

RenditionDefinition2 (org.alfresco.repo.rendition2.RenditionDefinition2)9 RenditionDefinitionRegistry2 (org.alfresco.repo.rendition2.RenditionDefinitionRegistry2)4 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 StringJoiner (java.util.StringJoiner)2 Rendition (org.alfresco.rest.api.model.Rendition)2 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)2 ContentData (org.alfresco.service.cmr.repository.ContentData)2 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 MimetypeMap (org.alfresco.repo.content.MimetypeMap)1 MIMETYPE_PDF (org.alfresco.repo.content.MimetypeMap.MIMETYPE_PDF)1