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.asynchronousupload.service;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40 import java.io.SequenceInputStream;
41 import java.io.UnsupportedEncodingException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45 import java.util.stream.Collectors;
46
47 import org.apache.commons.fileupload.FileItem;
48 import org.apache.commons.fileupload.FileItemHeaders;
49 import org.apache.commons.io.FilenameUtils;
50 import org.apache.commons.io.IOUtils;
51
52 import fr.paris.lutece.portal.service.util.AppLogService;
53 import fr.paris.lutece.util.filesystem.UploadUtil;
54
55
56
57
58 public class PartialFileItemGroup implements FileItem
59 {
60 private static final long serialVersionUID = 8696893066570050604L;
61 private List<FileItem> _items;
62 private SequenceInputStream _sequenceInputStream;
63
64
65
66
67
68
69
70 public PartialFileItemGroup( List<FileItem> items )
71 {
72 _items = items;
73 List<InputStream> vOut = new ArrayList<>( );
74
75 try
76 {
77
78 for ( FileItem fileItem : items )
79 {
80 vOut.add( fileItem.getInputStream( ) );
81
82 }
83 }
84 catch( IOException e )
85 {
86 AppLogService.error( "error creating Partial File item sequence inputstream", e );
87 }
88 _sequenceInputStream = new SequenceInputStream( Collections.enumeration( vOut ) );
89
90 }
91
92
93
94
95 @Override
96 public void delete( )
97 {
98 for ( FileItem item : _items )
99 {
100 item.delete( );
101 }
102 }
103
104
105
106
107 @Override
108 public byte [ ] get( )
109 {
110
111 byte [ ] bReturn = null;
112 try
113 {
114 bReturn = IOUtils.toByteArray( _sequenceInputStream );
115 }
116 catch( IOException e )
117 {
118 AppLogService.error( "error getting Partial File item inputstream", e );
119
120 }
121 return bReturn;
122 }
123
124
125
126
127 @Override
128 public String getContentType( )
129 {
130 return _items.get( 0 ).getContentType( );
131 }
132
133
134
135
136 @Override
137 public String getFieldName( )
138 {
139 return _items.get( 0 ).getFieldName( );
140 }
141
142
143
144
145 @Override
146 public InputStream getInputStream( ) throws IOException
147 {
148 return _sequenceInputStream;
149 }
150
151
152
153
154 @Override
155 public String getName( )
156 {
157 return UploadUtil.cleanFileName( FilenameUtils.getName( _items.get( 0 ).getName( ) ) );
158 }
159
160
161
162
163 @Override
164 public OutputStream getOutputStream( ) throws IOException
165 {
166 return null;
167 }
168
169
170
171
172 @Override
173 public long getSize( )
174 {
175 return _items.stream( ).collect( Collectors.summingLong( FileItem::getSize ) );
176 }
177
178
179
180
181 @Override
182 public String getString( )
183 {
184 return _items.get( 0 ).getString( );
185 }
186
187
188
189
190 @Override
191 public String getString( String encoding ) throws UnsupportedEncodingException
192 {
193 return _items.get( 0 ).getString( encoding );
194 }
195
196
197
198
199 @Override
200 public boolean isFormField( )
201 {
202 return _items.get( 0 ).isFormField( );
203 }
204
205
206
207
208 @Override
209 public boolean isInMemory( )
210 {
211 return _items.get( 0 ).isInMemory( );
212 }
213
214
215
216
217 @Override
218 public void setFieldName( String name )
219 {
220 _items.get( 0 ).setFieldName( name );
221 }
222
223
224
225
226 @Override
227 public void setFormField( boolean state )
228 {
229 _items.get( 0 ).setFormField( state );
230 }
231
232
233
234
235 @Override
236 public void write( File file ) throws Exception
237 {
238
239 }
240
241
242
243
244 @Override
245 public FileItemHeaders getHeaders( )
246 {
247 return _items.get( 0 ).getHeaders( );
248 }
249
250
251
252
253 @Override
254 public void setHeaders( FileItemHeaders headers )
255 {
256
257 }
258 }