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;
}
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;
}
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;
}
}
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;
}
}
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;
}
}
Aggregations