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.forms.web.file;
35
36 import javax.servlet.http.HttpServletRequest;
37
38 import org.apache.commons.fileupload.FileItem;
39 import org.apache.commons.lang3.math.NumberUtils;
40
41 import fr.paris.lutece.plugins.forms.service.FormService;
42 import fr.paris.lutece.plugins.forms.util.FormsConstants;
43 import fr.paris.lutece.portal.business.file.File;
44 import fr.paris.lutece.portal.business.file.FileHome;
45 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
46 import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
47 import fr.paris.lutece.portal.service.image.ImageResource;
48 import fr.paris.lutece.portal.service.image.ImageResourceManager;
49 import fr.paris.lutece.portal.service.image.ImageResourceProvider;
50 import fr.paris.lutece.portal.service.spring.SpringContextService;
51 import fr.paris.lutece.portal.service.util.AppException;
52 import fr.paris.lutece.portal.web.LocalVariables;
53 import fr.paris.lutece.util.file.FileUtil;
54
55
56
57
58 public final class FormsFileImageService implements ImageResourceProvider
59 {
60
61 private static final String IMAGE_RESOURCE_TYPE_ID = "forms_entry_img";
62 private static final int ID_RESPONSE_NOT_FOUND = NumberUtils.INTEGER_MINUS_ONE;
63
64
65
66
67 private FormsFileImageService( )
68 {
69
70 }
71
72
73
74
75
76
77 public static FormsFileImageService getInstance( )
78 {
79 return FormsFileImageServiceHolder._singleton;
80 }
81
82
83
84
85 public void register( )
86 {
87 ImageResourceManager.registerProvider( this );
88 }
89
90
91
92
93
94
95
96
97 @Override
98 public ImageResource getImageResource( int nIdResource )
99 {
100
101
102
103
104
105
106
107
108
109
110
111 HttpServletRequest request = LocalVariables.getRequest( );
112
113 ImageResource imageResource = null;
114
115 File file = FileHome.findByPrimaryKey( nIdResource );
116 if ( file != null && file.getPhysicalFile( ) != null && FileUtil.hasImageExtension( file.getTitle( ) ) && request != null )
117 {
118 FormService formService = SpringContextService.getBean( FormService.BEAN_NAME );
119 int nIdResponse = NumberUtils.toInt( request.getParameter( FormsConstants.PARAMETER_ID_RESPONSE ), ID_RESPONSE_NOT_FOUND );
120
121 if ( formService.isFileAccessAuthorized( request, nIdResponse, nIdResource ) )
122 {
123 imageResource = createImageResource( file );
124 }
125 }
126
127 return imageResource;
128 }
129
130
131
132
133
134
135
136
137 private ImageResource createImageResource( File file )
138 {
139 ImageResource imageResource = null;
140
141 if ( file != null && file.getPhysicalFile( ) != null )
142 {
143 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) );
144 if ( physicalFile != null )
145 {
146 imageResource = new ImageResource( );
147 imageResource.setImage( physicalFile.getValue( ) );
148 imageResource.setMimeType( file.getMimeType( ) );
149 }
150 }
151
152 return imageResource;
153 }
154
155
156
157
158
159
160 @Override
161 public String getResourceTypeId( )
162 {
163 return IMAGE_RESOURCE_TYPE_ID;
164 }
165
166
167
168
169 private static class FormsFileImageServiceHolder
170 {
171
172 private static final FormsFileImageService/FormsFileImageService.html#FormsFileImageService">FormsFileImageService _singleton = new FormsFileImageService( );
173 }
174
175 @Override
176 public String addImageResource(FileItem fileItem) {
177 throw new AppException( "Not implemented yet" );
178 }
179 }