Skip to content

Keep every file of a multiple file input, and add sqlpage.persist_uploaded_files - #1602

Open
theochr72 wants to merge 2 commits into
sqlpage:mainfrom
theochr72:persist-uploaded-files
Open

theochr72 wants to merge 2 commits into
sqlpage:mainfrom
theochr72:persist-uploaded-files

Conversation

@theochr72

Copy link
Copy Markdown
Contributor

Problem

The form component already renders multiple on file inputs, so users can pick several files at once.
But the server stores uploaded files in a HashMap<String, TempFile> keyed by field name: when a field
sends several files, only the last one survives, and there is no way to reach the others from SQL.
This makes it impossible to build "upload all your photos at once" forms, a common need on mobile.

Change

  • RequestInfo::uploaded_files is now an UploadedFiles list that keeps every file, in the order it was received.
    • get(name) keeps the previous behavior: one file per field, the last one if several were sent. All existing
      functions (uploaded_file_path, uploaded_file_name, uploaded_file_mime_type, persist_uploaded_file,
      CSV import) behave exactly as before.
    • all(name) returns every file of a field. name[] also matches name, following the convention used for
      multi-value form fields.
  • New function sqlpage.persist_uploaded_files(field, destination_folder, allowed_extensions, mode). It has the same
    parameters and the same security notes as persist_uploaded_file, and returns the paths as a JSON array, or NULL
    when no file was sent. Every extension is checked before anything is written, so a rejected file leaves no partial
    upload behind.
  • persist_uploaded_file was split into small helpers (allowed_extension, persist_file) shared by both functions.
    It has no behavior change.
-- form
select 'form' as component, 'save_photos.sql' as action;
select 'file' as type, 'photos[]' as name, true as multiple, 'image/*' as accept;

-- save_photos.sql
set paths = sqlpage.persist_uploaded_files('photos', 'photos', 'jpg,jpeg,png,webp');
insert into photo (path) select value from json_each($paths);

Tests and docs

  • Unit test: three files in a multipart body, two under photos[]. Both are kept in order, and get still returns the last one.
  • Integration tests:
    • three files persisted, with their contents checked;
    • a disallowed extension makes the call fail and writes no file;
    • NULL when no file is sent.
  • Documentation:
    • a new function page (migration 79_persist_uploaded_files.sql);
    • the form multiple property now mentions file inputs;
    • a CHANGELOG entry.

cargo fmt --check, cargo clippy --all-targets and the upload tests pass locally.

I use this in a coin-collection app where users send dozens of phone photos in one go.

…d_files

A file input with the multiple attribute sends several files under the same
field name, but uploaded files were stored in a HashMap keyed by field name,
so only the last one was kept.

- RequestInfo::uploaded_files keeps every file in an UploadedFiles list.
  get(name) still returns a single file (the last one), so existing
  functions behave as before; all(name) returns every file of a field
  (name[] also matches name).
- New sqlpage.persist_uploaded_files(field, folder, extensions, mode):
  persists all the files of a field and returns their paths as a JSON
  array (NULL when no file was sent). Extensions are all checked before
  anything is written.
- Tests, function documentation, form 'multiple' documentation, changelog.
@datadog-official

datadog-official Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 9960b26 | Docs | Give us feedback!

test_persist_uploaded_files_rejects_all_on_bad_extension counted the files of
tests/uploads/persisted_multiple, where test_persist_uploaded_files writes
concurrently, so it failed when both tests overlapped (seen on the MSSQL job).
@lovasoa

lovasoa commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks !

Two things:

  • Currently it looks like the first file is just going to lie on disk forever if there are two files copying the first works, and copying the second returns an error.
  • can we discuss the API/how we expose the feature ? If we add a new function, then we later have to add a plural version of all the existing uploaded file functions. Maybe it would be nicer for the user if we modified the existing functions instead, to add a new optional argument (like uploaded_file_mime_type('myfile', 3)), or a conventional file name indexing scheme (like uploaded_file_mime_type('myfile[3]') and maybe even uploaded_file_mime_type(field_index('myfile', 3))). What do you think ?

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.

2 participants