Search in sources :

Example 1 with FileServiceException

use of org.broadleafcommerce.common.file.FileServiceException in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImpl method initializeWorkArea.

/**
 * Create a file work area that can be used for further operations.
 * @return
 */
@Override
public FileWorkArea initializeWorkArea() {
    String baseDirectory = getBaseDirectory(false);
    String tempDirectory = getTempDirectory(baseDirectory);
    FileWorkArea fw = new FileWorkArea();
    fw.setFilePathLocation(tempDirectory);
    File tmpDir = new File(tempDirectory);
    if (!tmpDir.exists()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Creating temp directory named " + tempDirectory);
        }
        if (!tmpDir.mkdirs()) {
            throw new FileServiceException("Unable to create temporary working directory for " + tempDirectory);
        }
    }
    return fw;
}
Also used : FileServiceException(org.broadleafcommerce.common.file.FileServiceException) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Example 2 with FileServiceException

use of org.broadleafcommerce.common.file.FileServiceException in project BroadleafCommerce by BroadleafCommerce.

the class FileSystemFileServiceProvider method addOrUpdateResourcesForPaths.

@Override
public List<String> addOrUpdateResourcesForPaths(FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) {
    List<String> result = new ArrayList<String>();
    for (File srcFile : files) {
        if (!srcFile.getAbsolutePath().startsWith(workArea.getFilePathLocation())) {
            throw new FileServiceException("Attempt to update file " + srcFile.getAbsolutePath() + " that is not in the passed in WorkArea " + workArea.getFilePathLocation());
        }
        String fileName = srcFile.getAbsolutePath().substring(workArea.getFilePathLocation().length());
        // before building the resource name, convert the file path to a url-like path
        String url = FilenameUtils.separatorsToUnix(fileName);
        String resourceName = buildResourceName(url);
        String destinationFilePath = FilenameUtils.normalize(getBaseDirectory(false) + File.separator + resourceName);
        File destFile = new File(destinationFilePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        try {
            if (removeFilesFromWorkArea) {
                if (destFile.exists()) {
                    FileUtils.deleteQuietly(destFile);
                }
                FileUtils.moveFile(srcFile, destFile);
            } else {
                FileUtils.copyFile(srcFile, destFile);
            }
            result.add(fileName);
        } catch (IOException ioe) {
            throw new FileServiceException("Error copying resource named " + fileName + " from workArea " + workArea.getFilePathLocation() + " to " + resourceName, ioe);
        }
    }
    return result;
}
Also used : FileServiceException(org.broadleafcommerce.common.file.FileServiceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)2 FileServiceException (org.broadleafcommerce.common.file.FileServiceException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FileWorkArea (org.broadleafcommerce.common.file.domain.FileWorkArea)1