@Transactional("blTransactionManagerAssetStorageInfo") @Override public void createStaticAssetStorageFromFile(MultipartFile file, StaticAsset staticAsset) throws IOException { if (StorageType.DATABASE.equals(staticAsset.getStorageType())) { StaticAssetStorage storage = staticAssetStorageDao.create(); storage.setStaticAssetId(staticAsset.getId()); Blob uploadBlob = staticAssetStorageDao.createBlob(file); storage.setFileData(uploadBlob); staticAssetStorageDao.save(storage); } else if (StorageType.FILESYSTEM.equals(staticAsset.getStorageType())) { FileWorkArea tempWorkArea = broadleafFileService.initializeWorkArea(); String destFileName = tempWorkArea.getFilePathLocation() + removeLeadingSlash(staticAsset.getFullUrl()); InputStream input = file.getInputStream(); byte[] buffer = new byte[fileBufferSize]; File destFile = new File(destFileName); if (!destFile.getParentFile().exists()) { if (!destFile.getParentFile().mkdirs()) { throw new RuntimeException( "Unable to create parent directories for file: " + destFileName); } } OutputStream output = new FileOutputStream(destFile); boolean deleteFile = false; try { int bytesRead; int totalBytesRead = 0; while ((bytesRead = input.read(buffer)) != -1) { totalBytesRead += bytesRead; if (totalBytesRead > maxUploadableFileSize) { deleteFile = true; throw new IOException("Maximum Upload File Size Exceeded"); } output.write(buffer, 0, bytesRead); } broadleafFileService.addOrUpdateResource(tempWorkArea, destFile, true); } finally { output.close(); broadleafFileService.closeWorkArea(tempWorkArea); } } }
@Transactional("blTransactionManagerAssetStorageInfo") @Override public StaticAssetStorage save(StaticAssetStorage assetStorage) { return staticAssetStorageDao.save(assetStorage); }