Search in sources :

Example 1 with FileContentType

use of com.salesmanager.core.model.content.FileContentType 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 FileContentType

use of com.salesmanager.core.model.content.FileContentType 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 3 with FileContentType

use of com.salesmanager.core.model.content.FileContentType 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)

Example 4 with FileContentType

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

the class ContentFacadeImpl method addContentFile.

@Override
public void addContentFile(ContentFile file, String merchantStoreCode) {
    try {
        byte[] payload = file.getFile();
        String fileName = file.getName();
        try (InputStream targetStream = new ByteArrayInputStream(payload)) {
            String type = file.getContentType().split(FILE_CONTENT_DELIMETER)[0];
            FileContentType fileType = getFileContentType(type);
            InputContentFile cmsContent = new InputContentFile();
            cmsContent.setFileName(fileName);
            cmsContent.setMimeType(file.getContentType());
            cmsContent.setFile(targetStream);
            cmsContent.setFileContentType(fileType);
            contentService.addContentFile(merchantStoreCode, cmsContent);
        }
    } catch (ServiceException | IOException e) {
        throw new ServiceRuntimeException(e);
    }
}
Also used : InputContentFile(com.salesmanager.core.model.content.InputContentFile) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileContentType(com.salesmanager.core.model.content.FileContentType) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 5 with FileContentType

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

the class ContentFacadeImpl method delete.

@Override
public void delete(MerchantStore store, String fileName, String fileType) {
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(fileName, "File name cannot be null");
    try {
        FileContentType t = FileContentType.valueOf(fileType);
        contentService.removeFile(store.getCode(), t, fileName);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException(e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) FileContentType(com.salesmanager.core.model.content.FileContentType) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

FileContentType (com.salesmanager.core.model.content.FileContentType)6 OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 InputContentFile (com.salesmanager.core.model.content.InputContentFile)1 Customer (com.salesmanager.core.model.customer.Customer)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Order (com.salesmanager.core.model.order.Order)1 OrderProductDownload (com.salesmanager.core.model.order.orderproduct.OrderProductDownload)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1