Search in sources :

Example 6 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImplTest method testCreateAddFiles.

public void testCreateAddFiles() throws Exception {
    FileWorkArea workArea1 = bfs.initializeWorkArea();
    File f1 = new File(workArea1.getFilePathLocation() + "test2.txt");
    FileWriter fw = new FileWriter(f1);
    fw.append("Test File 2");
    fw.close();
    File f2 = new File(workArea1.getFilePathLocation() + "test3.txt");
    FileWriter fw2 = new FileWriter(f2);
    fw2.append("Test File 3");
    fw2.close();
    List<File> files = new ArrayList<>();
    files.add(f1);
    files.add(f2);
    bfs.addOrUpdateResources(workArea1, files, false);
    bfs.closeWorkArea(workArea1);
    File resource = bfs.getResource("test2.txt");
    assertTrue(resource.exists());
    resource = bfs.getResource("test3.txt");
    assertTrue(resource.exists());
    bfs.removeResource("test2.txt");
    bfs.removeResource("test3.txt");
    resource = bfs.getResource("test3.txt");
    assertFalse(resource.exists());
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Example 7 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class ResourceBundlingServiceImpl method saveBundle.

protected void saveBundle(Resource resource) {
    FileWorkArea tempWorkArea = fileService.initializeWorkArea();
    String fileToSave = FilenameUtils.separatorsToSystem(getResourcePath(resource.getDescription()));
    String tempFilename = FilenameUtils.concat(tempWorkArea.getFilePathLocation(), fileToSave);
    File tempFile = new File(tempFilename);
    if (!tempFile.getParentFile().exists()) {
        if (!tempFile.getParentFile().mkdirs()) {
            if (!tempFile.getParentFile().exists()) {
                throw new RuntimeException("Unable to create parent directories for file: " + tempFilename);
            }
        }
    }
    BufferedOutputStream out = null;
    InputStream ris = null;
    try {
        ris = resource.getInputStream();
        out = new BufferedOutputStream(new FileOutputStream(tempFile));
        StreamUtils.copy(ris, out);
        ris.close();
        out.close();
        fileService.addOrUpdateResourceForPath(tempWorkArea, tempFile, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ris);
        IOUtils.closeQuietly(out);
        fileService.closeWorkArea(tempWorkArea);
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetStorageServiceImpl method createLocalFileFromInputStream.

protected void createLocalFileFromInputStream(InputStream is, File baseLocalFile) throws IOException {
    FileOutputStream tos = null;
    FileWorkArea workArea = null;
    try {
        if (!baseLocalFile.getParentFile().exists()) {
            boolean directoriesCreated = false;
            if (!baseLocalFile.getParentFile().exists()) {
                directoriesCreated = baseLocalFile.getParentFile().mkdirs();
                if (!directoriesCreated) {
                    // proper permissions and this is an error we need to report.
                    if (!baseLocalFile.getParentFile().exists()) {
                        throw new RuntimeException("Unable to create middle directories for file: " + baseLocalFile.getAbsolutePath());
                    }
                }
            }
        }
        workArea = broadleafFileService.initializeWorkArea();
        File tmpFile = new File(FilenameUtils.concat(workArea.getFilePathLocation(), baseLocalFile.getName()));
        tos = new FileOutputStream(tmpFile);
        IOUtils.copy(is, tos);
        // close the input/output streams before trying to move files around
        is.close();
        tos.close();
        // Adding locks would be useless here since another VM could be executing the code.
        if (!baseLocalFile.exists()) {
            try {
                FileUtils.moveFile(tmpFile, baseLocalFile);
            } catch (FileExistsException e) {
                // No problem
                if (LOG.isDebugEnabled()) {
                    LOG.debug("File exists error moving file " + tmpFile.getAbsolutePath(), e);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(tos);
        if (workArea != null) {
            broadleafFileService.closeWorkArea(workArea);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileExistsException(org.apache.commons.io.FileExistsException)

Example 9 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea 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

FileWorkArea (org.broadleafcommerce.common.file.domain.FileWorkArea)9 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 Blob (java.sql.Blob)1 FileExistsException (org.apache.commons.io.FileExistsException)1 StaticAssetStorage (org.broadleafcommerce.cms.file.domain.StaticAssetStorage)1 FileServiceException (org.broadleafcommerce.common.file.FileServiceException)1 GloballySharedInputStream (org.broadleafcommerce.common.file.service.GloballySharedInputStream)1 SiteMapConfiguration (org.broadleafcommerce.common.sitemap.domain.SiteMapConfiguration)1 SiteMapGeneratorConfiguration (org.broadleafcommerce.common.sitemap.domain.SiteMapGeneratorConfiguration)1 Transactional (org.springframework.transaction.annotation.Transactional)1