use of org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl in project BroadleafCommerce by BroadleafCommerce.
the class StaticAssetServiceImpl method createStaticAsset.
@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public StaticAsset createStaticAsset(InputStream inputStream, String fileName, long fileSize, Map<String, String> properties) {
if (properties == null) {
properties = new HashMap<String, String>();
}
String fullUrl = buildAssetURL(properties, fileName);
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(fullUrl);
ExtensionResultStatusType resultStatusType = staticAssetExtensionManager.getProxy().modifyDuplicateAssetURL(urlBuilder);
fullUrl = urlBuilder.toString();
StaticAsset newAsset = staticAssetDao.readStaticAssetByFullUrl(fullUrl);
// logic for handling duplicate files.
if (resultStatusType != ExtensionResultStatusType.HANDLED) {
int count = 0;
while (newAsset != null) {
count++;
// try the new format first, then the old
newAsset = staticAssetDao.readStaticAssetByFullUrl(getCountUrl(fullUrl, count, false));
if (newAsset == null) {
newAsset = staticAssetDao.readStaticAssetByFullUrl(getCountUrl(fullUrl, count, true));
}
}
if (count > 0) {
fullUrl = getCountUrl(fullUrl, count, false);
}
}
try {
ImageMetadata metadata = imageArtifactProcessor.getImageMetadata(inputStream);
newAsset = new ImageStaticAssetImpl();
((ImageStaticAsset) newAsset).setWidth(metadata.getWidth());
((ImageStaticAsset) newAsset).setHeight(metadata.getHeight());
} catch (Exception e) {
// must not be an image stream
LOG.warn("unable to convert asset:" + fileName + " into Image");
LOG.debug(e);
if (getShouldAcceptNonImageAsset()) {
newAsset = createNonImageAsset(inputStream, fileName, properties);
} else {
throw new RuntimeException("Selected Asset/File was not valid image.");
}
}
if (storeAssetsOnFileSystem) {
newAsset.setStorageType(StorageType.FILESYSTEM);
} else {
newAsset.setStorageType(StorageType.DATABASE);
}
newAsset.setName(fileName);
getMimeType(inputStream, fileName, newAsset);
newAsset.setFileExtension(getFileExtension(fileName));
newAsset.setFileSize(fileSize);
newAsset.setFullUrl(fullUrl);
return staticAssetDao.addOrUpdateStaticAsset(newAsset, false);
}
use of org.broadleafcommerce.cms.file.domain.ImageStaticAssetImpl in project BroadleafCommerce by BroadleafCommerce.
the class AdminAssetUploadController method upload.
@RequestMapping(value = "/{id}/uploadAsset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, @PathVariable(value = "sectionKey") String sectionKey, @PathVariable(value = "id") String id) throws IOException {
Map<String, Object> responseMap = new HashMap<String, Object>();
Map<String, String> properties = new HashMap<String, String>();
properties.put("entityType", sectionKey);
properties.put("entityId", id);
StaticAsset staticAsset = staticAssetService.createStaticAssetFromFile(file, properties);
staticAssetStorageService.createStaticAssetStorageFromFile(file, staticAsset);
String staticAssetUrlPrefix = staticAssetService.getStaticAssetUrlPrefix();
if (staticAssetUrlPrefix != null && !staticAssetUrlPrefix.startsWith("/")) {
staticAssetUrlPrefix = "/" + staticAssetUrlPrefix;
}
String assetUrl = staticAssetUrlPrefix + staticAsset.getFullUrl();
responseMap.put("adminDisplayAssetUrl", request.getContextPath() + assetUrl);
responseMap.put("assetUrl", assetUrl);
if (staticAsset instanceof ImageStaticAssetImpl) {
responseMap.put("image", Boolean.TRUE);
responseMap.put("assetThumbnail", assetUrl + "?smallAdminThumbnail");
responseMap.put("assetLarge", assetUrl + "?largeAdminThumbnail");
} else {
responseMap.put("image", Boolean.FALSE);
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "text/html; charset=utf-8");
return new ResponseEntity<Map<String, Object>>(responseMap, responseHeaders, HttpStatus.OK);
}
Aggregations