Search in sources :

Example 1 with ImageStaticAsset

use of org.broadleafcommerce.cms.file.domain.ImageStaticAsset in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetServiceImpl method createStaticAsset.

@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public StaticAsset createStaticAsset(InputStream inputStream, String fileName, long fileSize, Map<String, String> properties) {
    if (properties == null) {
        properties = new HashMap<String, String>();
    }
    String fullUrl = buildAssetURL(properties, fileName);
    StringBuilder urlBuilder = new StringBuilder();
    urlBuilder.append(fullUrl);
    ExtensionResultStatusType resultStatusType = staticAssetExtensionManager.getProxy().modifyDuplicateAssetURL(urlBuilder);
    fullUrl = urlBuilder.toString();
    StaticAsset newAsset = staticAssetDao.readStaticAssetByFullUrl(fullUrl);
    // logic for handling duplicate files.
    if (resultStatusType != ExtensionResultStatusType.HANDLED) {
        int count = 0;
        while (newAsset != null) {
            count++;
            // try the new format first, then the old
            newAsset = staticAssetDao.readStaticAssetByFullUrl(getCountUrl(fullUrl, count, false));
            if (newAsset == null) {
                newAsset = staticAssetDao.readStaticAssetByFullUrl(getCountUrl(fullUrl, count, true));
            }
        }
        if (count > 0) {
            fullUrl = getCountUrl(fullUrl, count, false);
        }
    }
    try {
        ImageMetadata metadata = imageArtifactProcessor.getImageMetadata(inputStream);
        newAsset = new ImageStaticAssetImpl();
        ((ImageStaticAsset) newAsset).setWidth(metadata.getWidth());
        ((ImageStaticAsset) newAsset).setHeight(metadata.getHeight());
    } catch (Exception e) {
        // must not be an image stream
        LOG.warn("unable to convert asset:" + fileName + " into Image");
        LOG.debug(e);
        if (getShouldAcceptNonImageAsset()) {
            newAsset = createNonImageAsset(inputStream, fileName, properties);
        } else {
            throw new RuntimeException("Selected Asset/File was not valid image.");
        }
    }
    if (storeAssetsOnFileSystem) {
        newAsset.setStorageType(StorageType.FILESYSTEM);
    } else {
        newAsset.setStorageType(StorageType.DATABASE);
    }
    newAsset.setName(fileName);
    getMimeType(inputStream, fileName, newAsset);
    newAsset.setFileExtension(getFileExtension(fileName));
    newAsset.setFileSize(fileSize);
    newAsset.setFullUrl(fullUrl);
    return staticAssetDao.addOrUpdateStaticAsset(newAsset, false);
}
Also used : ImageStaticAsset(org.broadleafcommerce.cms.file.domain.ImageStaticAsset) ImageMetadata(org.broadleafcommerce.openadmin.server.service.artifact.image.ImageMetadata) ImageStaticAsset(org.broadleafcommerce.cms.file.domain.ImageStaticAsset) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ImageStaticAssetImpl(org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ImageStaticAsset (org.broadleafcommerce.cms.file.domain.ImageStaticAsset)1 ImageStaticAssetImpl (org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl)1 StaticAsset (org.broadleafcommerce.cms.file.domain.StaticAsset)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 ImageMetadata (org.broadleafcommerce.openadmin.server.service.artifact.image.ImageMetadata)1 Transactional (org.springframework.transaction.annotation.Transactional)1