Search in sources :

Example 6 with StaticAsset

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

the class StaticAssetStorageServiceImpl method getCacheFileModel.

@Override
public Map<String, String> getCacheFileModel(String fullUrl, Map<String, String> parameterMap) throws Exception {
    StaticAsset staticAsset = findStaticAsset(fullUrl);
    if (staticAsset == null) {
        throw new AssetNotFoundException("Unable to find an asset for the url (" + fullUrl + ")");
    }
    String mimeType = staticAsset.getMimeType();
    // extract the values for any named parameters
    Map<String, String> convertedParameters = namedOperationManager.manageNamedParameters(parameterMap);
    String cachedFileName = constructCacheFileName(staticAsset, convertedParameters);
    // Look for a shared file (this represents a file that was based on a file originally in the classpath.
    File cacheFile = getFileFromLocalRepository(cachedFileName);
    if (cacheFile.exists()) {
        return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }
    // Obtain the base file (that we may need to convert based on the parameters
    String baseCachedFileName = constructCacheFileName(staticAsset, null);
    File baseLocalFile = getFileFromLocalRepository(baseCachedFileName);
    if (!baseLocalFile.exists()) {
        if (broadleafFileService.checkForResourceOnClassPath(staticAsset.getFullUrl())) {
            cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
            baseLocalFile = broadleafFileService.getSharedLocalResource(baseCachedFileName);
            createLocalFileFromClassPathResource(staticAsset, baseLocalFile);
        } else {
            baseLocalFile = lookupAssetAndCreateLocalFile(staticAsset, baseLocalFile);
        }
    }
    if (convertedParameters.isEmpty()) {
        return buildModel(baseLocalFile.getAbsolutePath(), mimeType);
    } else {
        FileInputStream assetStream = new FileInputStream(baseLocalFile);
        BufferedInputStream original = new BufferedInputStream(assetStream);
        original.mark(0);
        Operation[] operations = artifactService.buildOperations(convertedParameters, original, staticAsset.getMimeType());
        InputStream converted = artifactService.convert(original, operations, staticAsset.getMimeType());
        createLocalFileFromInputStream(converted, cacheFile);
        if ("image/gif".equals(mimeType)) {
            mimeType = "image/png";
        }
        return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) GloballySharedInputStream(org.broadleafcommerce.common.file.service.GloballySharedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) AssetNotFoundException(org.broadleafcommerce.cms.common.AssetNotFoundException) Operation(org.broadleafcommerce.openadmin.server.service.artifact.image.Operation) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream)

Example 7 with StaticAsset

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

the class StaticAssetDaoImpl method readAllStaticAssets.

public List<StaticAsset> readAllStaticAssets() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class);
    Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class);
    criteria.select(handler);
    List<Predicate> restrictions = new ArrayList<Predicate>();
    List<Order> sorts = new ArrayList<Order>();
    try {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null);
            queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions);
            queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria, handler, sorts);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        return em.createQuery(criteria).getResultList();
    } catch (NoResultException e) {
        return new ArrayList<StaticAsset>();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ArrayList(java.util.ArrayList) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) NoResultException(javax.persistence.NoResultException) StaticAssetImpl(org.broadleafcommerce.cms.file.domain.StaticAssetImpl) Predicate(javax.persistence.criteria.Predicate)

Aggregations

StaticAsset (org.broadleafcommerce.cms.file.domain.StaticAsset)7 ArrayList (java.util.ArrayList)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 Predicate (javax.persistence.criteria.Predicate)3 StaticAssetImpl (org.broadleafcommerce.cms.file.domain.StaticAssetImpl)3 Order (javax.persistence.criteria.Order)2 ImageStaticAssetImpl (org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 NoResultException (javax.persistence.NoResultException)1 AssetNotFoundException (org.broadleafcommerce.cms.common.AssetNotFoundException)1 ImageStaticAsset (org.broadleafcommerce.cms.file.domain.ImageStaticAsset)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 GloballySharedInputStream (org.broadleafcommerce.common.file.service.GloballySharedInputStream)1