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 3, 2023
1 parent 00b000b commit bf54436
Showing 1 changed file with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Multipart form data request adapter for Jakarta Commons Fileupload package.
Expand Down Expand Up @@ -64,7 +70,7 @@ public void parse(HttpServletRequest request, String saveDir) {
} catch (FileUploadException e) {
LOG.warn("Request exceeded size limit!", e);
LocalizedMessage errorMessage;
if(e instanceof FileUploadBase.SizeLimitExceededException) {
if (e instanceof FileUploadBase.SizeLimitExceededException) {
FileUploadBase.SizeLimitExceededException ex = (FileUploadBase.SizeLimitExceededException) e;
errorMessage = buildErrorMessage(e, new Object[]{ex.getPermittedSize(), ex.getActualSize()});
} else {
Expand Down Expand Up @@ -100,7 +106,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 @@ -117,39 +123,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 });
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;
if (!errors.contains(localizedMessage)) {
errors.add(localizedMessage);
}
return;

} else if (charset != null) {
values.add(item.getString(charset));
} else {
// note: see http://jira.opensymphony.com/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());
} 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 @@ -323,14 +332,14 @@ public InputStream getInputStream() throws IOException {
}

/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
*/
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
*/
public void cleanUp() {
Set<String> names = files.keySet();
for (String name : names) {
List<FileItem> items = files.get(name);
for (FileItem item : items) {
LOG.debug("Removing file {} {}", name, item );
LOG.debug("Removing file {} {}", name, item);
if (!item.isInMemory()) {
item.delete();
}
Expand Down

0 comments on commit bf54436

Please sign in to comment.