View Javadoc
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  
36  import org.apache.commons.fileupload.FileItem;
37  
38  import fr.paris.lutece.portal.business.file.File;
39  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
40  import fr.paris.lutece.portal.service.file.FileService;
41  import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
42  import fr.paris.lutece.portal.service.image.ImageResource;
43  import fr.paris.lutece.portal.service.image.ImageResourceManager;
44  import fr.paris.lutece.portal.service.image.ImageResourceProvider;
45  import fr.paris.lutece.portal.service.init.LuteceInitException;
46  import fr.paris.lutece.util.file.FileUtil;
47  
48  public class FileImagePublicService extends AbstractCacheableService implements ImageResourceProvider 
49  {
50  	private static FileImagePublicServiceileImagePublicService.html#FileImagePublicService">FileImagePublicService _singleton = new FileImagePublicService( );
51  	public static final String IMAGE_RESOURCE_TYPE_ID = "public_image_resource";
52  	private static final IFileStoreServiceProvider _fileStoreService = FileService.getInstance( ).getFileStoreServiceProvider( );
53  	
54  	/**
55       * Creates a new instance of FileImgService
56       */
57      private FileImagePublicService( )
58      {
59      	initCache();
60      }
61      
62      /**
63       * Init
64       *
65       * @throws LuteceInitException
66       *             if an error occurs
67       */
68      public static synchronized void init( )
69      {
70          getInstance( ).register( );
71      }
72      
73      /**
74       * Initializes the service
75       */
76      public void register( )
77      {
78          ImageResourceManager.registerProvider( this );
79      }
80  
81      /**
82       * Get the unique instance of the service
83       *
84       * @return The unique instance
85       */
86      public static FileImagePublicService getInstance( )
87      {
88          return _singleton;
89      }
90  
91  	@Override
92  	/**
93       * Return the Resource Type id
94       *
95       * @return The Resource Type Id
96       */
97      public String getResourceTypeId( )
98      {
99          return IMAGE_RESOURCE_TYPE_ID;
100     }
101 
102 	@Override
103 	public ImageResource getImageResource( int nIdResource )
104 	{
105         /*get from cache*/
106 		String cacheKey = getCacheKey( nIdResource );
107 		ImageResource imageresource = (ImageResource) getFromCache( cacheKey );
108 		if ( imageresource != null )
109 		{
110 			return imageresource;
111 		}
112 		else
113 		{
114 			/*if no cache*/
115 	        File file = _fileStoreService.getFile( String.valueOf( nIdResource ) );
116 	
117 	        if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
118 	        {
119 	        	ImageResource imageResource = new ImageResource( file );
120 	        	putInCache( cacheKey, imageResource);
121 	            return imageResource;
122 	        }
123 		}
124 
125         return null;
126 	}
127 	
128 	/**
129      * Return the Resource id
130      *
131      * @param iImageResource
132      *            The resource identifier
133      * @return The New Resource Id
134      */
135 	public String addImageResource( FileItem fileItem )
136     {
137         return _fileStoreService.storeFileItem( fileItem ) ;
138     }
139 
140 	@Override
141 	public String getName( )
142 	{
143 		return IMAGE_RESOURCE_TYPE_ID;
144 	}
145 	
146 	/**
147      * get the cache key
148      * 
149      * @param nId
150      * @param user
151      * @return the key
152      */
153     private static String getCacheKey( int nId )
154     {
155         StringBuilder sbKey = new StringBuilder( );
156         return sbKey.append( "[" ).append( IMAGE_RESOURCE_TYPE_ID ).append( ":" ).append( nId )
157         		.append( "]" ).toString( );
158     }
159 }