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());
}
}
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);
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations