added error when exists and download now working
All checks were successful
Build Project / test (push) Successful in 5m35s

This commit is contained in:
2025-01-19 16:07:26 +00:00
parent afe1b7de9a
commit 01d35a502f

View File

@@ -4,7 +4,7 @@ use crate::schemas::{TelemetryMessageFromDevice, ValueMessageFromDevice};
use actix_web::{get, post, put, web, App, HttpResponse, HttpServer, Responder};
use database::Database;
use dotenvy::dotenv;
use log::{debug, error, info};
use log::{debug, error, info, warn};
use schemas::{BoardConfig, BoardType, Device, OTAConfiguration, OTAConfigurationList};
use sqlx::types::mac_address::MacAddress;
use util::{get_files, parse_mac_address};
@@ -126,6 +126,7 @@ async fn get_devices(data: web::Data<AppState>) -> impl Responder {
HttpResponse::Ok().json(devices)
}
// Upload Firmware file
#[put("/firmware/{device}/{config}/{version}")]
async fn upload_firmware(data: web::Data<AppState>, path: web::Path<(String, String, String)>, body: web::Bytes) -> impl Responder {
let (device, config, version) = path.into_inner();
@@ -136,6 +137,11 @@ async fn upload_firmware(data: web::Data<AppState>, path: web::Path<(String, Str
let firmware_folder = firmware_root_path.join(&device);
let firmware_path = firmware_folder.join(format!("firmware_{config}_{version}")).with_extension("bin");
if firmware_path.is_file() {
warn!("Firmware with product: {device}, config: {config} and version: {version} at path {firmware_path:?} already exists, cant upload");
return HttpResponse::Conflict().body(format!("{firmware_path:?}"));
}
info!("Uploading firmware with product: {device}, config: {config} and version: {version} to {firmware_path:?}");
fs::create_dir_all(&firmware_folder).unwrap();
@@ -160,31 +166,28 @@ async fn get_firmware_json(data: web::Data<AppState>, path: web::Path<String>) -
}
// #[get("/firmware/{product}/{config}/{version}")]
// async fn get_firmware_json(path: web::Path<(String, String, String)>, data: web::Data<AppState>) -> impl Responder {
// let (product, config, version) = path.into_inner();
// let version = version.replace(".", "-");
// match get_files(&data.firmwares_path, &data.hostname) {
// Ok(cfg) => HttpResponse::Ok().json(OTAConfigurationList{configurations: cfg} ),
// Err(e) => HttpResponse::InternalServerError().body(e.to_string())
// }
// }
#[get("/firmware/{product}/{config}/{version}.bin")]
async fn serve_firmware(path: web::Path<(String, String, String)>, data: web::Data<AppState>) -> impl Responder {
let (product, config, version) = path.into_inner();
let version = version.replace(".", "-").replace("_", "-");
let fw_root_path = &data.firmwares_path;
let file_path = PathBuf::from(format!("{product}/firmware_{config}_{version}.bin"));
let file_path = fw_root_path.join(&file_path);
#[get("/firmware/waterlevel/firmware_INA233_1.0.0.bin")]
async fn serve_firmware() -> impl Responder {
let file_path = PathBuf::from("./firmware/waterlevel/firmware_INA233_1.0.0.bin");
info!("Requested firmware for product: {product}, config: {config} and version: {version}, expected to be stored at {file_path:?}");
if file_path.exists() {
// Serve the file as a download
info!("File exists, serving download now");
HttpResponse::Ok()
.content_type("application/octet-stream") // Binary file MIME type
.content_type("application/octet-stream")
.insert_header((
"Content-Disposition",
format!("attachment; filename=\"{}\"", "firmware_INA233_1.0.0.bin"),
format!("attachment; filename=\"{}\"", format!("{product}_{config}_{version}.bin")),
))
.body(std::fs::read(file_path).unwrap_or_else(|_| Vec::new()))
} else {
warn!("File does not exist");
HttpResponse::NotFound().body("Firmware version not found")
}
}
@@ -210,7 +213,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(AppState { db: db.clone(), firmwares_path: PathBuf::from("./fw"), hostname: "127.0.0.1".to_string() }))
.app_data(web::Data::new(AppState { db: db.clone(), firmwares_path: PathBuf::from("./fw"), hostname: "127.0.0.1:8282".to_string() }))
.app_data(web::PayloadConfig::new(256 * 1024 * 1024)) //256MB
.service(receive_telemetry)
.service(get_telemetry)