protected File getFileFromLocalRepository(String cachedFileName) {
   // Look for a shared file (this represents a file that was based on a file originally in the
   // classpath.
   File cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
   if (cacheFile.exists()) {
     return cacheFile;
   } else {
     return broadleafFileService.getLocalResource(cachedFileName);
   }
 }
  @Transactional("blTransactionManagerAssetStorageInfo")
  @Override
  public Map<String, String> getCacheFileModel(String fullUrl, Map<String, String> parameterMap)
      throws Exception {
    StaticAsset staticAsset = findStaticAsset(fullUrl);
    if (staticAsset == null) {
      throw new AssetNotFoundException("Unable to find an asset for the url (" + fullUrl + ")");
    }
    String mimeType = staticAsset.getMimeType();

    // extract the values for any named parameters
    Map<String, String> convertedParameters =
        namedOperationManager.manageNamedParameters(parameterMap);
    String cachedFileName = constructCacheFileName(staticAsset, convertedParameters);

    // Look for a shared file (this represents a file that was based on a file originally in the
    // classpath.
    File cacheFile = getFileFromLocalRepository(cachedFileName);
    if (cacheFile.exists()) {
      return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }

    // Obtain the base file (that we may need to convert based on the parameters
    String baseCachedFileName = constructCacheFileName(staticAsset, null);
    File baseLocalFile = getFileFromLocalRepository(baseCachedFileName);

    if (!baseLocalFile.exists()) {
      if (broadleafFileService.checkForResourceOnClassPath(staticAsset.getFullUrl())) {
        cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
        baseLocalFile = broadleafFileService.getSharedLocalResource(baseCachedFileName);
        createLocalFileFromClassPathResource(staticAsset, baseLocalFile);
      } else {
        baseLocalFile = lookupAssetAndCreateLocalFile(staticAsset, baseLocalFile);
      }
    }

    if (convertedParameters.isEmpty()) {
      return buildModel(baseLocalFile.getAbsolutePath(), mimeType);
    } else {
      FileInputStream assetStream = new FileInputStream(baseLocalFile);
      BufferedInputStream original = new BufferedInputStream(assetStream);
      original.mark(0);

      Operation[] operations =
          artifactService.buildOperations(convertedParameters, original, staticAsset.getMimeType());
      InputStream converted =
          artifactService.convert(original, operations, staticAsset.getMimeType());

      createLocalFileFromInputStream(converted, cacheFile);
      if ("image/gif".equals(mimeType)) {
        mimeType = "image/png";
      }
      return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }
  }
  @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);
      }
    }
  }
  protected void createLocalFileFromInputStream(InputStream is, File baseLocalFile)
      throws IOException {
    BufferedOutputStream bos = null;
    FileWorkArea workArea = null;
    try {
      if (!baseLocalFile.getParentFile().exists()) {
        if (!baseLocalFile.getParentFile().mkdirs()) {
          throw new RuntimeException(
              "Unable to create middle directories for file: " + baseLocalFile.getAbsolutePath());
        }
      }

      workArea = broadleafFileService.initializeWorkArea();
      File tmpFile =
          new File(appendTrailingSlash(workArea.getFilePathLocation()) + baseLocalFile.getName());

      bos = new BufferedOutputStream(new FileOutputStream(tmpFile));

      boolean eof = false;
      int temp;
      while (!eof) {
        temp = is.read();
        if (temp < 0) {
          eof = true;
        } else {
          bos.write(temp);
        }
      }

      FileUtils.moveFile(tmpFile, baseLocalFile);
    } finally {
      try {
        if (bos != null) {
          bos.flush();
          bos.close();
        }
        if (workArea != null) {
          broadleafFileService.closeWorkArea(workArea);
        }
      } catch (Throwable e) {
        // do nothing
      }
    }
  }
 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;
 }
 protected void createLocalFileFromClassPathResource(StaticAsset staticAsset, File baseLocalFile)
     throws IOException {
   InputStream is = broadleafFileService.getClasspathResource(staticAsset.getFullUrl());
   createLocalFileFromInputStream(is, baseLocalFile);
 }