Search in sources :

Example 1 with ContentAssetInfoTO

use of org.craftercms.studio.api.v1.to.ContentAssetInfoTO in project studio by craftercms.

the class PostActivityProcessor method process.

public void process(PipelineContent content, ResultTO result) throws SiteNotFoundException {
    if (result.getCommitId() != null) {
        String site = content.getProperty(DmConstants.KEY_SITE);
        boolean skipAuditLogInsert = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SKIP_AUDIT_LOG_INSERT));
        if (!skipAuditLogInsert) {
            String type = content.getProperty(DmConstants.KEY_ACTIVITY_TYPE);
            String user = content.getProperty(DmConstants.KEY_USER);
            String activityType = OPERATION_CREATE.equals(type) ? OPERATION_CREATE : OPERATION_UPDATE;
            String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
            String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
            boolean isSystemAsset = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SYSTEM_ASSET));
            if (isSystemAsset) {
                ContentAssetInfoTO assetInfoTO = (ContentAssetInfoTO) result.getItem();
                fileName = assetInfoTO.getFileName();
            }
            String uri = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(activityType);
            auditLog.setActorId(user);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site + ":" + uri);
            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
            auditLog.setPrimaryTargetValue(uri);
            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(site, uri));
            auditServiceInternal.insertAuditLog(auditLog);
        }
        contentRepository.markGitLogAudited(site, result.getCommitId());
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 2 with ContentAssetInfoTO

use of org.craftercms.studio.api.v1.to.ContentAssetInfoTO in project studio by craftercms.

the class CheckImageSizeProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String name = content.getProperty(DmConstants.KEY_FILE_NAME);
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String mimetype = mimeTypesMap.getContentType(name);
    boolean process = (StringUtils.isEmpty(mimetype)) ? false : mimetype.startsWith("image/") && !StringUtils.equalsIgnoreCase(mimetype, "image/svg+xml");
    if (process) {
        String allowLessSize = content.getProperty(DmConstants.KEY_ALLOW_LESS_SIZE);
        boolean lessSize = ContentFormatUtils.getBooleanValue(allowLessSize);
        String allowedWidth = content.getProperty(DmConstants.KEY_ALLOWED_WIDTH);
        String allowedHeight = content.getProperty(DmConstants.KEY_ALLOWED_HEIGHT);
        int width = (StringUtils.isEmpty(allowedWidth)) ? -1 : ContentFormatUtils.getIntValue(allowedWidth);
        int height = (StringUtils.isEmpty(allowedHeight)) ? -1 : ContentFormatUtils.getIntValue(allowedHeight);
        InputStream in = content.getContentStream();
        ContentAssetInfoTO assetInfo = (result.getItem() == null) ? new ContentAssetInfoTO() : (ContentAssetInfoTO) result.getItem();
        in = checkForImageSize(in, width, height, lessSize, assetInfo);
        content.getProperties().put(DmConstants.KEY_WIDTH, String.valueOf(assetInfo.getWidth()));
        content.getProperties().put(DmConstants.KEY_HEIGHT, String.valueOf(assetInfo.getHeight()));
        assetInfo.getWidth();
        result.setItem(assetInfo);
        content.setContentStream(in);
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 3 with ContentAssetInfoTO

use of org.craftercms.studio.api.v1.to.ContentAssetInfoTO in project studio by craftercms.

the class AssetDmContentProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String site = content.getProperty(DmConstants.KEY_SITE);
    String user = content.getProperty(DmConstants.KEY_USER);
    String path = content.getProperty(DmConstants.KEY_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String widthStr = content.getProperty(DmConstants.KEY_WIDTH);
    String heightStr = content.getProperty(DmConstants.KEY_HEIGHT);
    int width = (widthStr != null) ? Integer.parseInt(widthStr) : -1;
    int height = (heightStr != null) ? Integer.parseInt(heightStr) : -1;
    String unlockValue = content.getProperty(DmConstants.KEY_UNLOCK);
    // default is true for unlocking on save
    boolean unlock = (!StringUtils.isEmpty(unlockValue) && unlockValue.equalsIgnoreCase("false")) ? false : true;
    boolean isPreview = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_IS_PREVIEW));
    boolean isSystemAsset = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SYSTEM_ASSET));
    boolean createFolders = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_CREATE_FOLDERS));
    try {
        ContentAssetInfoTO oldAssetInfo = (ContentAssetInfoTO) result.getItem();
        ContentAssetInfoTO assetInfo = writeContentAsset(content, site, user, path, fileName, content.getContentStream(), width, height, createFolders, isPreview, unlock, isSystemAsset, result);
        if (oldAssetInfo != null) {
            oldAssetInfo.setFileExtension(assetInfo.getFileExtension());
            oldAssetInfo.setFileName(assetInfo.getFileName());
            oldAssetInfo.setSize(assetInfo.getSize());
            oldAssetInfo.setSizeUnit(assetInfo.getSizeUnit());
            result.setItem(oldAssetInfo);
        } else {
            result.setItem(assetInfo);
        }
    } catch (ServiceLayerException e) {
        throw new ContentProcessException("Failed to write " + content.getId() + ", " + e, e);
    } finally {
        content.closeContentStream();
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 4 with ContentAssetInfoTO

use of org.craftercms.studio.api.v1.to.ContentAssetInfoTO in project studio by craftercms.

the class AssetDmContentProcessor method writeContentAsset.

/**
 * upload content asset to the given path
 *
 * @param site
 * @param path
 * @param assetName
 * @param in
 *            input stream to read the asset from
 * @param width
 * @param height
 * @param createFolders
 * 				create missing folders?
 * @param isPreview
 * @param unlock
 * 			unlock the content upon update?
 * @return asset information
 * @throws ServiceLayerException
 */
protected ContentAssetInfoTO writeContentAsset(PipelineContent content, String site, String user, String path, String assetName, InputStream in, int width, int height, boolean createFolders, boolean isPreview, boolean unlock, boolean isSystemAsset, ResultTO result) throws ServiceLayerException {
    logger.debug("Writing content asset: [site: " + site + ", path: " + path + ", assetName: " + assetName + ", createFolders: " + createFolders);
    String ext = null;
    int index = assetName.lastIndexOf(".");
    if (index > 0 && (index + 1) < assetName.length()) {
        ext = assetName.substring(index + 1).toUpperCase();
    }
    String contentPath = path + FILE_SEPARATOR + assetName;
    try {
        // look up the path content first
        ContentItemTO parentContentItem = contentService.getContentItem(site, path, 0);
        boolean parentExists = contentService.contentExists(site, path);
        if (!parentExists && createFolders) {
            parentContentItem = createMissingFoldersInPath(site, path, isPreview);
            parentExists = contentService.contentExists(site, path);
        }
        if (parentExists && parentContentItem.isFolder()) {
            boolean exists = contentService.contentExists(site, path + FILE_SEPARATOR + assetName);
            ContentItemTO contentItem = null;
            if (exists) {
                contentItem = contentService.getContentItem(site, path + FILE_SEPARATOR + assetName, 0);
                updateFile(site, contentItem, contentPath, in, user, isPreview, unlock, result);
                content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
            } else {
                // TODO: define content type
                contentItem = createNewFile(site, parentContentItem, assetName, null, in, user, unlock, result);
                content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_CREATE);
                objectStateService.insertNewEntry(site, contentItem);
            }
            ContentAssetInfoTO assetInfo = new ContentAssetInfoTO();
            assetInfo.setFileName(assetName);
            long sizeInBytes = contentService.getContentSize(site, path + FILE_SEPARATOR + assetName);
            double convertedSize = 0;
            if (sizeInBytes > 0) {
                convertedSize = sizeInBytes / 1024d;
                if (convertedSize >= 1024) {
                    assetInfo.setSizeUnit(FILE_SIZE_MB);
                    assetInfo.setSize(convertedSize / 1024d);
                } else {
                    if (convertedSize > 0 && convertedSize < 1) {
                        assetInfo.setSize(1);
                    } else {
                        assetInfo.setSize(Math.round(convertedSize));
                    }
                    assetInfo.setSizeUnit(FILE_SIZE_KB);
                }
            }
            assetInfo.setFileExtension(ext);
            return assetInfo;
        } else {
            throw new ServiceLayerException(path + " does not exist or not a directory.");
        }
    } finally {
        ContentUtils.release(in);
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 5 with ContentAssetInfoTO

use of org.craftercms.studio.api.v1.to.ContentAssetInfoTO in project studio by craftercms.

the class CheckImageSizeProcessor method checkForImageSize.

/**
 * check the width and the height of the given image as an inputstream match the width and the height specified
 *
 * @param in
 * @param allowedWidth
 * @param allowedHeight
 * @param lessSize
 * @param assetInfo
 * @return image as input stream
 */
protected InputStream checkForImageSize(InputStream in, int allowedWidth, int allowedHeight, boolean lessSize, ContentAssetInfoTO assetInfo) throws ContentProcessException {
    ByteArrayOutputStream byteOutput = null;
    try {
        byteOutput = new ByteArrayOutputStream();
        // PORT CStudioWebScriptConstants.READ_BUFFER_LENGTH];
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = in.read(buffer)) > 0) {
            byteOutput.write(buffer, 0, read);
        }
        byte[] imageData = byteOutput.toByteArray();
        Image image = Toolkit.getDefaultToolkit().createImage(imageData);
        ImageIcon icon = new ImageIcon(image);
        int height = icon.getIconHeight();
        int width = icon.getIconWidth();
        if (allowedHeight > 0 && allowedWidth > 0) {
            validateImageSize(allowedWidth, allowedHeight, height, width, lessSize);
        }
        assetInfo.setHeight(height);
        assetInfo.setWidth(width);
        return new ByteArrayInputStream(imageData);
    } catch (IOException e) {
        throw new ContentProcessException(e);
    } finally {
        // close the original inputstream
        ContentUtils.release(in);
        ContentUtils.release(byteOutput);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)5 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)2 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)2 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)1 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)1 ItemState (org.craftercms.studio.api.v1.dal.ItemState)1 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)1 PreviewEventContext (org.craftercms.studio.api.v1.ebus.PreviewEventContext)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)1