Search in sources :

Example 1 with StaticAsset

use of org.broadleafcommerce.cms.file.domain.StaticAsset 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)

Example 2 with StaticAsset

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

the class AdminAssetUploadController method upload.

@RequestMapping(value = "/{id}/uploadAsset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, @PathVariable(value = "sectionKey") String sectionKey, @PathVariable(value = "id") String id) throws IOException {
    Map<String, Object> responseMap = new HashMap<String, Object>();
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("entityType", sectionKey);
    properties.put("entityId", id);
    StaticAsset staticAsset = staticAssetService.createStaticAssetFromFile(file, properties);
    staticAssetStorageService.createStaticAssetStorageFromFile(file, staticAsset);
    String staticAssetUrlPrefix = staticAssetService.getStaticAssetUrlPrefix();
    if (staticAssetUrlPrefix != null && !staticAssetUrlPrefix.startsWith("/")) {
        staticAssetUrlPrefix = "/" + staticAssetUrlPrefix;
    }
    String assetUrl = staticAssetUrlPrefix + staticAsset.getFullUrl();
    responseMap.put("adminDisplayAssetUrl", request.getContextPath() + assetUrl);
    responseMap.put("assetUrl", assetUrl);
    if (staticAsset instanceof ImageStaticAssetImpl) {
        responseMap.put("image", Boolean.TRUE);
        responseMap.put("assetThumbnail", assetUrl + "?smallAdminThumbnail");
        responseMap.put("assetLarge", assetUrl + "?largeAdminThumbnail");
    } else {
        responseMap.put("image", Boolean.FALSE);
    }
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "text/html; charset=utf-8");
    return new ResponseEntity<Map<String, Object>>(responseMap, responseHeaders, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) ImageStaticAssetImpl(org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with StaticAsset

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

the class StaticAssetDaoImpl method readStaticAssetByFullUrl.

@Override
public StaticAsset readStaticAssetByFullUrl(String fullUrl) {
    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>();
    restrictions.add(builder.equal(handler.get("fullUrl"), fullUrl));
    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()]));
        if (!org.apache.commons.collections.CollectionUtils.isEmpty(sorts)) {
            criteria.orderBy(sorts);
        }
        TypedQuery<StaticAsset> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List<StaticAsset> response = query.getResultList();
        if (response.size() > 0) {
            return response.get(0);
        }
        return null;
    } finally {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().breakdown(StaticAssetImpl.class, null);
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ArrayList(java.util.ArrayList) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) StaticAssetImpl(org.broadleafcommerce.cms.file.domain.StaticAssetImpl) Predicate(javax.persistence.criteria.Predicate)

Example 4 with StaticAsset

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

the class StaticAssetDaoImpl method readStaticAssetById.

@Override
public StaticAsset readStaticAssetById(Long id) {
    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>();
    restrictions.add(builder.equal(handler.get("id"), id));
    if (queryExtensionManager != null) {
        queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null);
        queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions);
    }
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<StaticAsset> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<StaticAsset> response = query.getResultList();
    if (response.size() > 0) {
        return response.get(0);
    }
    return null;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) StaticAssetImpl(org.broadleafcommerce.cms.file.domain.StaticAssetImpl) Predicate(javax.persistence.criteria.Predicate)

Example 5 with StaticAsset

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

the class AdminAssetUploadController method upload.

/**
 * Used by the Asset list view to upload an asset and then immediately show the
 * edit form for that record.
 *
 * @param request
 * @param file
 * @param sectionKey
 * @return
 * @throws IOException
 */
@RequestMapping(value = "/uploadAsset", method = RequestMethod.POST)
public String upload(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @RequestParam("file") MultipartFile file, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
    StaticAsset staticAsset = staticAssetService.createStaticAssetFromFile(file, null);
    staticAssetStorageService.createStaticAssetStorageFromFile(file, staticAsset);
    String staticAssetUrlPrefix = staticAssetService.getStaticAssetUrlPrefix();
    if (staticAssetUrlPrefix != null && !staticAssetUrlPrefix.startsWith("/")) {
        staticAssetUrlPrefix = "/" + staticAssetUrlPrefix;
    }
    return "redirect:/assets/" + staticAsset.getId();
}
Also used : StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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