Search in sources :

Example 6 with StaticAssetStorage

use of org.broadleafcommerce.cms.file.domain.StaticAssetStorage in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetStorageServiceImpl method createStaticAssetStorage.

@Transactional("blTransactionManagerAssetStorageInfo")
@Override
public void createStaticAssetStorage(InputStream fileInputStream, StaticAsset staticAsset) throws IOException {
    if (StorageType.DATABASE.equals(staticAsset.getStorageType())) {
        StaticAssetStorage storage = create();
        storage.setStaticAssetId(staticAsset.getId());
        Blob uploadBlob = createBlob(fileInputStream, staticAsset.getFileSize());
        storage.setFileData(uploadBlob);
        save(storage);
    } else if (StorageType.FILESYSTEM.equals(staticAsset.getStorageType())) {
        FileWorkArea tempWorkArea = broadleafFileService.initializeWorkArea();
        // Convert the given URL from the asset to a system-specific suitable file path
        String destFileName = FilenameUtils.normalize(tempWorkArea.getFilePathLocation() + File.separator + FilenameUtils.separatorsToSystem(staticAsset.getFullUrl()));
        InputStream input = fileInputStream;
        byte[] buffer = new byte[getFileBufferSize()];
        File destFile = new File(destFileName);
        if (!destFile.getParentFile().exists()) {
            if (!destFile.getParentFile().mkdirs()) {
                if (!destFile.getParentFile().exists()) {
                    throw new RuntimeException("Unable to create parent directories for file: " + destFileName);
                }
            }
        }
        OutputStream output = new FileOutputStream(destFile);
        long maxFileSize = getMaxUploadSizeForFile(destFileName);
        try {
            int bytesRead;
            int totalBytesRead = 0;
            while ((bytesRead = input.read(buffer)) != -1) {
                totalBytesRead += bytesRead;
                if (totalBytesRead > maxFileSize) {
                    throw new IOException("Maximum Upload File Size Exceeded");
                }
                output.write(buffer, 0, bytesRead);
            }
            // close the output file stream prior to moving files around
            output.close();
            broadleafFileService.addOrUpdateResource(tempWorkArea, destFile, false);
        } finally {
            IOUtils.closeQuietly(output);
            broadleafFileService.closeWorkArea(tempWorkArea);
        }
    }
}
Also used : Blob(java.sql.Blob) StaticAssetStorage(org.broadleafcommerce.cms.file.domain.StaticAssetStorage) BufferedInputStream(java.io.BufferedInputStream) GloballySharedInputStream(org.broadleafcommerce.common.file.service.GloballySharedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

StaticAssetStorage (org.broadleafcommerce.cms.file.domain.StaticAssetStorage)6 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 GloballySharedInputStream (org.broadleafcommerce.common.file.service.GloballySharedInputStream)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Blob (java.sql.Blob)1 Query (javax.persistence.Query)1 FileWorkArea (org.broadleafcommerce.common.file.domain.FileWorkArea)1 Transactional (org.springframework.transaction.annotation.Transactional)1