use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class S3ProductContentFileManager method removeImages.
@Override
public void removeImages(String merchantStoreCode) throws ServiceException {
try {
// get buckets
String bucketName = bucketName();
final AmazonS3 s3 = s3Client();
s3.deleteObject(bucketName, nodePath(merchantStoreCode));
LOGGER.info("Remove folder");
} catch (final Exception e) {
LOGGER.error("Error while removing folder", e);
throw new ServiceException(e);
}
}
use of com.salesmanager.core.business.exception.ServiceException 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.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class S3ProductContentFileManager method addProductImage.
@Override
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
try {
// get buckets
String bucketName = bucketName();
final AmazonS3 s3 = s3Client();
String nodePath = this.nodePath(productImage.getProduct().getMerchantStore().getCode(), productImage.getProduct().getSku(), contentImage);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentImage.getMimeType());
PutObjectRequest request = new PutObjectRequest(bucketName, nodePath + productImage.getProductImage(), contentImage.getFile(), metadata);
request.setCannedAcl(CannedAccessControlList.PublicRead);
s3.putObject(request);
LOGGER.info("Product add file");
} catch (final Exception e) {
LOGGER.error("Error while removing file", e);
throw new ServiceException(e);
}
}
use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.
the class GCPProductContentFileManager method addProductImage.
@Override
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
Storage storage = StorageOptions.getDefaultInstance().getService();
String bucketName = bucketName();
if (!this.bucketExists(storage, bucketName)) {
createBucket(storage, bucketName);
}
// build filename
StringBuilder fileName = new StringBuilder().append(filePath(productImage.getProduct().getMerchantStore().getCode(), productImage.getProduct().getSku(), contentImage.getFileContentType())).append(productImage.getProductImage());
try {
byte[] targetArray = IOUtils.toByteArray(contentImage.getFile());
BlobId blobId = BlobId.of(bucketName, fileName.toString());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").build();
storage.create(blobInfo, targetArray);
Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
} catch (IOException ioe) {
throw new ServiceException(ioe);
}
}
use of com.salesmanager.core.business.exception.ServiceException 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) {
}
}
}
}
Aggregations