1 package fr.paris.lutece.plugins.identityexport.service.file.implementation;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9 import java.util.Map;
10 import java.util.UUID;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.apache.commons.fileupload.FileItem;
15 import org.apache.commons.io.FilenameUtils;
16 import org.apache.commons.io.IOUtils;
17 import org.apache.commons.lang3.StringUtils;
18
19 import fr.paris.lutece.api.user.User;
20 import fr.paris.lutece.portal.business.file.File;
21 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
22 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
23 import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
24 import fr.paris.lutece.portal.service.file.ExpiredLinkException;
25 import fr.paris.lutece.portal.service.file.FileService;
26 import fr.paris.lutece.portal.service.file.IFileDownloadUrlService;
27 import fr.paris.lutece.portal.service.file.IFileRBACService;
28 import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
29 import fr.paris.lutece.portal.service.security.SecurityService;
30 import fr.paris.lutece.portal.service.security.UserNotSignedException;
31 import fr.paris.lutece.portal.service.util.AppException;
32 import fr.paris.lutece.portal.service.util.AppLogService;
33
34
35
36
37
38
39 public class LocalFileSystemDirectoryFileService implements IFileStoreServiceProvider
40 {
41 private static final long serialVersionUID = 1L;
42
43 private IFileDownloadUrlService _fileDownloadUrlService;
44 private IFileRBACService _fileRBACService;
45 private String _strName;
46 private boolean _bDefault;
47 private java.io.File _storageDir ;
48
49
50
51
52
53
54
55
56 public LocalFileSystemDirectoryFileService( String strPath, IFileDownloadUrlService fileDownloadUrlService, IFileRBACService fileRBACService )
57 {
58 this._storageDir = new java.io.File( strPath );
59 this._fileDownloadUrlService = fileDownloadUrlService;
60 this._fileRBACService = fileRBACService;
61 }
62
63
64
65
66
67
68 public IFileRBACService getFileRBACService( )
69 {
70 return _fileRBACService;
71 }
72
73
74
75
76
77
78 public void setFileRBACService( IFileRBACService fileRBACService )
79 {
80 this._fileRBACService = fileRBACService;
81 }
82
83
84
85
86
87
88 public IFileDownloadUrlService getDownloadUrlService( )
89 {
90 return _fileDownloadUrlService;
91 }
92
93
94
95
96
97
98
99 public void setDownloadUrlService( IFileDownloadUrlService downloadUrlService )
100 {
101 _fileDownloadUrlService = downloadUrlService;
102 }
103
104
105
106
107 @Override
108 public String getName( )
109 {
110 return _strName;
111 }
112
113
114
115
116 @Override
117 public void delete( String strKey )
118 {
119
120 java.io.File resource = new java.io.File( _storageDir, strKey );
121
122
123 if ( resource.exists( ) )
124 {
125 resource.delete( );
126 }
127
128 }
129
130
131
132
133 @Override
134 public File getFile( String strKey )
135 {
136 return getFile( strKey, true );
137 }
138
139
140
141
142 @Override
143 public File getFileMetaData( String strKey )
144 {
145 return getFile( strKey, false );
146 }
147
148
149
150
151
152
153
154
155
156 public File getFile( String strKey, boolean withPhysicalFile )
157 {
158 if ( StringUtils.isBlank( strKey ) || strKey.contains("/") || strKey.contains("\\") || strKey.contains("..") )
159 {
160 return null;
161 }
162
163
164 java.io.File resource = new java.io.File( _storageDir, strKey );
165
166
167 if ( !resource.exists( ) )
168 {
169 return null;
170 }
171
172 File file = new File( );
173 file.setFileKey( strKey );
174 file.setTitle( strKey );
175 file.setOrigin( this.getName( ) );
176 file.setSize( (int) resource.length( ) );
177
178 try {
179 file.setMimeType( Files.probeContentType( resource.toPath( ) ) );
180 } catch (IOException e) {
181 AppLogService.error( "unable to get MimeType of file", e);
182 }
183
184
185 if ( withPhysicalFile )
186 {
187 PhysicalFile physicalFile = new PhysicalFile( );
188 try
189 {
190 physicalFile.setValue( Files.readAllBytes( resource.toPath( ) ) );
191 file.setPhysicalFile( physicalFile );
192 }
193 catch (IOException e)
194 {
195 AppLogService.error(e);
196 }
197 }
198
199 return file;
200
201 }
202
203
204
205
206 @Override
207 public String storeBytes( byte [ ] blob )
208 {
209 String randomFileName = UUID.randomUUID( ).toString( );
210 java.io.File resource = new java.io.File( _storageDir, randomFileName );
211
212 Path path = Paths.get( resource.getAbsolutePath( ) );
213
214 try {
215 Files.write( path, blob );
216 } catch (IOException e) {
217 AppLogService.error( e );
218 }
219
220 return String.valueOf( randomFileName );
221 }
222
223
224
225
226 @Override
227 public String storeInputStream( InputStream inputStream )
228 {
229 return "method not implemented yet";
230 }
231
232
233
234
235 @Override
236 public String storeFileItem( FileItem fileItem )
237 {
238 java.io.File file = new java.io.File( _storageDir, FilenameUtils.getName( fileItem.getName( ) ) );
239
240 byte [ ] byteArray;
241 try
242 {
243 byteArray = IOUtils.toByteArray( fileItem.getInputStream( ) );
244 }
245 catch( IOException ex )
246 {
247 throw new AppException( ex.getMessage( ), ex );
248 }
249
250 try {
251 Files.write( Paths.get( file.getAbsolutePath( ) ), byteArray );
252 } catch (IOException e) {
253 AppLogService.error( e );
254 }
255
256 return fileItem.getName( );
257 }
258
259
260
261
262 @Override
263 public String storeFile( File file )
264 {
265 java.io.File resource = new java.io.File( _storageDir, file.getTitle( ) );
266
267 Path path = Paths.get( resource.getAbsolutePath( ) );
268
269 try {
270 Files.write( path, file.getPhysicalFile( ).getValue( ) );
271 } catch (IOException e) {
272 AppLogService.error( e );
273 }
274
275 return String.valueOf( file.getTitle( ) );
276 }
277
278 public void setDefault( boolean bDefault )
279 {
280 this._bDefault = bDefault;
281 }
282
283 public void setName( String strName )
284 {
285 _strName = strName;
286 }
287
288
289
290
291 @Override
292 public boolean isDefault( )
293 {
294 return _bDefault;
295 }
296
297
298
299
300 @Override
301 public InputStream getInputStream( String strKey )
302 {
303 File file = getFile( strKey );
304
305 return new ByteArrayInputStream( file.getPhysicalFile( ).getValue( ) );
306 }
307
308
309
310
311 @Override
312 public String getFileDownloadUrlFO( String strKey )
313 {
314 return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, getName( ) );
315 }
316
317
318
319
320 @Override
321 public String getFileDownloadUrlFO( String strKey, Map<String, String> additionnalData )
322 {
323 return _fileDownloadUrlService.getFileDownloadUrlFO( strKey, additionnalData, getName( ) );
324 }
325
326
327
328
329 @Override
330 public String getFileDownloadUrlBO( String strKey )
331 {
332 return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, getName( ) );
333 }
334
335
336
337
338 @Override
339 public String getFileDownloadUrlBO( String strKey, Map<String, String> additionnalData )
340 {
341 return _fileDownloadUrlService.getFileDownloadUrlBO( strKey, additionnalData, getName( ) );
342 }
343
344
345
346
347 @Override
348 public void checkAccessRights( Map<String, String> fileData, User user ) throws AccessDeniedException, UserNotSignedException
349 {
350 if ( _fileRBACService != null )
351 {
352 _fileRBACService.checkAccessRights( fileData, user );
353 }
354 }
355
356
357
358
359 @Override
360 public void checkLinkValidity( Map<String, String> fileData ) throws ExpiredLinkException
361 {
362 _fileDownloadUrlService.checkLinkValidity( fileData );
363 }
364
365
366
367
368 @Override
369 public File getFileFromRequestBO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
370 {
371 Map<String, String> fileData = _fileDownloadUrlService.getRequestDataBO( request );
372
373
374 checkAccessRights( fileData, AdminAuthenticationService.getInstance( ).getRegisteredUser( request ) );
375
376
377 checkLinkValidity( fileData );
378
379 String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );
380
381 return getFile( strFileId );
382 }
383
384
385
386
387 @Override
388 public File getFileFromRequestFO( HttpServletRequest request ) throws AccessDeniedException, ExpiredLinkException, UserNotSignedException
389 {
390
391 Map<String, String> fileData = _fileDownloadUrlService.getRequestDataFO( request );
392
393
394 checkAccessRights( fileData, SecurityService.getInstance( ).getRegisteredUser( request ) );
395
396
397 checkLinkValidity( fileData );
398
399 String strFileId = fileData.get( FileService.PARAMETER_FILE_ID );
400
401 return getFile( strFileId );
402 }
403 }
404