1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
40 import org.apache.commons.fileupload.FileItem;
41 import org.apache.commons.lang3.StringUtils;
42
43 import fr.paris.lutece.portal.business.file.File;
44 import fr.paris.lutece.portal.service.file.FileService;
45 import fr.paris.lutece.portal.service.file.FileServiceException;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.portal.service.util.AppPropertiesService;
48
49 public class GenericAttributeFileService
50 {
51 private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
52 private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";
53
54 private static final GenericAttributeFileServiceervice/file/GenericAttributeFileService.html#GenericAttributeFileService">GenericAttributeFileService _instance = new GenericAttributeFileService( );
55 private static Map<String,String> _entryTypeFileServices;
56
57
58
59
60 private GenericAttributeFileService( )
61 {
62 List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);
63
64 _entryTypeFileServices = new HashMap<>();
65
66
67 if (keyList != null )
68 {
69 keyList.stream().forEach( s -> {
70 if ( !StringUtils.isAllBlank( AppPropertiesService.getProperty(s) ) )
71 {
72 _entryTypeFileServices.put(s, AppPropertiesService.getProperty(s));
73 }
74 } );
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87 public String getFileStoreProviderName( String strEntryType )
88 {
89 if (strEntryType != null )
90 {
91 if ( _entryTypeFileServices.containsKey( strEntryType ))
92 {
93 return _entryTypeFileServices.get( strEntryType );
94 }
95 }
96
97 if ( _entryTypeFileServices.containsKey( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
98 {
99 return _entryTypeFileServices.get( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
100 }
101
102 return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
103 }
104
105
106
107
108
109
110
111
112
113
114 public String getFileStoreProviderName( )
115 {
116 return getFileStoreProviderName( null );
117 }
118
119
120
121
122
123
124 public static GenericAttributeFileService getInstance( )
125 {
126 return _instance;
127 }
128
129
130
131
132
133
134
135 public String save( File file)
136 {
137 return save( file, null);
138 }
139
140
141
142
143
144
145
146 public String save( File file, String strEntryType)
147 {
148 try
149 {
150 if ( file.getOrigin( ) == null )
151 {
152 file.setOrigin( getFileStoreProviderName( strEntryType ) );
153 }
154
155 return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
156 }
157 catch( FileServiceException e )
158 {
159 AppLogService.error( e );
160 return null;
161 }
162 }
163
164
165
166
167
168
169
170
171 public String save( FileItem file, String strEntryType)
172 {
173 try
174 {
175 return FileService.getInstance( ).getFileStoreServiceProvider( getFileStoreProviderName( strEntryType ) ).storeFileItem( file );
176 }
177 catch( FileServiceException e )
178 {
179 AppLogService.error( e );
180 return null;
181 }
182 }
183
184
185
186
187
188
189
190
191 public File load( String strKey, String strOrigin )
192 {
193 try
194 {
195 if ( StringUtils.isEmpty( strOrigin ) )
196 {
197 strOrigin = getFileStoreProviderName( ) ;
198 }
199
200 return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
201 }
202 catch( FileServiceException e )
203 {
204 AppLogService.error( e );
205 return null;
206 }
207 }
208
209
210
211
212
213
214
215 public void delete( String strKey, String strOrigin )
216 {
217 try
218 {
219 if ( StringUtils.isEmpty( strOrigin ) )
220 {
221 strOrigin = getFileStoreProviderName( );
222 }
223
224 FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
225 }
226 catch( FileServiceException e )
227 {
228 AppLogService.error( e );
229 }
230 }
231
232
233
234
235
236
237
238
239 public String update( File file, String strEntryType )
240 {
241 try
242 {
243 if ( file.getOrigin( ) == null )
244 {
245 file.setOrigin( getFileStoreProviderName( strEntryType ) );
246 }
247
248 String strOldFileKey = file.getFileKey( );
249 String strNewFileKey = FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
250
251 FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).delete( strOldFileKey );
252
253 return strNewFileKey;
254 }
255 catch( FileServiceException e )
256 {
257 AppLogService.error( e );
258 return null;
259 }
260 }
261
262 }