1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package com.xpn.xwiki.plugin.fileupload; |
22 |
|
|
23 |
|
import java.io.File; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import org.apache.commons.fileupload.FileItem; |
30 |
|
import org.apache.commons.fileupload.FileUpload; |
31 |
|
import org.apache.commons.fileupload.FileUploadBase; |
32 |
|
import org.apache.commons.fileupload.RequestContext; |
33 |
|
import org.apache.commons.fileupload.disk.DiskFileItem; |
34 |
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
35 |
|
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
36 |
|
import org.apache.commons.fileupload.servlet.ServletRequestContext; |
37 |
|
import org.apache.commons.io.FilenameUtils; |
38 |
|
import org.slf4j.Logger; |
39 |
|
import org.slf4j.LoggerFactory; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWiki; |
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.XWikiException; |
44 |
|
import com.xpn.xwiki.api.Api; |
45 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
46 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
|
|
|
| 70.5% |
Uncovered Elements: 41 (139) |
Complexity: 41 |
Complexity Density: 0.46 |
|
54 |
|
public class FileUploadPlugin extends XWikiDefaultPlugin |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@see |
60 |
|
|
61 |
|
public static final String PLUGIN_NAME = "fileupload"; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public static final String FILE_LIST_KEY = "fileuploadlist"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
public static final String UPLOAD_MAXSIZE_PARAMETER = "upload_maxsize"; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public static final String UPLOAD_SIZETHRESHOLD_PARAMETER = "upload_sizethreshold"; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadPlugin.class); |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private static final long UPLOAD_DEFAULT_MAXSIZE = 33554432L; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private static final long UPLOAD_DEFAULT_SIZETHRESHOLD = 100000L; |
98 |
|
|
99 |
|
|
100 |
|
@param |
101 |
|
@param |
102 |
|
@param |
103 |
|
@see |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
10 |
public FileUploadPlugin(String name, String className, XWikiContext context)... |
106 |
|
{ |
107 |
10 |
super(name, className, context); |
108 |
|
} |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
0 |
@Override... |
111 |
|
public String getName() |
112 |
|
{ |
113 |
0 |
return PLUGIN_NAME; |
114 |
|
} |
115 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
116 |
0 |
@Override... |
117 |
|
public void init(XWikiContext context) |
118 |
|
{ |
119 |
0 |
super.init(context); |
120 |
|
} |
121 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
0 |
@Override... |
123 |
|
public void virtualInit(XWikiContext context) |
124 |
|
{ |
125 |
0 |
super.virtualInit(context); |
126 |
|
} |
127 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
0 |
@Override... |
129 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
130 |
|
{ |
131 |
0 |
return new FileUploadPluginApi((FileUploadPlugin) plugin, context); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
@inheritDoc |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
140 |
0 |
@Override... |
141 |
|
public void endRendering(XWikiContext context) |
142 |
|
{ |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
@see |
153 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
154 |
10 |
public void cleanFileList(XWikiContext context)... |
155 |
|
{ |
156 |
10 |
LOGGER.debug("Cleaning uploaded files"); |
157 |
|
|
158 |
10 |
List<FileItem> fileuploadlist = getFileItems(context); |
159 |
10 |
if (fileuploadlist != null) { |
160 |
10 |
for (FileItem item : fileuploadlist) { |
161 |
38 |
try { |
162 |
38 |
item.delete(); |
163 |
|
} catch (Exception ex) { |
164 |
0 |
LOGGER.warn("Exception cleaning uploaded files", ex); |
165 |
|
} |
166 |
|
} |
167 |
10 |
context.remove(FILE_LIST_KEY); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
@param |
175 |
|
@throws |
176 |
|
@see |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
178 |
10 |
public void loadFileList(XWikiContext context) throws XWikiException... |
179 |
|
{ |
180 |
10 |
XWiki xwiki = context.getWiki(); |
181 |
10 |
loadFileList( |
182 |
|
xwiki.getSpacePreferenceAsLong(UPLOAD_MAXSIZE_PARAMETER, UPLOAD_DEFAULT_MAXSIZE, context), |
183 |
|
(int) xwiki.getSpacePreferenceAsLong(UPLOAD_SIZETHRESHOLD_PARAMETER, UPLOAD_DEFAULT_SIZETHRESHOLD, context), |
184 |
|
xwiki.Param("xwiki.upload.tempdir"), context); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
@param |
191 |
|
@param |
192 |
|
@param |
193 |
|
@param |
194 |
|
@throws |
195 |
|
@see |
196 |
|
|
|
|
| 57.1% |
Uncovered Elements: 12 (28) |
Complexity: 8 |
Complexity Density: 0.4 |
|
197 |
10 |
public void loadFileList(long uploadMaxSize, int uploadSizeThreashold, String tempdir, XWikiContext context)... |
198 |
|
throws XWikiException |
199 |
|
{ |
200 |
10 |
LOGGER.debug("Loading uploaded files"); |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
10 |
if (context.get(FILE_LIST_KEY) != null) { |
205 |
0 |
LOGGER.debug("Called loadFileList twice"); |
206 |
|
|
207 |
0 |
return; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
10 |
DiskFileItemFactory factory = new DiskFileItemFactory() |
213 |
|
{ |
|
|
| 45.5% |
Uncovered Elements: 6 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
214 |
38 |
public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName)... |
215 |
|
{ |
216 |
38 |
try { |
217 |
38 |
final DiskFileItem item = |
218 |
|
(DiskFileItem) super.createItem(fieldName, contentType, isFormField, fileName); |
219 |
|
|
220 |
38 |
item.getOutputStream(); |
221 |
38 |
item.getStoreLocation().deleteOnExit(); |
222 |
38 |
return item; |
223 |
|
} catch (IOException e) { |
224 |
0 |
String path = System.getProperty("java.io.tmpdir"); |
225 |
0 |
if (super.getRepository() != null) { |
226 |
0 |
path = super.getRepository().getPath(); |
227 |
|
} |
228 |
0 |
throw new RuntimeException("Unable to create a temporary file for saving the attachment. " |
229 |
|
+ "Do you have write access on " + path + "?"); |
230 |
|
} |
231 |
|
} |
232 |
|
}; |
233 |
|
|
234 |
10 |
factory.setSizeThreshold(uploadSizeThreashold); |
235 |
|
|
236 |
10 |
if (tempdir != null) { |
237 |
0 |
File tempdirFile = new File(tempdir); |
238 |
0 |
if (tempdirFile.mkdirs() && tempdirFile.canWrite()) { |
239 |
0 |
factory.setRepository(tempdirFile); |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
10 |
FileUpload fileupload = new ServletFileUpload(factory); |
245 |
10 |
RequestContext reqContext = new ServletRequestContext(context.getRequest().getHttpServletRequest()); |
246 |
10 |
fileupload.setSizeMax(uploadMaxSize); |
247 |
|
|
248 |
|
|
249 |
10 |
try { |
250 |
10 |
@SuppressWarnings("unchecked") |
251 |
|
List<FileItem> list = fileupload.parseRequest(reqContext); |
252 |
10 |
if (list.size() > 0) { |
253 |
10 |
LOGGER.info("Loaded " + list.size() + " uploaded files"); |
254 |
|
} |
255 |
|
|
256 |
10 |
context.put(FILE_LIST_KEY, list); |
257 |
|
} catch (FileUploadBase.SizeLimitExceededException e) { |
258 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
259 |
|
XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE, "Exception uploaded file"); |
260 |
|
} catch (Exception e) { |
261 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
262 |
|
XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION, "Exception while parsing uploaded file", e); |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
@link |
268 |
|
@link |
269 |
|
|
270 |
|
@param |
271 |
|
@return |
272 |
|
@see |
273 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
274 |
80 |
public List<FileItem> getFileItems(XWikiContext context)... |
275 |
|
{ |
276 |
80 |
return (List<FileItem>) context.get(FILE_LIST_KEY); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
@link |
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
@param |
285 |
|
@return |
286 |
|
@throws |
287 |
|
@see |
288 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
289 |
20 |
public byte[] getFileItemData(String formfieldName, XWikiContext context) throws XWikiException... |
290 |
|
{ |
291 |
20 |
int size = getFileItemSize(formfieldName, context); |
292 |
|
|
293 |
20 |
if (size == 0) { |
294 |
10 |
return null; |
295 |
|
} |
296 |
|
|
297 |
10 |
byte[] data = new byte[size]; |
298 |
|
|
299 |
10 |
try { |
300 |
10 |
InputStream fileis = getFileItemInputStream(formfieldName, context); |
301 |
10 |
if (fileis != null) { |
302 |
10 |
fileis.read(data); |
303 |
10 |
fileis.close(); |
304 |
|
} |
305 |
|
} catch (java.lang.OutOfMemoryError e) { |
306 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE, |
307 |
|
"Java Heap Space, Out of memory exception", e); |
308 |
|
} catch (IOException ie) { |
309 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
310 |
|
XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION, "Exception while reading uploaded parsed file", |
311 |
|
ie); |
312 |
|
} |
313 |
|
|
314 |
10 |
return data; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
@link |
319 |
|
|
320 |
|
|
321 |
|
@param |
322 |
|
@param |
323 |
|
@return |
324 |
|
@throws |
325 |
|
@since |
326 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
327 |
20 |
public InputStream getFileItemInputStream(String formfieldName, XWikiContext context) throws IOException... |
328 |
|
{ |
329 |
20 |
FileItem fileitem = getFile(formfieldName, context); |
330 |
|
|
331 |
20 |
if (fileitem == null) { |
332 |
0 |
return null; |
333 |
|
} |
334 |
|
|
335 |
20 |
return fileitem.getInputStream(); |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
@link |
340 |
|
|
341 |
|
@param |
342 |
|
@param |
343 |
|
@return |
344 |
|
@since |
345 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
346 |
20 |
public int getFileItemSize(String formfieldName, XWikiContext context)... |
347 |
|
{ |
348 |
20 |
FileItem fileitem = getFile(formfieldName, context); |
349 |
|
|
350 |
20 |
if (fileitem == null) { |
351 |
6 |
return 0; |
352 |
|
} |
353 |
|
|
354 |
14 |
return ((int) fileitem.getSize()); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
@link |
359 |
|
|
360 |
|
|
361 |
|
@param |
362 |
|
@param |
363 |
|
@return |
364 |
|
@throws |
365 |
|
@see |
366 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
367 |
20 |
public String getFileItemAsString(String formfieldName, XWikiContext context) throws XWikiException... |
368 |
|
{ |
369 |
20 |
byte[] data = getFileItemData(formfieldName, context); |
370 |
20 |
if (data == null) { |
371 |
10 |
return null; |
372 |
|
} |
373 |
|
|
374 |
10 |
return new String(data); |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
|
@link |
379 |
|
|
380 |
|
|
381 |
|
@deprecated@link |
382 |
|
@param |
383 |
|
@param |
384 |
|
@return |
385 |
|
@throws |
386 |
|
@see |
387 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
388 |
0 |
@Deprecated... |
389 |
|
public String getFileItem(String formfieldName, XWikiContext context) throws XWikiException |
390 |
|
{ |
391 |
0 |
return getFileItemAsString(formfieldName, context); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
@link |
396 |
|
|
397 |
|
@param |
398 |
|
@return |
399 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
400 |
10 |
public List<String> getFileItemNames(XWikiContext context)... |
401 |
|
{ |
402 |
10 |
List<String> itemnames = new ArrayList<String>(); |
403 |
10 |
List<FileItem> fileuploadlist = getFileItems(context); |
404 |
10 |
if (fileuploadlist == null) { |
405 |
0 |
return itemnames; |
406 |
|
} |
407 |
|
|
408 |
10 |
for (FileItem item : fileuploadlist) { |
409 |
38 |
itemnames.add(item.getFieldName()); |
410 |
|
} |
411 |
|
|
412 |
10 |
return itemnames; |
413 |
|
} |
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
@param |
419 |
|
@param |
420 |
|
@return |
421 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
422 |
10 |
public String getFileName(String formfieldName, XWikiContext context)... |
423 |
|
{ |
424 |
10 |
FileItem fileitem = getFile(formfieldName, context); |
425 |
|
|
426 |
|
|
427 |
10 |
return (fileitem == null) ? null : FilenameUtils.getName(fileitem.getName()); |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
@param |
434 |
|
@param |
435 |
|
@return |
436 |
|
|
|
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
437 |
50 |
public FileItem getFile(String formfieldName, XWikiContext context)... |
438 |
|
{ |
439 |
50 |
LOGGER.debug("Searching file uploaded for field " + formfieldName); |
440 |
|
|
441 |
50 |
List<FileItem> fileuploadlist = getFileItems(context); |
442 |
50 |
if (fileuploadlist == null) { |
443 |
0 |
return null; |
444 |
|
} |
445 |
|
|
446 |
50 |
FileItem fileitem = null; |
447 |
50 |
for (FileItem item : fileuploadlist) { |
448 |
118 |
if (formfieldName.equals(item.getFieldName())) { |
449 |
44 |
fileitem = item; |
450 |
44 |
LOGGER.debug("Found uploaded file!"); |
451 |
44 |
break; |
452 |
|
} |
453 |
|
} |
454 |
|
|
455 |
50 |
return fileitem; |
456 |
|
} |
457 |
|
} |