Search in sources :

Example 16 with OutputContentFile

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

the class ContentServiceImpl method renameFile.

@Override
public void renameFile(String merchantStoreCode, FileContentType fileContentType, Optional<String> path, String originalName, String newName) throws ServiceException {
    OutputContentFile file = contentFileManager.getFile(merchantStoreCode, path, fileContentType, originalName);
    if (file == null) {
        throw new ServiceException("File name [" + originalName + "] not found for merchant [" + merchantStoreCode + "]");
    }
    ByteArrayOutputStream os = file.getFile();
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    // remove file
    contentFileManager.removeFile(merchantStoreCode, fileContentType, originalName, path);
    // recreate file
    InputContentFile inputFile = new InputContentFile();
    inputFile.setFileContentType(fileContentType);
    inputFile.setFileName(newName);
    inputFile.setMimeType(file.getMimeType());
    inputFile.setFile(is);
    contentFileManager.addFile(merchantStoreCode, path, inputFile);
}
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) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with OutputContentFile

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

the class FilesController method downloadFile.

/**
 * Serves static files (css, js ...) the repository is a single node by merchant
 * @param storeCode
 * @param extension
 * @return
 * @throws IOException
 * @throws ServiceException
 */
@RequestMapping("/static/files/{storeCode}/{fileName}.{extension}")
@ResponseBody
public byte[] downloadFile(@PathVariable final String storeCode, @PathVariable final String fileName, @PathVariable final String extension, HttpServletRequest request, HttpServletResponse response) throws IOException, ServiceException {
    // example -> /files/<store code>/myfile.css
    FileContentType fileType = FileContentType.STATIC_FILE;
    // needs to query the new API
    OutputContentFile file = contentService.getContentFile(storeCode, fileType, new StringBuilder().append(fileName).append(".").append(extension).toString());
    if (file != null) {
        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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 18 with OutputContentFile

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

the class ContentImagesTest method createStoreLogo.

// @Test
@Ignore
public void createStoreLogo() throws ServiceException, FileNotFoundException, IOException {
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    final File file1 = new File("C:/doc/Hadoop.jpg");
    if (!file1.exists() || !file1.canRead()) {
        throw new ServiceException("Can't read" + file1.getAbsolutePath());
    }
    byte[] is = IOUtils.toByteArray(new FileInputStream(file1));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(is);
    InputContentFile cmsContentImage = new InputContentFile();
    cmsContentImage.setFileName(file1.getName());
    cmsContentImage.setFile(inputStream);
    // logo as a content
    contentService.addLogo(store.getCode(), cmsContentImage);
    store.setStoreLogo(file1.getName());
    merchantService.update(store);
    // query the store
    store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    // get the logo
    String logo = store.getStoreLogo();
    OutputContentFile image = contentService.getContentFile(store.getCode(), FileContentType.LOGO, logo);
    // print image
    OutputStream outputStream = new FileOutputStream("C:/doc/logo-" + image.getFileName());
    ByteArrayOutputStream baos = image.getFile();
    baos.writeTo(outputStream);
    // remove image
    contentService.removeFile(store.getCode(), FileContentType.LOGO, store.getStoreLogo());
}
Also used : InputContentFile(com.salesmanager.core.model.content.InputContentFile) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) InputContentFile(com.salesmanager.core.model.content.InputContentFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore)

Example 19 with OutputContentFile

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

the class StaticContentTest method createImage.

@Test
public void createImage() throws ServiceException, FileNotFoundException, IOException {
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    final File file1 = new File(IMAGE_FILE);
    if (!file1.exists() || !file1.canRead()) {
        throw new ServiceException("Can't read" + file1.getAbsolutePath());
    }
    final byte[] is = IOUtils.toByteArray(new FileInputStream(file1));
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(is);
    final InputContentFile cmsContentImage = new InputContentFile();
    cmsContentImage.setFileName(file1.getName());
    cmsContentImage.setFile(inputStream);
    cmsContentImage.setFileContentType(FileContentType.IMAGE);
    // Add image
    contentService.addContentFile(store.getCode(), cmsContentImage);
    // get image
    OutputContentFile image = contentService.getContentFile(store.getCode(), FileContentType.IMAGE, file1.getName());
    // print image
    OutputStream outputStream = new FileOutputStream(OUTPUT_FOLDER + image.getFileName());
    ByteArrayOutputStream baos = image.getFile();
    baos.writeTo(outputStream);
    // remove image
    contentService.removeFile(store.getCode(), FileContentType.IMAGE, file1.getName());
}
Also used : InputContentFile(com.salesmanager.core.model.content.InputContentFile) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) InputContentFile(com.salesmanager.core.model.content.InputContentFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 20 with OutputContentFile

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

the class ProductTest method testViewImage.

private void testViewImage(Product product) throws Exception {
    ProductImage productImage = product.getProductImage();
    // get physical small image
    OutputContentFile contentFile = productImageService.getProductImage(product.getMerchantStore().getCode(), product.getSku(), productImage.getProductImage(), ProductImageSize.SMALL);
    Assert.assertNotNull(contentFile);
    // get physical original image
    contentFile = productImageService.getProductImage(product.getMerchantStore().getCode(), product.getSku(), productImage.getProductImage(), ProductImageSize.LARGE);
    Assert.assertNotNull(contentFile);
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile)

Aggregations

OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 FileContentType (com.salesmanager.core.model.content.FileContentType)4 FileNameMap (java.net.FileNameMap)4 InputContentFile (com.salesmanager.core.model.content.InputContentFile)3 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 IOException (java.io.IOException)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)2 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)2 S3Object (com.amazonaws.services.s3.model.S3Object)2 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 ReadChannel (com.google.cloud.ReadChannel)2