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