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

Automatically remove multipart temporary files #5652

Open
ikhoon opened this issue Apr 29, 2024 · 0 comments · May be fixed by #5653
Open

Automatically remove multipart temporary files #5652

ikhoon opened this issue Apr 29, 2024 · 0 comments · May be fixed by #5653

Comments

@ikhoon
Copy link
Contributor

ikhoon commented Apr 29, 2024

When files are uploaded to an Armeria server with multipart/form, it is stored to multipartUploadsLocation() when @Param of annotated services or GraphQL multipart request is used.
https://armeria.dev/docs/server-multipart#using-typeparam-annotation

// See https://github.com/jaydenseric/graphql-multipart-request-spec/blob/master/readme.md
if (contentType.is(MediaType.MULTIPART_FORM_DATA)) {
return HttpResponse.of(FileAggregatedMultipart.aggregateMultipart(ctx, request)

As Armeria does not automatically delete the uploaded files, users should manually remove the temporary files themselves.

// See https://github.com/jaydenseric/graphql-multipart-request-spec/blob/master/readme.md
if (contentType.is(MediaType.MULTIPART_FORM_DATA)) {
return HttpResponse.of(FileAggregatedMultipart.aggregateMultipart(ctx, request)

It would be useful if we provided some options for how to handle multipart files.

enum MultipartRemovalStrategy {
    /**
     * This option would be preferred if a file is transferred using an asynchronous job.
     * Users may want to delete the file when the job is done.
     */
    NEVER,
    /**
     * Remove after a response for the multipart request is fully sent.
     */
    ON_RESPONSE_COMPLETE,
    /**
     * Clean up all temporary files when JVM shuts down.
     * File.deleteOnExit() could be used for this option
     */
    ON_JVM_SHUTDOWN
}

The option can be set via ServerBuilder, VirtualHostBuilder, or ServiceBindingBuilder.

Server
  .builder()
  .multipartRemovalStrategy(MultipartRemovalStrategy.ON_RESPONSE_COMPLETE)
  ...
ikhoon added a commit to ikhoon/armeria that referenced this issue Apr 30, 2024
Motivation:

Armeria does not automatically delete the uploaded files, so users
should manually remove the temporary files themselves.
It would be useful if we provided some options for how to delete
multipart temporary files.

Related: line#5652

Modifications:

- Add `MultipartRemovalStrategy` that is used to determine how to delete
  multipart files. For now, three options are supported.
  - NEVER
  - ON_RESPONSE_COMPLETION
  - ON_JVM_SHUTDOWN
- Add builder methods to server/virtualhost/service builders.

Breaking changes:

- Multipart temporary files are now automatically removed when a
response is fully sent. If you want to keep the existing behavior,
use `MultipartRemovalStrategy.NEVER.

Result:

You can now specify when to remove multipart temporary files using
`MultipartRemovalStrategy`.

```java
Server
  .builder()
  .multipartRemovalStrategy(MultipartRemovalStrategy.ON_RESPONSE_COMPLETION)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant