Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4542 - Add ability to override upload location #4552

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
* </p>
*
* <ol>
* <li>Sets the MultiPartManager to an instance of FileUploadMultiPartManager.</li>
* <li>Adds the JakartaFileCleaner listener that cleans up the temporary files.</li>
* <li>Sets the MultiPartManager to an instance of
* FileUploadMultiPartManager.</li>
* <li>Adds the JakartaFileCleaner listener that cleans up the temporary
* files.</li>
* </ol>
*
* @author Manfred Riem (mriem@manorrock.com)
Expand All @@ -62,13 +64,17 @@ public class FileUploadMultiPartInitializer implements ServletContainerInitializ
*/
public FileUploadMultiPartInitializer() {
}

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
WebApplication webApplication = (WebApplication) servletContext;
LOGGER.log(TRACE, "Setting the MultiPartManager");
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Setting the MultiPartManager");
}
webApplication.getManager().setMultiPartManager(new FileUploadMultiPartManager());
LOGGER.log(TRACE, "Adding the listener used to cleanup temporary files");
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Adding the listener used to cleanup temporary files");
}
webApplication.addListener("org.apache.commons.fileupload2.jakarta.servlet6.JakartaFileCleaner");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public class FileUploadMultiPartManager implements MultiPartManager {
private static final String FILE_SIZE_THRESHOLD_NAME
= "cloud.piranha.extension.fileupload.fileSizeTreshold";

/**
* Stores the constant for the output directory.
*/
private static final String OUTPUT_DIRECTORY_NAME
= "cloud.piranha.extension.fileupload.outputDirectory";

/**
* Stores the logger.
*/
Expand Down Expand Up @@ -142,27 +148,47 @@ public Part getPart(WebApplication webApplication, WebApplicationRequest request
private synchronized JakartaServletFileUpload setupFileUpload(WebApplication webApplication, MultipartConfigElement multipartConfig) {
JakartaServletFileUpload upload = (JakartaServletFileUpload) webApplication.getAttribute(FileUploadMultiPartManager.class.getName());
if (upload == null) {
File outputDirectory;
if (multipartConfig.getLocation() == null || multipartConfig.getLocation().isEmpty()) {
outputDirectory = (File) webApplication.getAttribute(TEMPDIR);
} else {
/*
* Default to TEMPDIR.
*/
File outputDirectory = (File) webApplication.getAttribute(TEMPDIR);
/*
* If the multipart config has a location use it. If it is relative
* use the TEMPDIR as the parent directory.
*/
if (multipartConfig.getLocation() != null && !multipartConfig.getLocation().isEmpty()) {
File location = new File(multipartConfig.getLocation());
if (!location.isAbsolute()) {
location = ((File) webApplication.getAttribute(TEMPDIR)).toPath().resolve(location.toPath()).toFile();
}
outputDirectory = location;
}
/*
* If OUTPUT_DIRECTORY_NAME is set use it.
*/
if (webApplication.getInitParameter(OUTPUT_DIRECTORY_NAME) != null) {
outputDirectory = new File(webApplication.getInitParameter(OUTPUT_DIRECTORY_NAME));
}
/*
* Default to 10240 (10 KB).
*/
int sizeThreshold = 10240;
/*
* If the multipart config has a size > 0 use it.
*/
if (multipartConfig.getFileSizeThreshold() != 0) {
sizeThreshold = multipartConfig.getFileSizeThreshold();
}
/*
* If FILE_SIZE_THRESHOLD_NAME is set use it.
*/
if (webApplication.getInitParameter(FILE_SIZE_THRESHOLD_NAME) != null) {
try {
sizeThreshold = Integer.parseInt(webApplication.getInitParameter("cloud.piranha.extension.fileupload.fileSizeTreshold"));
} catch (NumberFormatException nfe) {
// ignore and let defaults apply.
}
}
if (multipartConfig.getFileSizeThreshold() != 0) {
sizeThreshold = multipartConfig.getFileSizeThreshold();
}
DiskFileItemFactory factory = DiskFileItemFactory
.builder()
.setBufferSize(sizeThreshold)
Expand Down
23 changes: 23 additions & 0 deletions extension/fileupload/src/site/markdown/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,26 @@ file upload. This extension is available by default for the following runtimes:
1. Piranha Server
1. Piranha Servlet
1. Piranha Web Profile

## Configuration parameters

The following configuration parameters are available:

1. `cloud.piranha.extension.fileupload.outputDirectory` - the directory where
the file upload will store temporary files. The default is the same location
as the ServletContext TEMPDIR.

1. `cloud.piranha.extension.fileupload.fileSizeTreshold` - the file size
threshold (in bytes) before the runtime will create a temporary file on the
filesystem to store the upload. The default is 10240 (10 KB).

## Setting the configuration parameters using web.xml

You can set the configuration parameter in the web.xml as illustrated below:

```xml
<context-param>
<param-name>cloud.piranha.extension.fileupload.fileSizeTreshold</param-name>
<param-value>1048576</param-value>
</context-param>
```
Loading