1 23 24 package org.infoglue.cms.applications.contenttool.actions; 25 26 import java.awt.Image ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.InputStream ; 30 import java.util.Enumeration ; 31 import java.util.List ; 32 33 import org.apache.log4j.Logger; 34 import org.infoglue.cms.applications.common.VisualFormatter; 35 import org.infoglue.cms.applications.databeans.AssetKeyDefinition; 36 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController; 37 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController; 38 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController; 39 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController; 40 import org.infoglue.cms.controllers.kernel.impl.simple.GroupPropertiesController; 41 import org.infoglue.cms.controllers.kernel.impl.simple.RolePropertiesController; 42 import org.infoglue.cms.controllers.kernel.impl.simple.UserPropertiesController; 43 import org.infoglue.cms.entities.content.ContentVersionVO; 44 import org.infoglue.cms.entities.content.DigitalAssetVO; 45 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO; 46 import org.infoglue.cms.entities.management.GroupProperties; 47 import org.infoglue.cms.entities.management.GroupPropertiesVO; 48 import org.infoglue.cms.entities.management.RoleProperties; 49 import org.infoglue.cms.entities.management.RolePropertiesVO; 50 import org.infoglue.cms.entities.management.UserProperties; 51 import org.infoglue.cms.entities.management.UserPropertiesVO; 52 import org.infoglue.cms.util.CmsPropertyHandler; 53 54 import webwork.action.ActionContext; 55 import webwork.multipart.MultiPartRequestWrapper; 56 57 58 public class CreateDigitalAssetAction extends ViewDigitalAssetAction 59 { 60 private final static Logger logger = Logger.getLogger(CreateDigitalAssetAction.class.getName()); 61 62 private static final long serialVersionUID = 1L; 63 64 private String entity; 65 private Integer entityId; 66 67 private Integer contentVersionId = null; 68 private String digitalAssetKey = ""; 69 private Integer uploadedFilesCounter = new Integer (0); 70 private ContentVersionVO contentVersionVO; 71 private ContentTypeDefinitionVO contentTypeDefinitionVO; 72 private String reasonKey; 73 private DigitalAssetVO digitalAssetVO = null; 74 private String closeOnLoad; 75 private Integer contentTypeDefinitionId; 76 77 public CreateDigitalAssetAction() 78 { 79 } 80 81 public void setContentVersionId(Integer contentVersionId) 82 { 83 this.contentVersionId = contentVersionId; 84 } 85 86 public Integer getContentVersionId() 87 { 88 return this.contentVersionId; 89 } 90 91 public void setDigitalAssetKey(String digitalAssetKey) 92 { 93 this.digitalAssetKey = digitalAssetKey; 94 } 95 96 public String getDigitalAssetKey() 97 { 98 return digitalAssetKey; 99 } 100 101 public void setUploadedFilesCounter(Integer uploadedFilesCounter) 102 { 103 this.uploadedFilesCounter = uploadedFilesCounter; 104 } 105 106 public Integer getUploadedFilesCounter() 107 { 108 return this.uploadedFilesCounter; 109 } 110 111 public List getDefinedAssetKeys() 112 { 113 return ContentTypeDefinitionController.getController().getDefinedAssetKeys(this.contentTypeDefinitionVO.getSchemaValue()); 114 } 115 116 public String doExecute() { 118 try 119 { 120 MultiPartRequestWrapper mpr = ActionContext.getMultiPartRequest(); 121 if(mpr == null) 122 { 123 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText"; 124 return "uploadFailed"; 125 } 126 127 if(this.contentVersionId != null) 128 { 129 this.contentVersionVO = ContentVersionController.getContentVersionController().getContentVersionVOWithId(this.contentVersionId); 130 this.contentTypeDefinitionVO = ContentController.getContentController().getContentTypeDefinition(contentVersionVO.getContentId()); 131 } 132 else 133 { 134 if(this.entity.equalsIgnoreCase(UserProperties.class.getName())) 135 { 136 UserPropertiesVO userPropertiesVO = UserPropertiesController.getController().getUserPropertiesVOWithId(this.entityId); 137 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(userPropertiesVO.getContentTypeDefinitionId()); 138 } 139 else if(this.entity.equalsIgnoreCase(RoleProperties.class.getName())) 140 { 141 RolePropertiesVO rolePropertiesVO = RolePropertiesController.getController().getRolePropertiesVOWithId(this.entityId); 142 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(rolePropertiesVO.getContentTypeDefinitionId()); 143 } 144 else if(this.entity.equalsIgnoreCase(GroupProperties.class.getName())) 145 { 146 GroupPropertiesVO groupPropertiesVO = GroupPropertiesController.getController().getGroupPropertiesVOWithId(this.entityId); 147 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(groupPropertiesVO.getContentTypeDefinitionId()); 148 } 149 } 150 151 152 InputStream is = null; 153 File file = null; 155 156 try 157 { 158 if(mpr != null) 159 { 160 Enumeration names = mpr.getFileNames(); 161 while (names.hasMoreElements()) 162 { 163 String name = (String )names.nextElement(); 164 String contentType = mpr.getContentType(name); 165 String fileSystemName = mpr.getFilesystemName(name); 166 167 logger.info("digitalAssetKey:" + digitalAssetKey); 168 logger.info("name:" + name); 169 logger.info("contentType:" + contentType); 170 logger.info("fileSystemName:" + fileSystemName); 171 172 file = mpr.getFile(name); 173 String fileName = fileSystemName; 175 176 fileName = new VisualFormatter().replaceNonAscii(fileName, '_'); 177 178 String tempFileName = "tmp_" + System.currentTimeMillis() + "_" + fileName; 179 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 181 fileSystemName = filePath + File.separator + tempFileName; 182 183 188 DigitalAssetVO newAsset = new DigitalAssetVO(); 189 newAsset.setAssetContentType(contentType); 190 newAsset.setAssetKey(digitalAssetKey); 191 newAsset.setAssetFileName(fileName); 192 newAsset.setAssetFilePath(filePath); 193 newAsset.setAssetFileSize(new Integer (new Long (file.length()).intValue())); 194 is = new FileInputStream (file); 196 197 String fileUploadMaximumSize = getPrincipalPropertyValue("fileUploadMaximumSize", false, true); 198 logger.info("fileUploadMaximumSize in create:" + fileUploadMaximumSize); 199 if(!fileUploadMaximumSize.equalsIgnoreCase("-1") && new Integer (fileUploadMaximumSize).intValue() < new Long (file.length()).intValue()) 200 { 201 file.delete(); 202 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText"; 203 return "uploadFailed"; 204 } 205 206 if(this.contentTypeDefinitionId != null && digitalAssetKey != null) 207 { 208 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(this.contentTypeDefinitionId); 209 AssetKeyDefinition assetKeyDefinition = ContentTypeDefinitionController.getController().getDefinedAssetKey(contentTypeDefinitionVO.getSchemaValue(), digitalAssetKey); 210 211 if(assetKeyDefinition != null) 212 { 213 if(assetKeyDefinition.getMaximumSize().intValue() < new Long (file.length()).intValue()) 214 { 215 file.delete(); 216 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnSizeText"; 217 return "uploadFailed"; 218 } 219 if(assetKeyDefinition.getAllowedContentTypes().startsWith("image")) 220 { 221 if(!contentType.startsWith("image")) 222 { 223 file.delete(); 224 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnTypeNotImageText"; 225 return "uploadFailed"; 226 } 227 228 Image image = javax.imageio.ImageIO.read(file); 229 int width = image.getWidth(null); 230 int height = image.getHeight(null); 231 232 String allowedWidth = assetKeyDefinition.getImageWidth(); 233 String allowedHeight = assetKeyDefinition.getImageHeight(); 234 235 if(!allowedWidth.equals("*")) 236 { 237 Integer allowedWidthNumber = new Integer (allowedWidth.substring(1)); 238 if(allowedWidth.startsWith("<") && width >= allowedWidthNumber.intValue()) 239 { 240 file.delete(); 241 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageToWideText"; 242 return "uploadFailed"; 243 } 244 if(allowedWidth.startsWith(">") && width <= allowedWidthNumber.intValue()) 245 { 246 file.delete(); 247 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageNotWideEnoughText"; 248 return "uploadFailed"; 249 } 250 if(!allowedWidth.startsWith(">") && !allowedWidth.startsWith("<") && width != new Integer (allowedWidth).intValue()) 251 { 252 file.delete(); 253 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageWrongWidthText"; 254 return "uploadFailed"; 255 } 256 } 257 258 if(!allowedHeight.equals("*")) 259 { 260 Integer allowedHeightNumber = new Integer (allowedHeight.substring(1)); 261 if(allowedHeight.startsWith("<") && height >= allowedHeightNumber.intValue()) 262 { 263 file.delete(); 264 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageToHighText"; 265 return "uploadFailed"; 266 } 267 if(allowedHeight.startsWith(">") && height <= allowedHeightNumber.intValue()) 268 { 269 file.delete(); 270 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageNotHighEnoughText"; 271 return "uploadFailed"; 272 } 273 if(!allowedHeight.startsWith(">") && !allowedHeight.startsWith("<") && height != new Integer (allowedHeight).intValue()) 274 { 275 file.delete(); 276 this.reasonKey = "tool.contenttool.fileUpload.fileUploadFailedOnImageWrongHeightText"; 277 return "uploadFailed"; 278 } 279 } 280 } 281 } 282 } 283 284 if(this.contentVersionId != null) 285 digitalAssetVO = DigitalAssetController.create(newAsset, is, this.contentVersionId); 286 else 287 digitalAssetVO = DigitalAssetController.create(newAsset, is, this.entity, this.entityId); 288 289 this.uploadedFilesCounter = new Integer (this.uploadedFilesCounter.intValue() + 1); 290 } 291 } 292 else 293 { 294 logger.error("File upload failed for some reason."); 295 } 296 } 297 catch (Throwable e) 298 { 299 logger.error("An error occurred when we tried to upload a new asset:" + e.getMessage(), e); 300 } 301 finally 302 { 303 try 304 { 305 is.close(); 306 file.delete(); 307 } 308 catch(Throwable e) 309 { 310 logger.error("An error occurred when we tried to close the fileinput stream and delete the file:" + e.getMessage(), e); 311 } 312 } 313 } 314 catch(Throwable e) 315 { 316 logger.error("An error occurred when we tried to upload a new asset:" + e.getMessage(), e); 317 } 318 319 return "success"; 320 } 321 322 326 327 public String getDigitalAssetUrl() throws Exception 328 { 329 String imageHref = null; 330 try 331 { 332 imageHref = DigitalAssetController.getDigitalAssetUrl(digitalAssetVO.getDigitalAssetId()); 333 } 334 catch(Exception e) 335 { 336 logger.warn("We could not get the url of the digitalAsset: " + e.getMessage(), e); 337 } 338 339 return imageHref; 340 } 341 342 public String getAssetThumbnailUrl() 343 { 344 String imageHref = null; 345 try 346 { 347 imageHref = DigitalAssetController.getDigitalAssetThumbnailUrl(digitalAssetVO.getDigitalAssetId()); 348 } 349 catch(Exception e) 350 { 351 logger.warn("We could not get the url of the thumbnail: " + e.getMessage(), e); 352 } 353 354 return imageHref; 355 } 356 357 358 public String getEntity() 359 { 360 return entity; 361 } 362 363 public void setEntity(String entity) 364 { 365 this.entity = entity; 366 } 367 368 public Integer getEntityId() 369 { 370 return entityId; 371 } 372 373 public void setEntityId(Integer entityId) 374 { 375 this.entityId = entityId; 376 } 377 378 public String getReasonKey() 379 { 380 return reasonKey; 381 } 382 383 public String getCloseOnLoad() 384 { 385 return closeOnLoad; 386 } 387 388 public void setCloseOnLoad(String closeOnLoad) 389 { 390 this.closeOnLoad = closeOnLoad; 391 } 392 393 public Integer getContentTypeDefinitionId() 394 { 395 return contentTypeDefinitionId; 396 } 397 398 public void setContentTypeDefinitionId(Integer contentTypeDefinitionId) 399 { 400 this.contentTypeDefinitionId = contentTypeDefinitionId; 401 } 402 403 public ContentTypeDefinitionVO getContentTypeDefinitionVO() 404 { 405 return contentTypeDefinitionVO; 406 } 407 } | Popular Tags |