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.directory.business;
35
36 import fr.paris.lutece.plugins.directory.service.upload.DirectoryAsynchronousUploadHandler;
37 import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39 import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
40 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
41 import fr.paris.lutece.portal.service.i18n.I18nService;
42 import fr.paris.lutece.portal.service.message.AdminMessage;
43 import fr.paris.lutece.portal.service.message.AdminMessageService;
44 import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46 import fr.paris.lutece.util.filesystem.FileSystemUtil;
47
48 import org.apache.commons.fileupload.FileItem;
49 import org.apache.commons.lang.StringUtils;
50
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.Locale;
54
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpSession;
57
58
59
60
61
62
63 public abstract class AbstractEntryTypeUpload extends Entry
64 {
65
66 protected static final String PARAMETER_MAX_FILES = "max_files";
67 protected static final String PARAMETER_FILE_MAX_SIZE = "file_max_size";
68
69
70 protected static final String CONSTANT_MAX_FILES = "max_files";
71 protected static final String CONSTANT_FILE_MAX_SIZE = "file_max_size";
72 protected static final String ALL = "*";
73 protected static final String COMMA = ",";
74
75
76 private static final String PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_MAX_FILES = "directory.message.error.uploading_file.max_files";
77 private static final String PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_FILE_MAX_SIZE = "directory.message.error.uploading_file.file_max_size";
78 private static final String PROPERTY_UPLOAD_FILE_DEFAULT_MAX_SIZE = "directory.upload.file.default_max_size";
79
80
81 private static final String FIELD_MAX_FILES = "directory.create_entry.label_max_files";
82 private static final String FIELD_FILE_MAX_SIZE = "directory.create_entry.label_file_max_size";
83
84
85
86
87
88
89
90
91
92 protected abstract void setFields( HttpServletRequest request, List<Field> listFields );
93
94
95
96
97
98
99
100
101
102
103
104 protected abstract void checkRecordFieldData( FileItem fileItem, Locale locale ) throws DirectoryErrorException;
105
106
107
108
109 @Override
110 public void canUploadFiles( List<FileItem> listUploadedFileItems, List<FileItem> listFileItemsToUpload, Locale locale ) throws DirectoryErrorException
111 {
112
113 Field fieldMaxFiles = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_MAX_FILES, getFields( ) );
114
115
116 int nMaxFiles = 1;
117
118 if ( ( fieldMaxFiles != null ) && StringUtils.isNotBlank( fieldMaxFiles.getValue( ) ) && StringUtils.isNumeric( fieldMaxFiles.getValue( ) ) )
119 {
120 nMaxFiles = DirectoryUtils.convertStringToInt( fieldMaxFiles.getValue( ) );
121 }
122
123 if ( ( listUploadedFileItems != null ) && ( listFileItemsToUpload != null ) )
124 {
125 int nNbFiles = listUploadedFileItems.size( ) + listFileItemsToUpload.size( );
126
127 if ( nNbFiles > nMaxFiles )
128 {
129 Object [ ] params = {
130 nMaxFiles
131 };
132 String strMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_MAX_FILES, params, locale );
133 throw new DirectoryErrorException( this.getTitle( ), strMessage );
134 }
135 }
136
137
138 Field fieldFileMaxSize = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_FILE_MAX_SIZE, getFields( ) );
139 int nMaxSize = DirectoryUtils.CONSTANT_ID_NULL;
140
141 if ( ( fieldFileMaxSize != null ) && StringUtils.isNotBlank( fieldFileMaxSize.getValue( ) ) && StringUtils.isNumeric( fieldFileMaxSize.getValue( ) ) )
142 {
143 nMaxSize = DirectoryUtils.convertStringToInt( fieldFileMaxSize.getValue( ) );
144 }
145 else
146 {
147
148 Field fieldOption = DirectoryUtils.findFieldByTitleInTheList( EntryTypeDownloadUrl.CONSTANT_OPTION, getFields( ) );
149
150 if ( fieldOption != null )
151 {
152 nMaxSize = fieldOption.getWidth( );
153 }
154 }
155
156
157 if ( nMaxSize == DirectoryUtils.CONSTANT_ID_NULL )
158 {
159 nMaxSize = AppPropertiesService.getPropertyInt( PROPERTY_UPLOAD_FILE_DEFAULT_MAX_SIZE, 5242880 );
160 }
161
162
163 if ( ( nMaxSize != DirectoryUtils.CONSTANT_ID_NULL ) && ( listFileItemsToUpload != null ) && !listFileItemsToUpload.isEmpty( ) )
164 {
165 for ( FileItem fileItem : listFileItemsToUpload )
166 {
167 if ( fileItem.getSize( ) > nMaxSize )
168 {
169 Object [ ] params = {
170 nMaxSize
171 };
172 String strMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_FILE_MAX_SIZE, params, locale );
173 throw new DirectoryErrorException( this.getTitle( ), strMessage );
174 }
175 }
176 }
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 protected String checkEntryData( HttpServletRequest request, Locale locale )
191 {
192 String strTitle = request.getParameter( PARAMETER_TITLE );
193 String strMaxFiles = request.getParameter( PARAMETER_MAX_FILES );
194 String strFileMaxSize = request.getParameter( PARAMETER_FILE_MAX_SIZE );
195 String strFieldError = DirectoryUtils.EMPTY_STRING;
196
197 if ( StringUtils.isBlank( strTitle ) )
198 {
199 strFieldError = FIELD_TITLE;
200 }
201 else
202 if ( StringUtils.isBlank( strMaxFiles ) )
203 {
204 strFieldError = FIELD_MAX_FILES;
205 }
206 else
207 if ( StringUtils.isBlank( strFileMaxSize ) )
208 {
209 strFieldError = FIELD_FILE_MAX_SIZE;
210 }
211
212 if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
213 {
214 Object [ ] tabRequiredFields = {
215 I18nService.getLocalizedString( strFieldError, locale )
216 };
217
218 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
219 }
220
221 if ( !StringUtils.isNumeric( strMaxFiles ) )
222 {
223 strFieldError = FIELD_MAX_FILES;
224 }
225 else
226 if ( !StringUtils.isNumeric( strFileMaxSize ) )
227 {
228 strFieldError = FIELD_FILE_MAX_SIZE;
229 }
230
231 if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
232 {
233 Object [ ] tabRequiredFields = {
234 I18nService.getLocalizedString( strFieldError, locale )
235 };
236
237 return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
238 }
239
240 return StringUtils.EMPTY;
241 }
242
243
244
245
246
247
248
249
250
251
252
253 protected void checkRecordFieldData( List<FileItem> listFilesSource, Locale locale ) throws DirectoryErrorException
254 {
255 for ( FileItem fileSource : listFilesSource )
256 {
257
258 String strFilename = ( fileSource != null ) ? FileUploadService.getFileNameOnly( fileSource ) : StringUtils.EMPTY;
259
260 if ( isMandatory( ) && StringUtils.isBlank( strFilename ) )
261 {
262 throw new DirectoryErrorException( getTitle( ) );
263 }
264
265 String strMimeType = FileSystemUtil.getMIMEType( strFilename );
266
267
268 List<RegularExpression> listRegularExpression = this.getFields( ).get( 0 ).getRegularExpressionList( );
269
270 if ( StringUtils.isNotBlank( strFilename ) && ( listRegularExpression != null ) && !listRegularExpression.isEmpty( )
271 && RegularExpressionService.getInstance( ).isAvailable( ) )
272 {
273 for ( RegularExpression regularExpression : listRegularExpression )
274 {
275 if ( !RegularExpressionService.getInstance( ).isMatches( strMimeType, regularExpression ) )
276 {
277 throw new DirectoryErrorException( getTitle( ), regularExpression.getErrorMessage( ) );
278 }
279 }
280 }
281
282
283 checkRecordFieldData( fileSource, locale );
284 }
285 }
286
287
288
289
290
291
292
293
294
295
296 protected List<FileItem> getFileSources( HttpServletRequest request )
297 {
298 HttpSession session = request.getSession( false );
299
300 if ( session != null )
301 {
302
303 return DirectoryAsynchronousUploadHandler.getHandler( ).getFileItems( Integer.toString( getIdEntry( ) ), session.getId( ) );
304 }
305
306 return null;
307 }
308
309
310
311
312
313
314
315
316
317 protected void setFields( HttpServletRequest request )
318 {
319 List<Field> listFields = new ArrayList<Field>( );
320 listFields.add( buildFieldMaxFiles( request ) );
321 listFields.add( buildFieldFileMaxSize( request ) );
322
323 setFields( request, listFields );
324
325 this.setFields( listFields );
326 }
327
328
329
330
331
332
333
334
335
336
337 private Field buildFieldMaxFiles( HttpServletRequest request )
338 {
339 String strMaxFiles = request.getParameter( PARAMETER_MAX_FILES );
340 int nMaxFiles = DirectoryUtils.convertStringToInt( strMaxFiles );
341 Field fieldMaxFiles = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_MAX_FILES, getFields( ) );
342
343 if ( fieldMaxFiles == null )
344 {
345 fieldMaxFiles = new Field( );
346 }
347
348 fieldMaxFiles.setEntry( this );
349 fieldMaxFiles.setTitle( CONSTANT_MAX_FILES );
350 fieldMaxFiles.setValue( Integer.toString( nMaxFiles ) );
351
352 return fieldMaxFiles;
353 }
354
355
356
357
358
359
360
361
362 private Field buildFieldFileMaxSize( HttpServletRequest request )
363 {
364 String strFileMaxSize = request.getParameter( PARAMETER_FILE_MAX_SIZE );
365 int nFileMaxSize = DirectoryUtils.convertStringToInt( strFileMaxSize );
366 Field fieldMaxFiles = DirectoryUtils.findFieldByTitleInTheList( CONSTANT_FILE_MAX_SIZE, getFields( ) );
367
368 if ( fieldMaxFiles == null )
369 {
370 fieldMaxFiles = new Field( );
371 }
372
373 fieldMaxFiles.setEntry( this );
374 fieldMaxFiles.setTitle( CONSTANT_FILE_MAX_SIZE );
375 fieldMaxFiles.setValue( Integer.toString( nFileMaxSize ) );
376
377 return fieldMaxFiles;
378 }
379 }