-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* document future file functions * make forms multipart when they contain a file field * extract http request data reading logic * start work on file upload * multipart post data parsing * millisecond precision in logs * implement customizable upload file size limits * wip: add file handling functions * file handling pseudo-functions * clippy * fmt
- Loading branch information
Showing
25 changed files
with
821 additions
and
103 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
select 'card' as component; | ||
select 'Your picture' as title; | ||
|
||
select 'debug' as component; | ||
select :my_file as file; |
This file contains 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
99 changes: 99 additions & 0 deletions
99
examples/official-site/sqlpage/migrations/23_uploaded_file_functions.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
-- Insert the 'variables' function into sqlpage_functions table | ||
INSERT INTO sqlpage_functions ( | ||
"name", | ||
"introduced_in_version", | ||
"icon", | ||
"description_md" | ||
) | ||
VALUES ( | ||
'uploaded_file_path', | ||
'0.17.0', | ||
'upload', | ||
'Returns the path to a temporary file containing the contents of an uploaded file. | ||
## Example: handling a picture upload | ||
### Making a form | ||
```sql | ||
select ''form'' as component, ''handle_picture_upload.sql'' as action; | ||
select ''myfile'' as name, ''file'' as type, ''Picture'' as label; | ||
select ''title'' as name, ''text'' as type, ''Title'' as label; | ||
``` | ||
### Handling the form response | ||
In `handle_picture_upload.sql`, one can process the form results like this: | ||
```sql | ||
insert into pictures (title, path) values (:title, sqlpage.read_file_as_data_url(sqlpage.uploaded_file_path(''myfile''))); | ||
``` | ||
' | ||
), | ||
( | ||
'uploaded_file_mime_type', | ||
'0.17.0', | ||
'file-settings', | ||
'Returns the MIME type of an uploaded file. | ||
## Example: handling a picture upload | ||
When letting the user upload a picture, you may want to check that the uploaded file is indeed an image. | ||
```sql | ||
select ''redirect'' as component, | ||
''invalid_file.sql'' as link | ||
where sqlpage.uploaded_file_mime_type(''myfile'') not like ''image/%''; | ||
``` | ||
In `invalid_file.sql`, you can display an error message to the user: | ||
```sql | ||
select ''alert'' as component, ''Error'' as title, | ||
''Invalid file type'' as description, | ||
''alert-circle'' as icon, ''red'' as color; | ||
``` | ||
## Example: white-listing file types | ||
You could have a database table containing the allowed MIME types, and check that the uploaded file is of one of those types: | ||
```sql | ||
select ''redirect'' as component, | ||
''invalid_file.sql'' as link | ||
where sqlpage.uploaded_file_mime_type(''myfile'') not in (select mime_type from allowed_mime_types); | ||
``` | ||
' | ||
); | ||
|
||
INSERT INTO sqlpage_function_parameters ( | ||
"function", | ||
"index", | ||
"name", | ||
"description_md", | ||
"type" | ||
) | ||
VALUES ( | ||
'uploaded_file_path', | ||
1, | ||
'name', | ||
'Name of the file input field in the form.', | ||
'TEXT' | ||
), | ||
( | ||
'uploaded_file_path', | ||
2, | ||
'allowed_mime_type', | ||
'Makes the function return NULL if the uploaded file is not of the specified MIME type. | ||
If omitted, any MIME type is allowed. | ||
This makes it possible to restrict the function to only accept certain file types.', | ||
'TEXT' | ||
), | ||
( | ||
'uploaded_file_mime_type', | ||
1, | ||
'name', | ||
'Name of the file input field in the form.', | ||
'TEXT' | ||
) | ||
; |
Oops, something went wrong.