View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.plugins.genericattributes.service.file;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.function.Function;
40  
41  import org.apache.commons.fileupload.FileItem;
42  import org.apache.commons.lang3.StringUtils;
43  
44  import fr.paris.lutece.portal.business.file.File;
45  import fr.paris.lutece.portal.service.file.FileService;
46  import fr.paris.lutece.portal.service.file.FileServiceException;
47  import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  
51  public class GenericAttributeFileService
52  {
53  	private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
54  	private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";
55  
56      private static final GenericAttributeFileServiceervice/file/GenericAttributeFileService.html#GenericAttributeFileService">GenericAttributeFileService _instance = new GenericAttributeFileService( );
57      private static final Map<String,String> _entryTypeFileServices = new HashMap<>();
58  	
59      /**
60       * Constructor
61       */
62      private GenericAttributeFileService( )
63      {
64      	List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);
65      	
66      	// init specific entryType fileStoreService names if exists
67      	keyList.stream().forEach( s -> { 
68      		if ( !StringUtils.isAllBlank(  AppPropertiesService.getProperty(s) ) )
69      		{
70      			_entryTypeFileServices.put(s, AppPropertiesService.getProperty(s)); 
71      		}
72      	} );
73      }
74      
75      /**
76       * get FileStoreServiceProvider name for entry type :
77       * - returns the entry type FileService (if set)
78       * - otherwise, returns the GenAttr default FileService (if set)
79       * - otherwise, returns the lutece default FileService 
80       * 
81       * @param strEntryType
82       * @return the name of the FileStoreServiceProvider
83       */
84      public String getFileStoreProviderName( String strEntryType )
85      {
86      	if (strEntryType != null ) 
87      	{
88      		if ( _entryTypeFileServices.containsKey( strEntryType ))
89      		{
90      			return _entryTypeFileServices.get( strEntryType );
91      		}
92      	}
93      	
94      	if ( _entryTypeFileServices.containsKey( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
95  		{
96  			return _entryTypeFileServices.get( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
97  		}
98      	
99      	return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
100     }
101     
102     /**
103      * get FileStoreServiceProvider name  :
104      * - returns the GenAttr default FileService (if set)
105      * - otherwise, returns the lutece default FileService 
106      * 
107      * Use getFileStoreProviderName( String strEntryType ) to get entryType specific File service
108      * 
109      * @return the name of the FileStoreServiceProvider
110      */
111     public String getFileStoreProviderName(  )
112     {
113     	return getFileStoreProviderName( null );
114     }
115 
116     /**
117      * get instance of service
118      * 
119      * @return the instance
120      */
121     public static GenericAttributeFileService getInstance( )
122     {
123         return _instance;
124     }
125 
126     /**
127      * Save a file
128      * 
129      * @param file The lutece file in default generic file Service
130      * @return The key of the file
131      */
132     public String save( File file)
133     {
134     	return save( file, null);
135     }
136     /**
137      * Save a file
138      * 
139      * @param file The lutece file
140      * @param strEntryType the entry type
141      * @return The key of the file
142      */
143     public String save( File file, String strEntryType)
144     {
145         try
146         {
147         	if ( file.getOrigin( ) == null )
148         	{
149         		file.setOrigin( getFileStoreProviderName( strEntryType ) );
150         	}
151         	
152         	return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
153         }
154         catch( FileServiceException e )
155         {
156             AppLogService.error( e );
157             return null;
158         }
159     }
160 
161     /**
162      * Save a file
163      * 
164      * @param file The fileItem
165      * @param strEntryType the entry type
166      * @return The key of the file
167      */
168     public String save( FileItem file, String strEntryType)
169     {
170         try
171         {        	
172         	return FileService.getInstance( ).getFileStoreServiceProvider( getFileStoreProviderName( strEntryType )  ).storeFileItem( file );
173         }
174         catch( FileServiceException e )
175         {
176             AppLogService.error( e );
177             return null;
178         }
179     }
180     
181     /**
182      * Load a file
183      * 
184      * @param strKey
185      *            The key of the file
186      * @return The file
187      */
188     public File load( String strKey, String strOrigin )
189     {
190         try
191         {
192         	if ( StringUtils.isEmpty( strOrigin ) )
193         	{
194         		strOrigin =  getFileStoreProviderName( ) ;
195         	}
196         	
197             return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
198         }
199         catch( FileServiceException e )
200         {
201             AppLogService.error( e );
202             return null;
203         }
204     }
205 
206     /**
207      * Delete a file
208      * 
209      * @param strKey
210      *            The key of the file
211      */
212     public void delete( String strKey, String strOrigin )
213     {
214         try
215         {
216         	if ( StringUtils.isEmpty( strOrigin ) )
217         	{
218         		strOrigin =  getFileStoreProviderName( );
219         	}
220         	
221             FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
222         }
223         catch( FileServiceException e )
224         {
225             AppLogService.error( e );
226         }
227     }
228 
229 }