Skip to content

Commit

Permalink
Always delete uploaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Sep 11, 2023
1 parent f4253ff commit 4c044f1
Showing 1 changed file with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected void processFileField(FileItem item) {
LOG.debug("Item is a file upload");

// Skip file uploads that don't have a file name - meaning that no file was selected.
if (item.getName() == null || item.getName().trim().length() < 1) {
if (item.getName() == null || item.getName().trim().isEmpty()) {
LOG.debug("No file has been uploaded for the field: {}", item.getFieldName());
return;
}
Expand All @@ -131,39 +131,42 @@ protected void processFileField(FileItem item) {
}

protected void processNormalFormField(FileItem item, String charset) throws UnsupportedEncodingException {
LOG.debug("Item is a normal form field");
try {
LOG.debug("Item is a normal form field");

List<String> values;
if (params.get(item.getFieldName()) != null) {
values = params.get(item.getFieldName());
} else {
values = new ArrayList<>();
}
List<String> values;
if (params.get(item.getFieldName()) != null) {
values = params.get(item.getFieldName());
} else {
values = new ArrayList<>();
}

long size = item.getSize();
if (size == 0) {
values.add(StringUtils.EMPTY);
} else if (size > maxStringLength) {
String errorKey = "struts.messages.upload.error.parameter.too.long";
LocalizedMessage localizedMessage = new LocalizedMessage(this.getClass(), errorKey, null,
new Object[] { item.getFieldName(), maxStringLength, size });

if (!errors.contains(localizedMessage)) {
errors.add(localizedMessage);
}
return;

} else if (charset != null) {
values.add(item.getString(charset));
} else {
// note: see https://issues.apache.org/jira/browse/WW-633
// basically, in some cases the charset may be null, so
// we're just going to try to "other" method (no idea if this
// will work)
values.add(item.getString());
long size = item.getSize();
if (size == 0) {
values.add(StringUtils.EMPTY);
} else if (size > maxStringLength) {
String errorKey = "struts.messages.upload.error.parameter.too.long";
LocalizedMessage localizedMessage = new LocalizedMessage(this.getClass(), errorKey, null,
new Object[]{item.getFieldName(), maxStringLength, size});

if (!errors.contains(localizedMessage)) {
errors.add(localizedMessage);
}
return;

} else if (charset != null) {
values.add(item.getString(charset));
} else {
// note: see https://issues.apache.org/jira/browse/WW-633
// basically, in some cases the charset may be null, so
// we're just going to try to "other" method (no idea if this
// will work)
values.add(item.getString());
}
params.put(item.getFieldName(), values);
} finally {
item.delete();
}
params.put(item.getFieldName(), values);
item.delete();
}

protected List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
Expand Down Expand Up @@ -218,7 +221,7 @@ public String[] getContentType(String fieldName) {
contentTypes.add(fileItem.getContentType());
}

return contentTypes.toArray(new String[contentTypes.size()]);
return contentTypes.toArray(new String[0]);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -247,7 +250,7 @@ public UploadedFile[] getFile(String fieldName) {
fileList.add(new StrutsUploadedFile(storeLocation));
}

return fileList.toArray(new UploadedFile[fileList.size()]);
return fileList.toArray(new UploadedFile[0]);
}

/* (non-Javadoc)
Expand All @@ -265,7 +268,7 @@ public String[] getFileNames(String fieldName) {
fileNames.add(getCanonicalName(fileItem.getName()));
}

return fileNames.toArray(new String[fileNames.size()]);
return fileNames.toArray(new String[0]);
}

/* (non-Javadoc)
Expand All @@ -283,15 +286,15 @@ public String[] getFilesystemName(String fieldName) {
fileNames.add(((DiskFileItem) fileItem).getStoreLocation().getName());
}

return fileNames.toArray(new String[fileNames.size()]);
return fileNames.toArray(new String[0]);
}

/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameter(java.lang.String)
*/
public String getParameter(String name) {
List<String> v = params.get(name);
if (v != null && v.size() > 0) {
if (v != null && !v.isEmpty()) {
return v.get(0);
}

Expand All @@ -310,8 +313,8 @@ public Enumeration<String> getParameterNames() {
*/
public String[] getParameterValues(String name) {
List<String> v = params.get(name);
if (v != null && v.size() > 0) {
return v.toArray(new String[v.size()]);
if (v != null && !v.isEmpty()) {
return v.toArray(new String[0]);
}

return null;
Expand Down

0 comments on commit 4c044f1

Please sign in to comment.