Search in sources :

Example 6 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method getImages.

@Override
public List<OutputContentFile> getImages(Product product) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(product.getMerchantStore().getCode());
        Node<String, Object> merchantNode = this.getNode(nodePath.toString());
        if (merchantNode == null) {
            return null;
        }
        for (String key : merchantNode.getKeys()) {
            byte[] imageBytes = (byte[]) merchantNode.get(key);
            OutputContentFile contentImage = new OutputContentFile();
            InputStream input = new ByteArrayInputStream(imageBytes);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(input, output);
            String contentType = fileNameMap.getContentTypeFor(key);
            contentImage.setFile(output);
            contentImage.setMimeType(contentType);
            contentImage.setFileName(key);
            images.add(contentImage);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
    }
    return images;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNameMap(java.net.FileNameMap) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 7 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method getProductImage.

private OutputContentFile getProductImage(String merchantStoreCode, String productCode, String imageName, String size) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    InputStream input = null;
    OutputContentFile contentImage = new OutputContentFile();
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        // SMALL by default
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(merchantStoreCode).append(Constants.SLASH).append(productCode).append(Constants.SLASH).append(size);
        Node<String, Object> productNode = this.getNode(nodePath.toString());
        byte[] imageBytes = (byte[]) productNode.get(imageName);
        if (imageBytes == null) {
            LOGGER.warn("Image " + imageName + " does not exist");
            // no post processing will occur
            return null;
        }
        input = new ByteArrayInputStream(imageBytes);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(input, output);
        String contentType = fileNameMap.getContentTypeFor(imageName);
        contentImage.setFile(output);
        contentImage.setMimeType(contentType);
        contentImage.setFileName(imageName);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (Exception ignore) {
            }
        }
    }
    return contentImage;
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNameMap(java.net.FileNameMap) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 8 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class ImagesController method printImage.

/**
 * Exclusive method for dealing with product images
 * @param storeCode
 * @param productCode
 * @param imageName
 * @param extension
 * @param request
 * @return
 * @throws IOException
 */
@RequestMapping(value = "/static/products/{storeCode}/{productCode}/{imageSize}/{imageName}.{extension}", produces = { "image/gif", "image/jpg", "image/png", "application/octet-stream" })
@ResponseBody
public byte[] printImage(@PathVariable final String storeCode, @PathVariable final String productCode, @PathVariable final String imageSize, @PathVariable final String imageName, @PathVariable final String extension, HttpServletRequest request) throws IOException {
    // product image small
    // example small product image -> /static/products/DEFAULT/TB12345/SMALL/product1.jpg
    // example large product image -> /static/products/DEFAULT/TB12345/LARGE/product1.jpg
    /**
     * List of possible imageType
     */
    ProductImageSize size = ProductImageSize.SMALL;
    if (FileContentType.PRODUCTLG.name().equals(imageSize)) {
        size = ProductImageSize.LARGE;
    }
    OutputContentFile image = null;
    try {
        image = productImageService.getProductImage(storeCode, productCode, new StringBuilder().append(imageName).append(".").append(extension).toString(), size);
    } catch (ServiceException e) {
        LOGGER.error("Cannot retrieve image " + imageName, e);
    }
    if (image != null) {
        return image.getFile().toByteArray();
    } else {
        // empty image placeholder
        return tempImage;
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ProductImageSize(com.salesmanager.core.model.catalog.product.file.ProductImageSize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class ImagesController method printImage.

/**
 * Logo, content image
 * @param storeId
 * @param imageType (LOGO, CONTENT, IMAGE)
 * @param imageName
 * @return
 * @throws IOException
 * @throws ServiceException
 */
@RequestMapping("/static/files/{storeCode}/{imageType}/{imageName}.{extension}")
@ResponseBody
public byte[] printImage(@PathVariable final String storeCode, @PathVariable final String imageType, @PathVariable final String imageName, @PathVariable final String extension) throws IOException, ServiceException {
    // example -> /static/files/DEFAULT/CONTENT/myImage.png
    FileContentType imgType = null;
    if (FileContentType.LOGO.name().equals(imageType)) {
        imgType = FileContentType.LOGO;
    }
    if (FileContentType.IMAGE.name().equals(imageType)) {
        imgType = FileContentType.IMAGE;
    }
    if (FileContentType.PROPERTY.name().equals(imageType)) {
        imgType = FileContentType.PROPERTY;
    }
    OutputContentFile image = contentService.getContentFile(storeCode, imgType, new StringBuilder().append(imageName).append(".").append(extension).toString());
    if (image != null) {
        return image.getFile().toByteArray();
    } else {
        return tempImage;
    }
}
Also used : OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) FileContentType(com.salesmanager.core.model.content.FileContentType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class FilesController method downloadProduct.

/**
 * Requires admin with roles admin, superadmin or product
 * @param storeCode
 * @param fileName
 * @param extension
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@PreAuthorize("hasRole('PRODUCTS')")
@RequestMapping("/admin/files/downloads/{storeCode}/{fileName}.{extension}")
@ResponseBody
public byte[] downloadProduct(@PathVariable final String storeCode, @PathVariable final String fileName, @PathVariable final String extension, HttpServletRequest request, HttpServletResponse response) throws Exception {
    FileContentType fileType = FileContentType.PRODUCT_DIGITAL;
    String fileNameAndExtension = new StringBuilder().append(fileName).append(".").append(extension).toString();
    // needs to query the new API
    OutputContentFile file = contentService.getContentFile(storeCode, fileType, fileNameAndExtension);
    if (file != null) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileNameAndExtension + "\"");
        return file.getFile().toByteArray();
    } else {
        LOGGER.debug("File not found " + fileName + "." + extension);
        response.sendError(404, Constants.FILE_NOT_FOUND);
        return null;
    }
}
Also used : OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) FileContentType(com.salesmanager.core.model.content.FileContentType) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 FileContentType (com.salesmanager.core.model.content.FileContentType)4 FileNameMap (java.net.FileNameMap)4 InputContentFile (com.salesmanager.core.model.content.InputContentFile)3 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 IOException (java.io.IOException)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)2 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)2 S3Object (com.amazonaws.services.s3.model.S3Object)2 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 ReadChannel (com.google.cloud.ReadChannel)2