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