Add file cleanup callback for uploaded files (issues #264, #270)#356
Merged
Add file cleanup callback for uploaded files (issues #264, #270)#356
Conversation
Add a server-level callback that allows users to customize file cleanup
behavior for uploaded files. Previously, all uploaded files were
automatically deleted when the request completed, making it impossible
to keep or move files to permanent storage.
The new file_cleanup_callback option accepts a function with signature:
bool(const std::string& key, const std::string& filename,
const http::file_info& info)
Return true to delete the file (default behavior) or false to keep it.
If the callback throws an exception, the file is deleted as a safety
measure. When no callback is set, files are deleted (backward compatible).
Changes:
- Add file_cleanup_callback_ptr typedef and builder method
- Store callback in webserver and pass to http_request
- Modify http_request destructor to invoke callback per file
- Add 5 test cases covering callback behavior
- Add file_upload_with_callback example
- Update README with documentation and example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a server-level callback that allows users to customize file cleanup behavior for uploaded files, addressing issues #264 and #270.
Problem: Previously, all uploaded files were automatically deleted when the request completed, making it impossible to keep or move files to permanent storage.
Solution: Add a
file_cleanup_callbackoption that is invoked for each uploaded file when the request completes. The callback can:trueto delete the file (default behavior)falseto keep the file (e.g., after moving it to permanent storage)Usage Example
Changes
file_cleanup_callback_ptrtypedef and builder methodTest plan
make check)Closes #264
Closes #270