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