Search in sources :

Example 1 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent 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 PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent 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 PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent in project studio by craftercms.

the class ExtractDependencyProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String site = content.getProperty(DmConstants.KEY_SITE);
    String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String path = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
    try {
        dependencyService.upsertDependencies(site, path);
    } catch (ServiceLayerException e) {
        throw new ContentProcessException(e);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 4 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent 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 5 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent 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)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)5 ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)4 InputStream (java.io.InputStream)2 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 ContentProcessorPipeline (org.craftercms.studio.api.v1.content.pipeline.ContentProcessorPipeline)1 PipelineContent (org.craftercms.studio.api.v1.content.pipeline.PipelineContent)1 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)1 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)1 RepositoryLockedException (org.craftercms.studio.api.v2.exception.RepositoryLockedException)1 PipelineContentImpl (org.craftercms.studio.impl.v1.content.pipeline.PipelineContentImpl)1