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());
}
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);
}
}
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);
}
}
}
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);
}
}
}
Aggregations