use of com.salesmanager.core.model.content.ImageContentFile in project shopizer by shopizer-ecommerce.
the class ProductImageServiceImpl method addProductImages.
@Override
public void addProductImages(Product product, List<ProductImage> productImages) throws ServiceException {
try {
for (ProductImage productImage : productImages) {
Assert.notNull(productImage.getImage());
InputStream inputStream = productImage.getImage();
ImageContentFile cmsContentImage = new ImageContentFile();
cmsContentImage.setFileName(productImage.getProductImage());
cmsContentImage.setFile(inputStream);
cmsContentImage.setFileContentType(FileContentType.PRODUCT);
addProductImage(product, productImage, cmsContentImage);
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.content.ImageContentFile in project shopizer by shopizer-ecommerce.
the class ProductFileManagerImpl method addProductImage.
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
try {
/**
* copy to input stream *
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Fake code simulating the copy
// You can generally do better with nio if you need...
// And please, unlike me, do something about the Exceptions :D
byte[] buffer = new byte[1024];
int len;
while ((len = contentImage.getFile().read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
// Open new InputStreams using the recorded bytes
// Can be repeated as many times as you wish
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
BufferedImage bufferedImage = ImageIO.read(is2);
if (bufferedImage == null) {
LOGGER.error("Cannot read image format for " + productImage.getProductImage());
throw new Exception("Cannot read image format " + productImage.getProductImage());
}
// contentImage.setBufferedImage(bufferedImage);
contentImage.setFile(is1);
// upload original -- L
contentImage.setFileContentType(FileContentType.PRODUCTLG);
uploadImage.addProductImage(productImage, contentImage);
/*
* //default large InputContentImage largeContentImage = new
* InputContentImage(ImageContentType.PRODUCT);
* largeContentImage.setFile(contentImage.getFile());
* largeContentImage.setDefaultImage(productImage.isDefaultImage());
* largeContentImage.setImageName(new
* StringBuilder().append("L-").append(productImage.getProductImage()).toString());
*
*
* uploadImage.uploadProductImage(configuration, productImage, largeContentImage);
*/
/*
* //default small InputContentImage smallContentImage = new
* InputContentImage(ImageContentType.PRODUCT);
* smallContentImage.setFile(contentImage.getFile());
* smallContentImage.setDefaultImage(productImage.isDefaultImage());
* smallContentImage.setImageName(new
* StringBuilder().append("S-").append(productImage.getProductImage()).toString());
*
* uploadImage.uploadProductImage(configuration, productImage, smallContentImage);
*/
// get template properties file
String slargeImageHeight = configuration.getProperty(PRODUCT_IMAGE_HEIGHT_SIZE);
String slargeImageWidth = configuration.getProperty(PRODUCT_IMAGE_WIDTH_SIZE);
// Resizes
if (!StringUtils.isBlank(slargeImageHeight) && !StringUtils.isBlank(slargeImageWidth)) {
// &&
// !StringUtils.isBlank(ssmallImageHeight)
// &&
// !StringUtils.isBlank(ssmallImageWidth))
// {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentType = fileNameMap.getContentTypeFor(contentImage.getFileName());
String extension = null;
if (contentType != null) {
extension = contentType.substring(contentType.indexOf('/') + 1, contentType.length());
}
if (extension == null) {
extension = "jpeg";
}
int largeImageHeight = Integer.parseInt(slargeImageHeight);
int largeImageWidth = Integer.parseInt(slargeImageWidth);
if (largeImageHeight <= 0 || largeImageWidth <= 0) {
String sizeMsg = "Image configuration set to an invalid value [PRODUCT_IMAGE_HEIGHT_SIZE] " + largeImageHeight + " , [PRODUCT_IMAGE_WIDTH_SIZE] " + largeImageWidth;
LOGGER.error(sizeMsg);
throw new ServiceException(sizeMsg);
}
if (!StringUtils.isBlank(configuration.getProperty(CROP_UPLOADED_IMAGES)) && configuration.getProperty(CROP_UPLOADED_IMAGES).equals(Constants.TRUE)) {
// crop image
ProductImageCropUtils utils = new ProductImageCropUtils(bufferedImage, largeImageWidth, largeImageHeight);
if (utils.isCropeable()) {
bufferedImage = utils.getCroppedImage();
}
}
// do not keep a large image for now, just take care of the regular image and a small image
// resize large
// ByteArrayOutputStream output = new ByteArrayOutputStream();
BufferedImage largeResizedImage;
if (bufferedImage.getWidth() > largeImageWidth || bufferedImage.getHeight() > largeImageHeight) {
largeResizedImage = ProductImageSizeUtils.resizeWithRatio(bufferedImage, largeImageWidth, largeImageHeight);
} else {
largeResizedImage = bufferedImage;
}
File tempLarge = File.createTempFile(new StringBuilder().append(productImage.getProduct().getId()).append("tmpLarge").toString(), "." + extension);
ImageIO.write(largeResizedImage, extension, tempLarge);
try (FileInputStream isLarge = new FileInputStream(tempLarge)) {
// IOUtils.copy(isLarge, output);
ImageContentFile largeContentImage = new ImageContentFile();
largeContentImage.setFileContentType(FileContentType.PRODUCT);
largeContentImage.setFileName(productImage.getProductImage());
largeContentImage.setFile(isLarge);
// largeContentImage.setBufferedImage(bufferedImage);
// largeContentImage.setFile(output);
// largeContentImage.setDefaultImage(false);
// largeContentImage.setImageName(new
// StringBuilder().append("L-").append(productImage.getProductImage()).toString());
uploadImage.addProductImage(productImage, largeContentImage);
// output.flush();
// output.close();
tempLarge.delete();
// now upload original
/*
* //resize small BufferedImage smallResizedImage = ProductImageSizeUtils.resize(cropped,
* smallImageWidth, smallImageHeight); File tempSmall = File.createTempFile(new
* StringBuilder().append(productImage.getProduct().getId()).append("tmpSmall").toString(),
* "." + extension ); ImageIO.write(smallResizedImage, extension, tempSmall);
*
* //byte[] is = IOUtils.toByteArray(new FileInputStream(tempSmall));
*
* FileInputStream isSmall = new FileInputStream(tempSmall);
*
* output = new ByteArrayOutputStream(); IOUtils.copy(isSmall, output);
*
*
* smallContentImage = new InputContentImage(ImageContentType.PRODUCT);
* smallContentImage.setFile(output); smallContentImage.setDefaultImage(false);
* smallContentImage.setImageName(new
* StringBuilder().append("S-").append(productImage.getProductImage()).toString());
*
* uploadImage.uploadProductImage(configuration, productImage, smallContentImage);
*
* output.flush(); output.close();
*
* tempSmall.delete();
*/
}
} else {
// small will be the same as the original
contentImage.setFileContentType(FileContentType.PRODUCT);
uploadImage.addProductImage(productImage, contentImage);
}
} catch (Exception e) {
throw new ServiceException(e);
} finally {
try {
productImage.getImage().close();
} catch (Exception ignore) {
}
}
}
use of com.salesmanager.core.model.content.ImageContentFile in project shopizer by shopizer-ecommerce.
the class ProductServiceImpl method saveOrUpdate.
private void saveOrUpdate(Product product) throws ServiceException {
LOGGER.debug("Save or update product ");
Validate.notNull(product, "product cannot be null");
Validate.notNull(product.getAvailabilities(), "product must have at least one availability");
Validate.notEmpty(product.getAvailabilities(), "product must have at least one availability");
// take care of product images separately
Set<ProductImage> originalProductImages = new HashSet<ProductImage>(product.getImages());
if (product.getId() != null && product.getId() > 0) {
super.update(product);
} else {
super.create(product);
}
/**
* Image creation needs extra service to save the file in the CMS
*/
List<Long> newImageIds = new ArrayList<Long>();
Set<ProductImage> images = product.getImages();
try {
if (images != null && images.size() > 0) {
for (ProductImage image : images) {
if (image.getImage() != null && (image.getId() == null || image.getId() == 0L)) {
image.setProduct(product);
InputStream inputStream = image.getImage();
ImageContentFile cmsContentImage = new ImageContentFile();
cmsContentImage.setFileName(image.getProductImage());
cmsContentImage.setFile(inputStream);
cmsContentImage.setFileContentType(FileContentType.PRODUCT);
productImageService.addProductImage(product, image, cmsContentImage);
newImageIds.add(image.getId());
} else {
if (image.getId() != null) {
productImageService.save(image);
newImageIds.add(image.getId());
}
}
}
}
// cleanup old and new images
if (originalProductImages != null) {
for (ProductImage image : originalProductImages) {
if (image.getImage() != null && image.getId() == null) {
image.setProduct(product);
InputStream inputStream = image.getImage();
ImageContentFile cmsContentImage = new ImageContentFile();
cmsContentImage.setFileName(image.getProductImage());
cmsContentImage.setFile(inputStream);
cmsContentImage.setFileContentType(FileContentType.PRODUCT);
productImageService.addProductImage(product, image, cmsContentImage);
newImageIds.add(image.getId());
} else {
if (!newImageIds.contains(image.getId())) {
productImageService.delete(image);
}
}
}
}
} catch (Exception e) {
LOGGER.error("Cannot save images " + e.getMessage());
}
}
use of com.salesmanager.core.model.content.ImageContentFile in project shopizer by shopizer-ecommerce.
the class ProductTest method testInsertImage.
/**
* Images
* @param product
* @throws Exception
*/
private void testInsertImage(Product product) throws Exception {
ProductImage productImage = new ProductImage();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classloader.getResourceAsStream("img/" + IMAGE_NAME);
ImageContentFile cmsContentImage = new ImageContentFile();
cmsContentImage.setFileName(IMAGE_NAME);
cmsContentImage.setFile(inputStream);
cmsContentImage.setFileContentType(FileContentType.PRODUCT);
productImage.setProductImage(IMAGE_NAME);
productImage.setProduct(product);
// absolutely required otherwise the file is not created on disk
productImage.setImage(inputStream);
product.getImages().add(productImage);
// saves the ProductImage entity and the file on disk
productService.update(product);
}
Aggregations