Skip to content

Commit f15618d

Browse files
committed
Fix a bug in sqlpage.read_file_as_data_url
1 parent 8f92781 commit f15618d

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- new handlebars helper for [custom components](https://sql.ophir.dev/custom_components.sql): `{{app_config 'property'}}` to access the configuration object from the handlebars template.
1212
- Prevent form validation and give a helpful error message when an user tries to submit a form with a file upload field that is above the maximum file size.
1313
- ![file upload too large](https://github.com/lovasoa/SQLpage/assets/552629/1c684d33-49bd-4e49-9ee0-ed3f0d454ced)
14+
- Fix a bug in [`sqlpage.read_file_as_data_url`](https://sql.ophir.dev/functions.sql?function=read_file_as_data_url#function) where it would truncate the mime subtype of the file. This would cause the browser to refuse to display SVG files, for instance.
1415

1516
## 0.23.0 (2024-06-09)
1617

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ async fn read_file_as_data_url<'a>(
300300
|| Cow::Owned(mime_guess_from_filename(&file_path)),
301301
Cow::Borrowed,
302302
);
303-
let mut data_url = format!("data:{}/{};base64,", mime.type_(), mime.subtype());
303+
let mut data_url = format!("data:{mime};base64,");
304304
base64::Engine::encode_string(
305305
&base64::engine::general_purpose::STANDARD,
306306
bytes,

tests/index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ async fn test_upload_file_data_url() -> actix_web::Result<()> {
295295
.set_payload(
296296
"--1234567890\r\n\
297297
Content-Disposition: form-data; name=\"my_file\"; filename=\"testfile.txt\"\r\n\
298-
Content-Type: application/json\r\n\
298+
Content-Type: image/svg+xml\r\n\
299299
\r\n\
300-
{\"a\": 1}\r\n\
300+
<svg></svg>\r\n\
301301
--1234567890--\r\n",
302302
)
303303
.to_srv_request();
@@ -307,7 +307,7 @@ async fn test_upload_file_data_url() -> actix_web::Result<()> {
307307
let body_str = String::from_utf8(body.to_vec()).unwrap();
308308
// The file name suffix was ".txt", but the content type was "application/json"
309309
// so the file should be treated as a JSON file
310-
assert_eq!(body_str, "data:application/json;base64,eyJhIjogMX0=");
310+
assert_eq!(body_str, "data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=");
311311
Ok(())
312312
}
313313

0 commit comments

Comments
 (0)