if (result.hasErrors()) {
return display(request, response, modelMap);
}
final String currentAssetId = assetForm.getId();
final Asset asset = productMarketingService.getProductMarketingAssetById(currentAssetId);
MultipartFile multipartFile = assetForm.getFile();
if(multipartFile != null){
long size = multipartFile.getSize();
asset.setFileSize(size);
}
try {
if (multipartFile.getSize() > 0) {
String pathProductMarketingImage = multipartFile.getOriginalFilename();
String assetFileRootPath = engineSettingService.getSettingAssetFileRootPath().getDefaultValue();
assetFileRootPath.replaceAll("\\\\", "/");
if(assetFileRootPath.endsWith("/")){
assetFileRootPath = assetFileRootPath.substring(0, assetFileRootPath.length() - 1);
}
String assetProductMarketingFilePath = engineSettingService.getSettingAssetProductMarketingFilePath().getDefaultValue();
assetProductMarketingFilePath.replaceAll("\\\\", "/");
if(assetProductMarketingFilePath.endsWith("/")){
assetProductMarketingFilePath = assetProductMarketingFilePath.substring(0, assetProductMarketingFilePath.length() - 1);
}
if(!assetProductMarketingFilePath.startsWith("/")){
assetProductMarketingFilePath = "/" + assetProductMarketingFilePath;
}
String absoluteFilePath = assetFileRootPath + assetProductMarketingFilePath + "/" + asset.getType().toLowerCase() + "/" + pathProductMarketingImage;
InputStream inputStream = multipartFile.getInputStream();
URI url = new URI(absoluteFilePath);
File fileAsset;
try {
fileAsset = new File(url);
} catch(IllegalArgumentException e) {
fileAsset = new File(url.getPath());
}
OutputStream outputStream = new FileOutputStream(fileAsset);
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
asset.setPath(pathProductMarketingImage);
}
// UPDATE ASSET
webBackofficeService.createOrUpdateProductMarketingAsset(asset, assetForm);