lovasoa / SQLpage

SQL-only webapp builder, empowering data analysts to build websites and applications quickly
https://sql.ophir.dev
MIT License
883 stars 64 forks source link

Preserve file name on file upload #333

Closed djyotta closed 1 month ago

djyotta commented 1 month ago

What are you building with SQLPage ?

I have a suite of apps I use to help me transfer data between devices.

ie, I can paste something from the clip board on my PC to the web form, and then scan the QR code on my phone. OR, on a device without a camera, the codes are 5 base 32 digits (yes, I'm not worried about collisions) so it's easy to remember and type.

Here is the tools:

I formerly used python + bottle and rolled my own UI. This is fine.

I persisted clip/link/file to disk by naming it by it's base32 hash. Lots of small files. This is fine.

On finding out about SQLPage, I'm converting these so my website has a similar look and feel. Also want to get rid of the horrific python code I wrote.

It also make sense to store this stuff in db so it's easier to cleanup old data and apply size limits (had a bash script to do it, but ... yeah not comfortable with it).

What is your problem ? A description of the problem, not the solution you are proposing.

The drawback with the SQLPage version is that I can not preserve the file name on upload.

My old UI, I was preserving the file name supplied by the browser:

-----------------------------4053620313313727090337127195
Content-Disposition: form-data; name="action"

Upload
-----------------------------4053620313313727090337127195
Content-Disposition: form-data; name="content"; filename="match.sh"
Content-Type: application/x-shellscript

#!/bin/bash
grep -ni 'import .*gnu' "$@"

-----------------------------4053620313313727090337127195--

The Content-Disposition header includes the filename (at least on Brave and Firefox).

What are you currently doing ? Since your solution is not implemented in SQLPage currently, what are you doing instead ?

There doesn't seem to be a way to get the filename out of the form data on SQLPage.

For now, I'm using the base32 code as the file name - at least this is not random and will be consistent on each download and also relates back to the code used to share the file.

Describe the solution you'd like

I'd like to preserve the file name on upload, so that when users download the file, the don't get a random or meaningless name.

A SQLPage function like uploaded_file_mime_type would be good. Maybe call it uploaded_file_name. Can return NULL if user-agent doesn't provide it.

lovasoa commented 1 month ago

Hi ! Indeed, I agree we should have an uploaded_file_name function. It shouldn't be hard to add in functions.rs. Do you want to have a stab at implementing it, and opening a PR ?

The files are stored as TempFiles in the request object. Tempfile has a file_name attribute.

djyotta commented 1 month ago

OK, I'll have a go. I think I should do some basic programs in rust first though - I feel I do everything oddly in rust because I have no idea what is available in terms of libraries etc...

lovasoa commented 1 month ago

Don't hesitate to open even a awkward pull request, I'll be there to help !

I think you don't need any library to implement this. The other file-related functions in functions.rs should give a good idea of how to do things.

djyotta commented 1 month ago

@lovasoa I've started looking at this. I see there is also a file size attribute too which would also be handy to have access to. So I'm wondering if instead we should have a uploaded_file_attributes returning JSON:

{
  "name": "myfile.ext",
  "size": 1234,
  "content-type": "application/octet-stream",
  "path": "/path/to/file"
}
lovasoa commented 1 month ago

If rather have separate functions, for performance and convenience