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