1 31 32 package org.opencms.file.types; 33 34 import org.opencms.configuration.CmsConfigurationException; 35 import org.opencms.db.CmsSecurityManager; 36 import org.opencms.file.CmsFile; 37 import org.opencms.file.CmsObject; 38 import org.opencms.file.CmsProperty; 39 import org.opencms.file.CmsPropertyDefinition; 40 import org.opencms.file.CmsResource; 41 import org.opencms.file.CmsResourceFilter; 42 import org.opencms.file.CmsVfsException; 43 import org.opencms.loader.CmsDumpLoader; 44 import org.opencms.loader.CmsImageLoader; 45 import org.opencms.loader.CmsImageScaler; 46 import org.opencms.main.CmsException; 47 import org.opencms.main.CmsLog; 48 import org.opencms.main.OpenCms; 49 import org.opencms.security.CmsPermissionSet; 50 import org.opencms.security.CmsSecurityException; 51 import org.opencms.util.CmsStringUtil; 52 53 import java.util.ArrayList ; 54 import java.util.List ; 55 56 import org.apache.commons.logging.Log; 57 58 67 public class CmsResourceTypeImage extends A_CmsResourceType { 68 69 72 protected class CmsImageAdjuster { 73 74 75 private byte[] m_content; 76 77 78 private CmsImageScaler m_imageDownScaler; 79 80 81 private List m_properties; 82 83 84 private String m_rootPath; 85 86 94 public CmsImageAdjuster(byte[] content, String rootPath, List properties, CmsImageScaler downScaler) { 95 96 m_content = content; 97 m_rootPath = rootPath; 98 m_properties = properties; 99 m_imageDownScaler = downScaler; 100 } 101 102 110 public void adjust() { 111 112 CmsImageScaler scaler = new CmsImageScaler(getContent(), getRootPath()); 113 if (!scaler.isValid()) { 114 return; 116 } 117 118 if (scaler.isDownScaleRequired(m_imageDownScaler)) { 120 CmsImageScaler downScaler = scaler.getDownScaler(m_imageDownScaler); 122 m_content = downScaler.scaleImage(m_content, m_rootPath); 124 scaler.setHeight(downScaler.getHeight()); 126 scaler.setWidth(downScaler.getWidth()); 127 } 128 129 CmsProperty p = new CmsProperty(CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, null, scaler.toString()); 130 List result = new ArrayList (); 132 if ((m_properties != null) && (m_properties.size() > 0)) { 133 result.addAll(m_properties); 134 result.remove(p); 135 } 136 result.add(p); 138 m_properties = result; 140 } 141 142 147 public byte[] getContent() { 148 149 return m_content; 150 } 151 152 157 public List getProperties() { 158 159 return m_properties; 160 } 161 162 167 public String getRootPath() { 168 169 return m_rootPath; 170 } 171 } 172 173 174 public static final Log LOG = CmsLog.getLog(CmsResourceTypeImage.class); 175 176 180 public static final String PROPERTY_VALUE_UNLIMITED = "unlimited"; 181 182 183 private static CmsImageScaler m_downScaler; 184 185 186 private static boolean m_staticFrozen; 187 188 189 private static int m_staticLoaderId; 190 191 192 private static int m_staticTypeId; 193 194 195 private static final int RESOURCE_TYPE_ID = 3; 196 197 198 private static final String RESOURCE_TYPE_NAME = "image"; 199 200 203 public CmsResourceTypeImage() { 204 205 super(); 206 m_typeId = RESOURCE_TYPE_ID; 207 m_typeName = RESOURCE_TYPE_NAME; 208 } 209 210 223 public static CmsImageScaler getDownScaler(CmsObject cms, String rootPath) { 224 225 if (m_downScaler == null) { 226 return null; 228 } 229 String parentFolder = CmsResource.getParentFolder(rootPath); 231 parentFolder = cms.getRequestContext().removeSiteRoot(parentFolder); 232 try { 233 CmsProperty fileSizeProperty = cms.readPropertyObject( 234 parentFolder, 235 CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, 236 false); 237 if (!fileSizeProperty.isNullProperty()) { 238 String value = fileSizeProperty.getValue().trim(); 240 if (CmsStringUtil.isNotEmpty(value)) { 241 if (PROPERTY_VALUE_UNLIMITED.equals(value)) { 242 return null; 244 } else { 245 CmsImageScaler scaler = new CmsImageScaler(value); 246 if (scaler.isValid()) { 247 return scaler; 249 } 250 } 251 } 252 } 253 } catch (CmsException e) { 254 } 256 return (CmsImageScaler)m_downScaler.clone(); 257 } 258 259 264 public static int getStaticTypeId() { 265 266 return m_staticTypeId; 267 } 268 269 274 public static String getStaticTypeName() { 275 276 return RESOURCE_TYPE_NAME; 277 } 278 279 282 public CmsResource createResource( 283 CmsObject cms, 284 CmsSecurityManager securityManager, 285 String resourcename, 286 byte[] content, 287 List properties) throws CmsException { 288 289 if (CmsImageLoader.isEnabled()) { 290 String rootPath = cms.getRequestContext().addSiteRoot(resourcename); 291 CmsImageScaler downScaler = getDownScaler(cms, rootPath); 293 CmsImageAdjuster adjuster = new CmsImageAdjuster(content, rootPath, properties, downScaler); 295 adjuster.adjust(); 297 content = adjuster.getContent(); 299 properties = adjuster.getProperties(); 300 } 301 return super.createResource(cms, securityManager, resourcename, content, properties); 302 } 303 304 307 public int getLoaderId() { 308 309 return m_staticLoaderId; 310 } 311 312 315 public CmsResource importResource( 316 CmsObject cms, 317 CmsSecurityManager securityManager, 318 String resourcename, 319 CmsResource resource, 320 byte[] content, 321 List properties) throws CmsException { 322 323 if (CmsImageLoader.isEnabled()) { 324 if (content != null) { 326 CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath()); 328 CmsImageAdjuster adjuster = new CmsImageAdjuster( 330 content, 331 resource.getRootPath(), 332 properties, 333 downScaler); 334 adjuster.adjust(); 336 content = adjuster.getContent(); 338 properties = adjuster.getProperties(); 339 } 340 } 341 return super.importResource(cms, securityManager, resourcename, resource, content, properties); 342 } 343 344 347 public void initConfiguration(String name, String id, String className) throws CmsConfigurationException { 348 349 if ((OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) && m_staticFrozen) { 350 throw new CmsConfigurationException(Messages.get().container( 352 Messages.ERR_CONFIG_FROZEN_3, 353 this.getClass().getName(), 354 getStaticTypeName(), 355 new Integer (getStaticTypeId()))); 356 } 357 358 if (!RESOURCE_TYPE_NAME.equals(name)) { 359 throw new CmsConfigurationException(Messages.get().container( 361 Messages.ERR_INVALID_RESTYPE_CONFIG_NAME_3, 362 this.getClass().getName(), 363 RESOURCE_TYPE_NAME, 364 name)); 365 } 366 367 m_staticFrozen = true; 369 370 super.initConfiguration(RESOURCE_TYPE_NAME, id, className); 371 m_staticTypeId = m_typeId; 373 374 if (CmsImageLoader.isEnabled()) { 375 m_staticLoaderId = CmsImageLoader.RESOURCE_LOADER_ID_IMAGE_LOADER; 377 String downScaleParams = CmsImageLoader.getDownScaleParams(); 379 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(downScaleParams)) { 380 m_downScaler = new CmsImageScaler(downScaleParams); 381 if (!m_downScaler.isValid()) { 382 m_downScaler = null; 384 } 385 } 386 } else { 387 m_staticLoaderId = CmsDumpLoader.RESOURCE_LOADER_ID; 389 m_downScaler = null; 391 } 392 } 393 394 397 public void replaceResource( 398 CmsObject cms, 399 CmsSecurityManager securityManager, 400 CmsResource resource, 401 int type, 402 byte[] content, 403 List properties) throws CmsException { 404 405 if (CmsImageLoader.isEnabled()) { 406 securityManager.checkPermissions( 409 cms.getRequestContext(), 410 resource, 411 CmsPermissionSet.ACCESS_WRITE, 412 true, 413 CmsResourceFilter.ALL); 414 415 CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath()); 417 CmsImageAdjuster adjuster = new CmsImageAdjuster(content, resource.getRootPath(), properties, downScaler); 419 adjuster.adjust(); 421 content = adjuster.getContent(); 423 if (adjuster.getProperties() != null) { 424 writePropertyObjects(cms, securityManager, resource, adjuster.getProperties()); 426 } 427 } 428 super.replaceResource(cms, securityManager, resource, type, content, properties); 429 } 430 431 434 public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) 435 throws CmsException, CmsVfsException, CmsSecurityException { 436 437 if (CmsImageLoader.isEnabled()) { 438 securityManager.checkPermissions( 441 cms.getRequestContext(), 442 resource, 443 CmsPermissionSet.ACCESS_WRITE, 444 true, 445 CmsResourceFilter.ALL); 446 447 CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath()); 449 CmsImageAdjuster adjuster = new CmsImageAdjuster( 451 resource.getContents(), 452 resource.getRootPath(), 453 null, 454 downScaler); 455 adjuster.adjust(); 457 resource.setContents(adjuster.getContent()); 459 if (adjuster.getProperties() != null) { 460 writePropertyObjects(cms, securityManager, resource, adjuster.getProperties()); 462 } 463 } 464 return super.writeFile(cms, securityManager, resource); 465 } 466 } | Popular Tags |