Skip to content

Commit 7c38f7a

Browse files
committed
implement sqlpage.uploaded_file_mime_type
fixes #204
1 parent 3798078 commit 7c38f7a

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- New `embed` attribute in the `card` component that lets you build multi-column layouts of various components with cards.
1313
- ![](./examples/cards-with-remote-content/screenshot.png)
1414
- Added `id` and `class` attributes to all components, to make it easier to style them with custom CSS and to reference them in intra-page links and custom javascript code.
15+
- Implemented [uploaded_file_mime_type](https://sql.ophir.dev/functions.sql?function=uploaded_file_mime_type#function)
1516

1617
## 0.17.1 (2023-12-10)
1718

examples/official-site/sqlpage/migrations/23_uploaded_file_functions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ insert into uploaded_files (title, path) values (:title, $url);
8888
),
8989
(
9090
'uploaded_file_mime_type',
91-
'0.17.0',
91+
'0.18.0',
9292
'file-settings',
9393
'Returns the MIME type of an uploaded file.
9494

src/webserver/database/sql_pseudofunctions.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(super) enum StmtParam {
3535
SqlPageVersion,
3636
Literal(String),
3737
UploadedFilePath(String),
38+
UploadedFileMimeType(String),
3839
ReadFileAsText(Box<StmtParam>),
3940
ReadFileAsDataUrl(Box<StmtParam>),
4041
Path,
@@ -95,6 +96,10 @@ pub(super) fn func_call_to_param(func_name: &str, arguments: &mut [FunctionArg])
9596
"protocol" => StmtParam::Protocol,
9697
"uploaded_file_path" => extract_single_quoted_string("uploaded_file_path", arguments)
9798
.map_or_else(StmtParam::Error, StmtParam::UploadedFilePath),
99+
"uploaded_file_mime_type" => {
100+
extract_single_quoted_string("uploaded_file_mime_type", arguments)
101+
.map_or_else(StmtParam::Error, StmtParam::UploadedFileMimeType)
102+
}
98103
"read_file_as_text" => StmtParam::ReadFileAsText(Box::new(extract_variable_argument(
99104
"read_file_as_text",
100105
arguments,
@@ -244,7 +249,7 @@ async fn read_file_as_data_url<'a>(
244249
}
245250

246251
fn mime_from_upload<'a>(param0: &StmtParam, request: &'a RequestInfo) -> Option<&'a Mime> {
247-
if let StmtParam::UploadedFilePath(name) = param0 {
252+
if let StmtParam::UploadedFilePath(name) | StmtParam::UploadedFileMimeType(name) = param0 {
248253
request.uploaded_files.get(name)?.content_type.as_ref()
249254
} else {
250255
None
@@ -296,6 +301,12 @@ pub(super) fn extract_req_param_non_nested<'a>(
296301
.get(x)
297302
.and_then(|x| x.file.path().to_str())
298303
.map(Cow::Borrowed),
304+
StmtParam::UploadedFileMimeType(x) => request
305+
.uploaded_files
306+
.get(x)
307+
.and_then(|x| x.content_type.as_ref())
308+
.map(|x| x.as_ref())
309+
.map(Cow::Borrowed),
299310
StmtParam::ReadFileAsText(_) => bail!("Nested read_file_as_text() function not allowed",),
300311
StmtParam::ReadFileAsDataUrl(_) => {
301312
bail!("Nested read_file_as_data_url() function not allowed",)

0 commit comments

Comments
 (0)