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.announce.service.entrytype;
35
36 import fr.paris.lutece.plugins.announce.service.upload.AnnounceAsynchronousUploadHandler;
37 import fr.paris.lutece.plugins.genericattributes.business.Entry;
38 import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
39 import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
40 import fr.paris.lutece.plugins.genericattributes.business.Response;
41 import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeFile;
42 import fr.paris.lutece.plugins.genericattributes.service.upload.AbstractGenAttUploadHandler;
43 import fr.paris.lutece.portal.business.file.File;
44 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
45 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
46 import fr.paris.lutece.portal.service.i18n.I18nService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49 import fr.paris.lutece.util.filesystem.FileSystemUtil;
50
51 import org.apache.commons.fileupload.FileItem;
52 import org.apache.commons.lang3.StringUtils;
53
54 import java.awt.image.BufferedImage;
55
56 import java.io.ByteArrayInputStream;
57 import java.io.IOException;
58
59 import java.util.List;
60 import java.util.Locale;
61
62 import javax.imageio.ImageIO;
63
64 import javax.servlet.http.HttpServletRequest;
65
66
67
68
69
70
71 public class EntryTypeImage extends AbstractEntryTypeFile
72 {
73
74
75
76 public static final String BEAN_NAME = "announce.entryTypeImage";
77 private static final String MESSAGE_ERROR_NOT_AN_IMAGE = "announce.message.notAnImage";
78 private static final String TEMPLATE_CREATE = "admin/plugins/announce/entries/create_entry_type_image.html";
79 private static final String TEMPLATE_MODIFY = "admin/plugins/announce/entries/modify_entry_type_image.html";
80 private static final String TEMPLATE_HTML_CODE = "skin/plugins/announce/entries/html_code_entry_type_image.html";
81 private static final String TEMPLATE_HTML_CODE_ADMIN = "admin/plugins/announce/entries/html_code_entry_type_image.html";
82
83
84
85
86 @Override
87 public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
88 {
89 return bDisplayFront ? TEMPLATE_HTML_CODE : TEMPLATE_HTML_CODE_ADMIN;
90 }
91
92
93
94
95 @Override
96 public String getTemplateCreate( Entry entry, boolean bDisplayFront )
97 {
98 return TEMPLATE_CREATE;
99 }
100
101
102
103
104 @Override
105 public String getTemplateModify( Entry entry, boolean bDisplayFront )
106 {
107 return TEMPLATE_MODIFY;
108 }
109
110
111
112
113 @Override
114 public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
115 {
116 List<FileItem> listFilesSource = null;
117
118 if ( request instanceof MultipartHttpServletRequest )
119 {
120
121 String strIdEntry = Integer.toString( entry.getIdEntry( ) );
122 String strAttributePrefix = "attribute" + strIdEntry;
123
124 List<FileItem> asynchronousFileItem = getFileSources( request, strAttributePrefix );
125
126 if ( asynchronousFileItem != null )
127 {
128 listFilesSource = asynchronousFileItem;
129 }
130
131 GenericAttributeError formError = null;
132
133 if ( ( listFilesSource != null ) && !listFilesSource.isEmpty( ) )
134 {
135 formError = checkResponseData( entry, listFilesSource, locale, request );
136
137 if ( formError != null )
138 {
139
140 Response response = new Response( );
141 response.setEntry( entry );
142 listResponse.add( response );
143 }
144
145 for ( FileItem fileItem : listFilesSource )
146 {
147 String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
148
149
150 Response response = new Response( );
151 response.setEntry( entry );
152
153 if ( ( fileItem != null ) && ( fileItem.getSize( ) < Integer.MAX_VALUE ) )
154 {
155 PhysicalFile physicalFile = new PhysicalFile( );
156 physicalFile.setValue( fileItem.get( ) );
157
158 File file = new File( );
159 file.setPhysicalFile( physicalFile );
160 file.setTitle( strFilename );
161 file.setSize( (int) fileItem.getSize( ) );
162 file.setMimeType( FileSystemUtil.getMIMEType( strFilename ) );
163
164 response.setFile( file );
165 }
166
167 listResponse.add( response );
168
169 BufferedImage image = null;
170
171 try
172 {
173 if ( ( fileItem != null ) && ( fileItem.get( ) != null ) )
174 {
175 image = ImageIO.read( new ByteArrayInputStream( fileItem.get( ) ) );
176 }
177 }
178 catch( IOException e )
179 {
180 AppLogService.error( e );
181 }
182
183 if ( ( image == null ) && StringUtils.isNotBlank( strFilename ) )
184 {
185 formError = new GenericAttributeError( );
186 formError.setMandatoryError( false );
187
188 Object [ ] args = {
189 ( fileItem != null ) ? fileItem.getName( ) : StringUtils.EMPTY
190 };
191 formError.setErrorMessage( I18nService.getLocalizedString( MESSAGE_ERROR_NOT_AN_IMAGE, args, request.getLocale( ) ) );
192 formError.setTitleQuestion( entry.getTitle( ) );
193 }
194 }
195
196 return formError;
197 }
198
199 if ( entry.isMandatory( ) && ( ( listFilesSource == null ) || listFilesSource.isEmpty( ) ) )
200 {
201 formError = new MandatoryError( entry, locale );
202
203 Response response = new Response( );
204 response.setEntry( entry );
205 listResponse.add( response );
206 }
207
208 return formError;
209 }
210
211 return new MandatoryError( entry, locale );
212 }
213
214
215
216
217 @Override
218 public AbstractGenAttUploadHandler getAsynchronousUploadHandler( )
219 {
220 return AnnounceAsynchronousUploadHandler.getHandler( );
221 }
222
223
224
225
226 @Override
227 public String getUrlDownloadFile( int nResponseId, String strBaseUrl )
228 {
229 return getUrlDownloadImage( nResponseId, strBaseUrl );
230 }
231
232 @Override
233 protected boolean checkForImages( )
234 {
235 return true;
236 }
237 }