Skip to content

Commit 48042d6

Browse files
committed
fix: image binaries multiparts was not able to be streamed due to default body limit malfunctioning
1 parent 0e74946 commit 48042d6

File tree

5 files changed

+33
-34
lines changed

5 files changed

+33
-34
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua-astra"
3-
version = "0.18.1"
3+
version = "0.18.2"
44
authors = ["Elham Aryanpur <elhamaryanpur5@gmail.com>"]
55
description = "🔥 Blazingly Fast 🔥 web server runtime for Lua"
66
documentation = "https://astra.arkforge.net/docs/latest"

src/components/http/requests.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl UserData for RequestLua {
8585
.collect::<HashMap<String, String>>())
8686
});
8787
methods.add_async_method("get_cookie", |_, this, name: String| async move {
88+
// ! Move this to a dedicated Cookie Userdata
8889
match this
8990
.cookie
9091
.get(name.as_str())
@@ -108,38 +109,35 @@ impl UserData for RequestLua {
108109
pub struct LuaMultipart {
109110
pub multipart: Multipart,
110111
}
111-
impl LuaMultipart {
112-
async fn save_file(&mut self, file_path: Option<String>) -> mlua::Result<()> {
113-
let mut file_path = if let Some(file_path) = file_path {
114-
Some(tokio::fs::File::create(file_path).await?)
115-
} else {
116-
None
117-
};
112+
impl UserData for LuaMultipart {
113+
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
114+
methods.add_async_method_mut(
115+
"save_file",
116+
|_, mut this, file_path: Option<String>| async move {
117+
let mut file_path = if let Some(file_path) = file_path {
118+
Some(tokio::fs::File::create(file_path).await?)
119+
} else {
120+
None
121+
};
118122

119-
while let Ok(Some(field)) = self.multipart.next_field().await {
120-
if file_path.is_none() {
121-
if let Some(filename) = field.file_name() {
122-
file_path = Some(tokio::fs::File::create(filename).await?);
123-
}
124-
}
123+
while let Ok(Some(field)) = this.multipart.next_field().await {
124+
if file_path.is_none() {
125+
if let Some(filename) = field.file_name() {
126+
file_path = Some(tokio::fs::File::create(filename).await?);
127+
}
128+
}
125129

126-
if let Some(ref mut file) = file_path {
127-
if let Ok(bytes) = field.bytes().await {
128-
if let Err(err) = file.write(&bytes).await {
129-
return Err(mlua::Error::runtime(err));
130+
if let Some(ref mut file) = file_path {
131+
if let Ok(bytes) = field.bytes().await {
132+
if let Err(err) = file.write(&bytes).await {
133+
return Err(mlua::Error::runtime(err));
134+
}
135+
}
130136
}
131137
}
132-
}
133-
}
134138

135-
Ok(())
136-
}
137-
}
138-
impl UserData for LuaMultipart {
139-
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
140-
methods.add_async_method_mut(
141-
"save_file",
142-
|_, mut this, file_path: Option<String>| async move { this.save_file(file_path).await },
139+
Ok(())
140+
},
143141
);
144142
}
145143
}

src/components/http/responses.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
22

3+
// ! Support more cookie types like signed and private
34
#[derive(Debug, Clone)]
45
pub enum CookieOperation {
56
Add { key: String, value: String },
@@ -80,6 +81,7 @@ impl mlua::UserData for ResponseLua {
8081
});
8182

8283
methods.add_method_mut("set_cookie", |_, this, (key, value): (String, String)| {
84+
// ! More operations needs to be added per cookie, such as lifetime, ...
8385
this.cookie_operations
8486
.push(CookieOperation::Add { key, value });
8587

src/components/http/routes.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
use axum::{
1010
Router,
1111
body::Body,
12-
extract::DefaultBodyLimit,
1312
http::Request,
1413
response::IntoResponse,
1514
routing::{delete, get, options, patch, post, put, trace},
@@ -176,6 +175,10 @@ pub fn load_routes() -> Router {
176175
}
177176

178177
if let Ok(settings) = lua.globals().get::<mlua::Table>("Astra") {
178+
// if let Ok(default_body_limit) = settings.get::<usize>("default_body_limit") {
179+
// router = router.layer(DefaultBodyLimit::max(default_body_limit));
180+
// };
181+
179182
if let Ok(should_compress) = settings.get::<bool>("compression") {
180183
if should_compress {
181184
router = router.layer(
@@ -185,10 +188,6 @@ pub fn load_routes() -> Router {
185188
);
186189
}
187190
};
188-
189-
if let Ok(default_body_limit) = settings.get::<usize>("default_body_limit") {
190-
router = router.layer(DefaultBodyLimit::max(default_body_limit));
191-
};
192191
}
193192

194193
router

0 commit comments

Comments
 (0)