Search in sources :

Example 1 with OutputContentFile

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

the class ShoppingOrderDownloadController method downloadFile.

/**
 * Virtual product(s) download link
 * @param id
 * @param model
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping("/download/{orderId}/{id}.html")
@ResponseBody
public byte[] downloadFile(@PathVariable Long orderId, @PathVariable Long id, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    FileContentType fileType = FileContentType.PRODUCT_DIGITAL;
    // get customer and check order
    Order order = orderService.getById(orderId);
    if (order == null) {
        LOGGER.warn("Order is null for id " + orderId);
        response.sendError(404, "Image not found");
        return null;
    }
    // order belongs to customer
    Customer customer = (Customer) super.getSessionAttribute(Constants.CUSTOMER, request);
    if (customer == null) {
        response.sendError(404, "Image not found");
        return null;
    }
    // get it from OrderProductDownlaod
    String fileName = null;
    OrderProductDownload download = orderProductDownloadService.getById(id);
    if (download == null) {
        LOGGER.warn("OrderProductDownload is null for id " + id);
        response.sendError(404, "Image not found");
        return null;
    }
    fileName = download.getOrderProductFilename();
    // needs to query the new API
    OutputContentFile file = contentService.getContentFile(store.getCode(), fileType, fileName);
    if (file != null) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        return file.getFile().toByteArray();
    } else {
        LOGGER.warn("Image not found for OrderProductDownload id " + id);
        response.sendError(404, "Image not found");
        return null;
    }
// product image
// example -> /download/12345/120.html
}
Also used : Order(com.salesmanager.core.model.order.Order) Customer(com.salesmanager.core.model.customer.Customer) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) FileContentType(com.salesmanager.core.model.content.FileContentType) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with OutputContentFile

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

the class S3ProductContentFileManager method getImages.

@Override
public List<OutputContentFile> getImages(String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    try {
        // get buckets
        String bucketName = bucketName();
        ListObjectsV2Request listObjectsRequest = new ListObjectsV2Request().withBucketName(bucketName).withPrefix(nodePath(merchantStoreCode));
        List<OutputContentFile> files = null;
        final AmazonS3 s3 = s3Client();
        ListObjectsV2Result results = s3.listObjectsV2(listObjectsRequest);
        List<S3ObjectSummary> objects = results.getObjectSummaries();
        for (S3ObjectSummary os : objects) {
            if (files == null) {
                files = new ArrayList<OutputContentFile>();
            }
            String mimetype = URLConnection.guessContentTypeFromName(os.getKey());
            if (!StringUtils.isBlank(mimetype)) {
                S3Object o = s3.getObject(bucketName, os.getKey());
                byte[] byteArray = IOUtils.toByteArray(o.getObjectContent());
                ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
                baos.write(byteArray, 0, byteArray.length);
                OutputContentFile ct = new OutputContentFile();
                ct.setFile(baos);
                files.add(ct);
            }
        }
        return files;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ServiceException(com.salesmanager.core.business.exception.ServiceException) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 3 with OutputContentFile

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

the class GCPProductContentFileManager method getImages.

/**
 * List files
 */
@Override
public List<OutputContentFile> getImages(String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    InputStream inputStream = null;
    try {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        String bucketName = bucketName();
        if (!this.bucketExists(storage, bucketName)) {
            return null;
        }
        Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(merchantStoreCode));
        List<OutputContentFile> files = new ArrayList<OutputContentFile>();
        for (Blob blob : blobs.iterateAll()) {
            blob.getName();
            ReadChannel reader = blob.reader();
            inputStream = Channels.newInputStream(reader);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, outputStream);
            OutputContentFile ct = new OutputContentFile();
            ct.setFile(outputStream);
            files.add(ct);
        }
        return files;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) ServiceException(com.salesmanager.core.business.exception.ServiceException) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) ReadChannel(com.google.cloud.ReadChannel)

Example 4 with OutputContentFile

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

the class GCPProductContentFileManager method getProductImage.

@Override
public OutputContentFile getProductImage(String merchantStoreCode, String productCode, String imageName, ProductImageSize size) throws ServiceException {
    InputStream inputStream = null;
    try {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        String bucketName = bucketName();
        if (!this.bucketExists(storage, bucketName)) {
            return null;
        }
        Blob blob = storage.get(BlobId.of(bucketName, filePath(merchantStoreCode, productCode, size.name(), imageName)));
        ReadChannel reader = blob.reader();
        inputStream = Channels.newInputStream(reader);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        IOUtils.copy(inputStream, outputStream);
        OutputContentFile ct = new OutputContentFile();
        ct.setFile(outputStream);
        ct.setFileName(blob.getName());
        return ct;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) ServiceException(com.salesmanager.core.business.exception.ServiceException) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) ReadChannel(com.google.cloud.ReadChannel)

Example 5 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(final String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    try {
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(merchantStoreCode);
        Node<String, Object> merchantNode = this.getNode(nodePath.toString());
        Set<Node<String, Object>> childs = merchantNode.getChildren();
        // TODO image sizes
        for (Node<String, Object> node : childs) {
            for (String key : node.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) Node(org.infinispan.tree.Node) 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)

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