FileImagePublicService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.service.fileimage;

  35. import org.apache.commons.fileupload.FileItem;

  36. import fr.paris.lutece.portal.business.file.File;
  37. import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
  38. import fr.paris.lutece.portal.service.file.FileService;
  39. import fr.paris.lutece.portal.service.file.FileServiceException;
  40. import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
  41. import fr.paris.lutece.portal.service.image.ImageResource;
  42. import fr.paris.lutece.portal.service.image.ImageResourceManager;
  43. import fr.paris.lutece.portal.service.image.ImageResourceProvider;
  44. import fr.paris.lutece.portal.service.init.LuteceInitException;
  45. import fr.paris.lutece.portal.service.util.AppLogService;
  46. import fr.paris.lutece.util.file.FileUtil;

  47. public class FileImagePublicService extends AbstractCacheableService implements ImageResourceProvider
  48. {
  49.     private static FileImagePublicService _singleton = new FileImagePublicService( );
  50.     public static final String IMAGE_RESOURCE_TYPE_ID = "public_image_resource";
  51.     private static final IFileStoreServiceProvider _fileStoreService = FileService.getInstance( ).getFileStoreServiceProvider( );
  52.    
  53.     /**
  54.      * Creates a new instance of FileImgService
  55.      */
  56.     private FileImagePublicService( )
  57.     {
  58.         initCache();
  59.     }
  60.    
  61.     /**
  62.      * Init
  63.      *
  64.      * @throws LuteceInitException
  65.      *             if an error occurs
  66.      */
  67.     public static synchronized void init( )
  68.     {
  69.         getInstance( ).register( );
  70.     }
  71.    
  72.     /**
  73.      * Initializes the service
  74.      */
  75.     public void register( )
  76.     {
  77.         ImageResourceManager.registerProvider( this );
  78.     }

  79.     /**
  80.      * Get the unique instance of the service
  81.      *
  82.      * @return The unique instance
  83.      */
  84.     public static FileImagePublicService getInstance( )
  85.     {
  86.         return _singleton;
  87.     }

  88.     @Override
  89.     /**
  90.      * Return the Resource Type id
  91.      *
  92.      * @return The Resource Type Id
  93.      */
  94.     public String getResourceTypeId( )
  95.     {
  96.         return IMAGE_RESOURCE_TYPE_ID;
  97.     }

  98.     @Override
  99.     public ImageResource getImageResource( int nIdResource )
  100.     {
  101.         /*get from cache*/
  102.         String cacheKey = getCacheKey( nIdResource );
  103.         ImageResource imageresource = (ImageResource) getFromCache( cacheKey );
  104.         if ( imageresource != null )
  105.         {
  106.             return imageresource;
  107.         }
  108.         else
  109.         {
  110.             /*if no cache*/
  111.             try
  112.             {
  113.                 File file = _fileStoreService.getFile( String.valueOf( nIdResource ) );
  114.                
  115.                 if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
  116.                 {
  117.                     ImageResource imageResource = new ImageResource( file );
  118.                     putInCache( cacheKey, imageResource);
  119.                     return imageResource;
  120.                 }
  121.             }
  122.             catch ( FileServiceException e )
  123.             {
  124.                 AppLogService.error(e);
  125.             }
  126.         }

  127.         return null;
  128.     }
  129.    
  130.     /**
  131.      * Return the Resource id
  132.      *
  133.      * @param iImageResource
  134.      *            The resource identifier
  135.      * @return The New Resource Id
  136.      */
  137.     public String addImageResource( FileItem fileItem )
  138.     {
  139.         try
  140.         {
  141.             return _fileStoreService.storeFileItem( fileItem ) ;
  142.         }
  143.         catch ( FileServiceException e )
  144.         {
  145.             AppLogService.error(e);
  146.             return null;
  147.         }
  148.     }

  149.     @Override
  150.     public String getName( )
  151.     {
  152.         return IMAGE_RESOURCE_TYPE_ID;
  153.     }
  154.    
  155.     /**
  156.      * get the cache key
  157.      *
  158.      * @param nId
  159.      * @param user
  160.      * @return the key
  161.      */
  162.     private static String getCacheKey( int nId )
  163.     {
  164.         StringBuilder sbKey = new StringBuilder( );
  165.         return sbKey.append( "[" ).append( IMAGE_RESOURCE_TYPE_ID ).append( ":" ).append( nId )
  166.                 .append( "]" ).toString( );
  167.     }
  168. }