[Fix] Filedownloads

This commit is contained in:
Alex Emmet 2026-02-15 01:35:21 +01:00
commit bbe18ee759

View file

@ -1,21 +1,27 @@
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes}; use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
use crate::get_public_key;
use crate::server::omikron_manager::get_random_omikron; use crate::server::omikron_manager::get_random_omikron;
use crate::sql::sql; use crate::sql::sql;
use crate::sql::user_online_tracker::get_iota_primary_omikron_connection; use crate::sql::user_online_tracker::get_iota_primary_omikron_connection;
use crate::util::file_util::load_file;
use crate::{get_public_key, log};
use crate::{ use crate::{
sql::sql::{get_by_user_id, get_omikron_by_id}, sql::sql::{get_by_user_id, get_omikron_by_id},
util::crypto_helper::public_key_to_base64, util::crypto_helper::public_key_to_base64,
}; };
use actix_web::HttpResponse; use actix_web::HttpResponse;
use actix_web::body::BoxBody; use actix_web::http::{StatusCode, header};
use actix_web::http::StatusCode;
use base64::Engine as _; use base64::Engine as _;
use json::JsonValue; use json::JsonValue;
use json::number::Number; use json::number::Number;
pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse { pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse {
if path == "OPTIONS" {
return HttpResponse::Ok()
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("Access-Control-Allow-Methods", "GET, POST, OPTIONS"))
.insert_header(("Access-Control-Allow-Headers", "*"))
.finish();
}
let path_parts: Vec<&str> = path.split("/").filter(|s| !s.is_empty()).collect(); let path_parts: Vec<&str> = path.split("/").filter(|s| !s.is_empty()).collect();
let _body: Option<JsonValue> = if body_string.is_some() { let _body: Option<JsonValue> = if body_string.is_some() {
@ -27,11 +33,28 @@ pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse {
} else { } else {
None None
}; };
log!("Path parts: {:?}", path_parts);
let (status, body_text) = match path_parts.as_slice() { let (status, body_text) = match path_parts.as_slice() {
["api", "download", "iota_frontend"] => { ["api", "download", "iota_frontend"] => {
let file = load_file("downloads", "iota_frontend.zip"); let file_path = "downloads/[iota_frontend].zip";
(StatusCode::OK, file)
match std::fs::read(file_path) {
Ok(file_bytes) => {
return HttpResponse::Ok()
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("Content-Type", "application/zip"))
.insert_header((
"Content-Disposition",
"attachment; filename=\"iota_frontend.zip\"",
))
.body(file_bytes);
}
Err(_) => {
return HttpResponse::NotFound()
.insert_header(("Access-Control-Allow-Origin", "*"))
.body("File not found");
}
}
} }
["api", "get", "omikron"] => { ["api", "get", "omikron"] => {
if let Ok(omikron_conn) = get_random_omikron().await { if let Ok(omikron_conn) = get_random_omikron().await {
@ -205,9 +228,13 @@ pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse {
.to_string(), .to_string(),
), ),
}; };
let body_bytes = body_text.to_string().into_bytes(); let body_bytes = body_text.into_bytes();
let body = BoxBody::new(body_bytes.clone());
HttpResponse::new(status).set_body(body) HttpResponse::build(status)
.insert_header((header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"))
.insert_header((header::ACCESS_CONTROL_ALLOW_HEADERS, "*"))
.insert_header((header::ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, OPTIONS"))
.body(body_bytes)
} }
pub fn bad_request() -> (StatusCode, String) { pub fn bad_request() -> (StatusCode, String) {