add fetch_b64 for downloading binary files in base64 format

This commit is contained in:
2024-08-31 16:20:05 +02:00
parent 9323e64fa2
commit 36cf1c7cf2

View File

@@ -205,6 +205,20 @@ async fn file_handler(request: Request) -> impl IntoResponse {
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
let fetch_b64 = lua
.create_function(|lua, uri: String| {
let resp = reqwest::blocking::get(&uri)
.and_then(|resp| resp.error_for_status())
.into_lua_err()?;
let bytes = resp.bytes().into_lua_err()?;
let b64 = BASE64_STANDARD.encode(bytes);
lua.to_value(&b64)
})
.map_err(|e| {
eprintln!("Lua Error: {:?}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
let dbg = lua let dbg = lua
.create_function(|_, value: LuaValue| { .create_function(|_, value: LuaValue| {
dbg!(value); dbg!(value);
@@ -220,6 +234,11 @@ async fn file_handler(request: Request) -> impl IntoResponse {
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
globals.set("fetch_b64", fetch_b64).map_err(|e| {
eprintln!("Lua Error: {:?}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
globals.set("dbg", dbg).map_err(|e| { globals.set("dbg", dbg).map_err(|e| {
eprintln!("Lua Error: {:?}", e); eprintln!("Lua Error: {:?}", e);
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR