Skip to content

Commit

Permalink
Merge branch 'current' into issue-4548
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jan 27, 2025
2 parents c654e7b + 029b2bb commit c52b68d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
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 ([email protected])
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
1 change: 0 additions & 1 deletion extension/fileupload/src/site/markdown/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 location as
of 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
Expand Down

0 comments on commit c52b68d

Please sign in to comment.