Repository navigation
Conversation
…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.
This comment has been minimized.
This comment has been minimized.
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).
|
Thanks ! Two things:
|
|
Thanks for the review!
Two questions: 0-based (like JSON arrays) or 1-based (like SQL)? And should I drop persist_uploaded_files, or keep it as a shortcut for the common "save them all and insert the paths" case, which is much more verbose with per-row iteration? Happy to rework the PR either way. |
|
I'm not sure what's the best experience, but let's think it through together. I would love to be able to let users do something like insert into uploaded_files(name, local_path, type)
select
sqlpage.uploaded_file_name(f.value),
sqlpage.persist_uploaded_file(f.value),
sqlpage.uploaded_file_mime_type(f.value)
from sqlpage.uploaded_files('my_form_field') as f -- maybe wrap this in json_each ? or introduce a new concept of table-returning sqlpage functions ?But that's hard to do under the current sqlpage model. Do you have an idea that keeps the model coherent, avoids duplicating existing functions, and is easy and intuitive for users ? |
|
Ok, maybe the most realistic yet useful thing to do is keep your function, but make it return a json array of objects, so it can be used like this -- SQLite
set files = sqlpage.persist_uploaded_files('photos', 'photos', 'jpg,jpeg,png,webp');
insert into uploaded_files (name, local_path, mime_type)
select value ->> 'name', value ->> 'path', value ->> 'mime_type'
from json_each($files);what do you think? |
…n failure - Return a JSON array of objects with the persisted path, original name, MIME type and size of each file, so that per-file metadata is available without plural versions of the other uploaded file functions. - If saving a file fails, remove the files already persisted by the same call, so that a failed call leaves nothing behind on disk. A file whose permissions cannot be set is removed too. - Update tests, documentation and changelog.
|
Sounds good to me: it keeps the model coherent and covers the common case in one statement. Done in a49e60f:
|
Problem
The form component already renders
multipleon 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 fieldsends 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_filesis now anUploadedFileslist 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 existingfunctions (
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 matchesname, following the convention used formulti-value form fields.
sqlpage.persist_uploaded_files(field, destination_folder, allowed_extensions, mode). It has the sameparameters and the same security notes as
persist_uploaded_file, and returns the paths as a JSON array, or NULLwhen no file was sent. Every extension is checked before anything is written, so a rejected file leaves no partial
upload behind.
persist_uploaded_filewas split into small helpers (allowed_extension,persist_file) shared by both functions.It has no behavior change.
Tests and docs
photos[]. Both are kept in order, andgetstill returns the last one.79_persist_uploaded_files.sql);multipleproperty now mentions file inputs;cargo fmt --check,cargo clippy --all-targetsand the upload tests pass locally.I use this in a coin-collection app where users send dozens of phone photos in one go.