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 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
61
62 private GenericAttributeFileService( )
63 {
64 List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);
65
66
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
77
78
79
80
81
82
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
104
105
106
107
108
109
110
111 public String getFileStoreProviderName( )
112 {
113 return getFileStoreProviderName( null );
114 }
115
116
117
118
119
120
121 public static GenericAttributeFileService getInstance( )
122 {
123 return _instance;
124 }
125
126
127
128
129
130
131
132 public String save( File file)
133 {
134 return save( file, null);
135 }
136
137
138
139
140
141
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
163
164
165
166
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
183
184
185
186
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
208
209
210
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 }