コード例 #1
0
 protected File lookupAssetAndCreateLocalFile(StaticAsset staticAsset, File baseLocalFile)
     throws IOException, SQLException {
   if (StorageType.FILESYSTEM.equals(staticAsset.getStorageType())) {
     return broadleafFileService.getResource(staticAsset.getFullUrl());
   } else {
     StaticAssetStorage storage = readStaticAssetStorageByStaticAssetId(staticAsset.getId());
     if (storage != null) {
       InputStream is = storage.getFileData().getBinaryStream();
       createLocalFileFromInputStream(is, baseLocalFile);
     }
   }
   return baseLocalFile;
 }
コード例 #2
0
 @Override
 public void delete(StaticAssetStorage assetStorage) {
   if (!em.contains(assetStorage)) {
     assetStorage = (StaticAssetStorage) readStaticAssetStorageById(assetStorage.getId());
   }
   em.remove(assetStorage);
 }
コード例 #3
0
  @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);
      }
    }
  }