Skip to content

Conversation

@etr
Copy link
Owner

@etr etr commented Jan 28, 2026

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_callback option that is invoked for each uploaded file when the request completes. The callback can:

  • Return true to delete the file (default behavior)
  • Return false to keep the file (e.g., after moving it to permanent storage)

Usage Example

webserver ws = create_webserver(8080)
    .file_upload_target(FILE_UPLOAD_DISK_ONLY)
    .file_upload_dir("/tmp/uploads")
    .file_cleanup_callback([](const std::string& key,
                              const std::string& filename,
                              const http::file_info& info) {
        // Move file to permanent storage
        std::string dest = "/var/uploads/" + filename;
        std::rename(info.get_file_system_file_name().c_str(), dest.c_str());
        return false;  // Don't delete - we moved it
    });

Changes

  • src/httpserver/create_webserver.hpp: Add file_cleanup_callback_ptr typedef and builder method
  • src/httpserver/webserver.hpp: Add callback storage member
  • src/webserver.cpp: Initialize callback and pass to http_request
  • src/httpserver/http_request.hpp: Add callback member and setter
  • src/http_request.cpp: Modify destructor to invoke callback per file
  • test/integ/file_upload.cpp: Add 5 test cases covering callback behavior
  • examples/file_upload_with_callback.cpp: New example demonstrating the feature
  • examples/Makefile.am: Add new example to build
  • README.md: Documentation and usage example

Test plan

  • All existing tests pass (make check)
  • New test: callback returns true → file deleted
  • New test: callback returns false → file kept
  • New test: selective cleanup (keep some, delete others)
  • New test: callback throws → file deleted (safe default)
  • New test: no callback → backward compatible deletion

Closes #264
Closes #270

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
@etr etr merged commit 89b55d2 into master Jan 29, 2026
38 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]The uploaded file cannot be saved locally Add a hook or callback for cleaning up on disk file uploads

2 participants