001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.loader; 029 030import org.opencms.ade.galleries.CmsPreviewService; 031import org.opencms.cache.CmsVfsNameBasedDiskCache; 032import org.opencms.configuration.CmsParameterConfiguration; 033import org.opencms.file.CmsFile; 034import org.opencms.file.CmsObject; 035import org.opencms.file.CmsResource; 036import org.opencms.main.CmsEvent; 037import org.opencms.main.CmsException; 038import org.opencms.main.CmsLog; 039import org.opencms.main.I_CmsEventListener; 040import org.opencms.main.OpenCms; 041import org.opencms.scheduler.jobs.CmsImageCacheCleanupJob; 042import org.opencms.util.CmsStringUtil; 043 044import java.io.IOException; 045import java.util.Map; 046 047import javax.servlet.http.HttpServletRequest; 048import javax.servlet.http.HttpServletResponse; 049 050import org.apache.commons.logging.Log; 051 052/** 053 * Loader for images from the OpenCms VSF with integrated image scaling and processing capabilities.<p> 054 * 055 * To scale or process an image, the parameter <code>{@link org.opencms.loader.CmsImageScaler#PARAM_SCALE}</code> 056 * has to be appended to the image URI. The value for the parameter needs to be composed from the <code>SCALE_PARAM</code> 057 * options provided by the constants in the <code>{@link org.opencms.file.types.CmsResourceTypeImage}</code> class.<p> 058 * 059 * For example, to scale an image to exact 800x600 pixel with center fitting and a background color of grey, 060 * the following parameter String can be used: <code>w:800,h:600,t:0,c:c0c0c0</code>.<p> 061 * 062 * @since 6.2.0 063 */ 064public class CmsImageLoader extends CmsDumpLoader implements I_CmsEventListener { 065 066 /** Controls max number of threads that are allowed to scale images concurrently. */ 067 public static final String CONFIGURATION_CONCURRENCY = "image.scaling.concurrency"; 068 069 /** The configuration parameter for the OpenCms XML configuration to set the image down scale operation. */ 070 public static final String CONFIGURATION_DOWNSCALE = "image.scaling.downscale"; 071 072 /** The configuration parameter for the OpenCms XML configuration to set the image cache repository. */ 073 public static final String CONFIGURATION_IMAGE_FOLDER = "image.folder"; 074 075 /** The configuration parameter for the OpenCms XML configuration to set the maximum image blur size. */ 076 public static final String CONFIGURATION_MAX_BLUR_SIZE = "image.scaling.maxblursize"; 077 078 /** The configuration parameter for the OpenCms XML configuration to set the maximum image scale size. */ 079 public static final String CONFIGURATION_MAX_SCALE_SIZE = "image.scaling.maxsize"; 080 081 /** The configuration parameter for the OpenCms XML configuration to enable the image scaling. */ 082 public static final String CONFIGURATION_SCALING_ENABLED = "image.scaling.enabled"; 083 084 /** Default name for the image cache repository. */ 085 public static final String IMAGE_REPOSITORY_DEFAULT = "/WEB-INF/imagecache/"; 086 087 /** Clear event parameter. */ 088 public static final String PARAM_CLEAR_IMAGES_CACHE = "_IMAGES_CACHE_"; 089 090 /** The id of this loader. */ 091 public static final int RESOURCE_LOADER_ID_IMAGE_LOADER = 2; 092 093 /** The log object for this class. */ 094 protected static final Log LOG = CmsLog.getLog(CmsImageLoader.class); 095 096 /** The (optional) image down scale parameters for image write operations. */ 097 protected static String m_downScaleParams; 098 099 /** Indicates if image scaling is active. */ 100 protected static boolean m_enabled; 101 102 /** The maximum image size (width * height) to apply image blurring when down scaling (setting this to high may cause "out of memory" errors). */ 103 protected static int m_maxBlurSize = CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE; 104 105 /** The disk cache to use for saving scaled image versions. */ 106 protected static CmsVfsNameBasedDiskCache m_vfsDiskCache; 107 108 /** The name of the configured image cache repository. */ 109 protected String m_imageRepositoryFolder; 110 111 /** The maximum image size (width or height) to allow when up scaling an image using request parameters. */ 112 protected int m_maxScaleSize = CmsImageScaler.SCALE_DEFAULT_MAX_SIZE; 113 114 /** 115 * Creates a new image loader.<p> 116 */ 117 public CmsImageLoader() { 118 119 super(); 120 } 121 122 /** 123 * Returns the image down scale parameters, 124 * which is set with the {@link #CONFIGURATION_DOWNSCALE} configuration option.<p> 125 * 126 * If no down scale parameters have been set in the configuration, this will return <code>null</code>. 127 * 128 * @return the image down scale parameters 129 */ 130 public static String getDownScaleParams() { 131 132 return m_downScaleParams; 133 } 134 135 /** 136 * Returns the path of the image cache repository folder in the RFS, 137 * which is set with the {@link #CONFIGURATION_IMAGE_FOLDER} configuration option.<p> 138 * 139 * @return the path of the image cache repository folder in the RFS 140 */ 141 public static String getImageRepositoryPath() { 142 143 return m_vfsDiskCache.getRepositoryPath(); 144 } 145 146 /** 147 * The maximum blur size for image re-scale operations, 148 * which is set with the {@link #CONFIGURATION_MAX_BLUR_SIZE} configuration option.<p> 149 * 150 * The default is 2500 * 2500 pixel.<p> 151 * 152 * @return the maximum blur size for image re-scale operations 153 */ 154 public static int getMaxBlurSize() { 155 156 return m_maxBlurSize; 157 } 158 159 /** 160 * Returns <code>true</code> if the image scaling and processing capabilities for the 161 * OpenCms VFS images have been enabled, <code>false</code> if not.<p> 162 * 163 * Image scaling is enabled by setting the loader parameter <code>image.scaling.enabled</code> 164 * to the value <code>true</code> in the configuration file <code>opencms-vfs.xml</code>.<p> 165 * 166 * Enabling image processing in OpenCms may require several additional configuration steps 167 * on the server running OpenCms, especially in UNIX systems. Here it is often required to have an X window server 168 * configured and accessible so that the required Java ImageIO operations work. 169 * Therefore the image scaling capabilities in OpenCms are disabled by default.<p> 170 * 171 * @return <code>true</code> if the image scaling and processing capabilities for the 172 * OpenCms VFS images have been enabled 173 */ 174 public static boolean isEnabled() { 175 176 return m_enabled; 177 } 178 179 /** 180 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String) 181 */ 182 @Override 183 public void addConfigurationParameter(String paramName, String paramValue) { 184 185 if (CmsStringUtil.isNotEmpty(paramName) && CmsStringUtil.isNotEmpty(paramValue)) { 186 if (CONFIGURATION_SCALING_ENABLED.equals(paramName)) { 187 m_enabled = Boolean.valueOf(paramValue).booleanValue(); 188 } 189 if (CONFIGURATION_IMAGE_FOLDER.equals(paramName)) { 190 m_imageRepositoryFolder = paramValue.trim(); 191 } 192 if (CONFIGURATION_MAX_SCALE_SIZE.equals(paramName)) { 193 m_maxScaleSize = CmsStringUtil.getIntValue( 194 paramValue, 195 CmsImageScaler.SCALE_DEFAULT_MAX_SIZE, 196 paramName); 197 } 198 if (CONFIGURATION_MAX_BLUR_SIZE.equals(paramName)) { 199 m_maxBlurSize = CmsStringUtil.getIntValue( 200 paramValue, 201 CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE, 202 paramName); 203 } 204 if (CONFIGURATION_DOWNSCALE.equals(paramName)) { 205 m_downScaleParams = paramValue.trim(); 206 } 207 208 if (CONFIGURATION_CONCURRENCY.equals(paramName)) { 209 int concurrency = CmsStringUtil.getIntValue(paramValue, CmsImageScaler.DEFAULT_CONCURRENCY, paramName); 210 if (concurrency > 0) { 211 CmsImageScaler.setConcurrency(concurrency); 212 } 213 } 214 } 215 super.addConfigurationParameter(paramName, paramValue); 216 } 217 218 /** 219 * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent) 220 */ 221 public void cmsEvent(CmsEvent event) { 222 223 if (event == null) { 224 return; 225 } 226 // only react on the clear caches event 227 int type = event.getType(); 228 if (type != I_CmsEventListener.EVENT_CLEAR_CACHES) { 229 return; 230 } 231 // only react if the clear images cache parameter is set 232 Map<String, ?> data = event.getData(); 233 if (data == null) { 234 return; 235 } 236 Object param = data.get(PARAM_CLEAR_IMAGES_CACHE); 237 if (param == null) { 238 return; 239 } 240 float age = -1; 241 if (param instanceof String) { 242 age = Float.valueOf((String)param).floatValue(); 243 } else if (param instanceof Number) { 244 age = ((Number)param).floatValue(); 245 } 246 CmsImageCacheCleanupJob.cleanImageCache(age); 247 } 248 249 /** 250 * @see org.opencms.loader.I_CmsResourceLoader#destroy() 251 */ 252 @Override 253 public void destroy() { 254 255 m_enabled = false; 256 m_imageRepositoryFolder = null; 257 m_vfsDiskCache = null; 258 } 259 260 /** 261 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration() 262 */ 263 @Override 264 public CmsParameterConfiguration getConfiguration() { 265 266 CmsParameterConfiguration result = new CmsParameterConfiguration(); 267 CmsParameterConfiguration config = super.getConfiguration(); 268 if (config != null) { 269 result.putAll(config); 270 } 271 result.put(CONFIGURATION_SCALING_ENABLED, String.valueOf(m_enabled)); 272 result.put(CONFIGURATION_IMAGE_FOLDER, m_imageRepositoryFolder); 273 return result; 274 } 275 276 /** 277 * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId() 278 */ 279 @Override 280 public int getLoaderId() { 281 282 return RESOURCE_LOADER_ID_IMAGE_LOADER; 283 } 284 285 /** 286 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 287 */ 288 @Override 289 public void initConfiguration() { 290 291 if (CmsStringUtil.isEmpty(m_imageRepositoryFolder)) { 292 m_imageRepositoryFolder = IMAGE_REPOSITORY_DEFAULT; 293 } 294 // initialize the image cache 295 if (m_vfsDiskCache == null) { 296 m_vfsDiskCache = new CmsVfsNameBasedDiskCache( 297 OpenCms.getSystemInfo().getWebApplicationRfsPath(), 298 m_imageRepositoryFolder); 299 } 300 OpenCms.addCmsEventListener(this); 301 // output setup information 302 if (CmsLog.INIT.isInfoEnabled()) { 303 CmsLog.INIT.info( 304 Messages.get().getBundle().key( 305 Messages.INIT_IMAGE_REPOSITORY_PATH_1, 306 m_vfsDiskCache.getRepositoryPath())); 307 CmsLog.INIT.info( 308 Messages.get().getBundle().key(Messages.INIT_IMAGE_SCALING_ENABLED_1, Boolean.valueOf(m_enabled))); 309 } 310 } 311 312 /** 313 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 314 */ 315 @Override 316 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 317 throws IOException, CmsException { 318 319 if (m_enabled) { 320 if (canSendLastModifiedHeader(resource, req, res)) { 321 // no image processing required at all 322 return; 323 } 324 // get the scale information from the request 325 CmsImageScaler scaler = new CmsImageScaler(req, m_maxScaleSize, m_maxBlurSize); 326 // load the file from the cache 327 CmsFile file = getScaledImage(cms, resource, scaler); 328 // now perform standard load operation inherited from dump loader 329 super.load(cms, file, req, res); 330 } else { 331 // scaling is disabled 332 super.load(cms, resource, req, res); 333 } 334 } 335 336 /** 337 * Returns a scaled version of the given OpenCms VFS image resource.<p> 338 * 339 * All results are cached in disk. 340 * If the scaled version does not exist in the cache, it is created. 341 * Unscaled versions of the images are also stored in the cache.<p> 342 * 343 * @param cms the current users OpenCms context 344 * @param resource the base VFS resource for the image 345 * @param scaler the configured image scaler 346 * 347 * @return a scaled version of the given OpenCms VFS image resource 348 * 349 * @throws IOException in case of errors accessing the disk based cache 350 * @throws CmsException in case of errors accessing the OpenCms VFS 351 */ 352 protected CmsFile getScaledImage(CmsObject cms, CmsResource resource, CmsImageScaler scaler) 353 throws IOException, CmsException { 354 355 String cacheParam = scaler.isValid() ? scaler.toString() : null; 356 String cacheName = m_vfsDiskCache.getCacheName(resource, cacheParam); 357 byte[] content = m_vfsDiskCache.getCacheContent(cacheName); 358 359 CmsFile file; 360 if (content != null) { 361 if (resource instanceof CmsFile) { 362 // the original file content must be modified (required e.g. for static export) 363 file = (CmsFile)resource; 364 } else { 365 // this is no file, but we don't want to use "upgrade" since we don't need to read the content from the VFS 366 file = new CmsFile(resource); 367 } 368 // save the content in the file 369 file.setContents(content); 370 } else { 371 // we must read the content from the VFS (if this has not been done yet) 372 file = cms.readFile(resource); 373 // upgrade the file (load the content) 374 if (scaler.isValid()) { 375 if (scaler.getType() == 8) { 376 // only need the focal point for mode 8 377 scaler.setFocalPoint(CmsPreviewService.readFocalPoint(cms, resource)); 378 } 379 // valid scaling parameters found, scale the content 380 content = scaler.scaleImage(file); 381 // exchange the content of the file with the scaled version 382 file.setContents(content); 383 } 384 // save the file content in the cache 385 m_vfsDiskCache.saveCacheFile(cacheName, file.getContents()); 386 } 387 return file; 388 } 389}