Add a route to upload update to the platform

This commit is contained in:
2024-10-05 15:50:46 +02:00
parent e1a94acdcb
commit 2f971c0055
10 changed files with 211 additions and 5 deletions

View File

@ -185,6 +185,10 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/ota/supported_platforms",
web::get().to(ota_controller::supported_platforms),
)
.route(
"/web_api/ota/{platform}/{version}",
web::post().to(ota_controller::upload_firmware),
)
// TODO : upload a new software update
// TODO : list ota software update per platform
// TODO : download a OTA file

View File

@ -1,7 +1,51 @@
use crate::constants;
use crate::ota::ota_manager;
use crate::ota::ota_update::OTAPlatform;
use crate::server::custom_error::HttpResult;
use actix_web::HttpResponse;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm;
use actix_web::{web, HttpResponse};
pub async fn supported_platforms() -> HttpResult {
Ok(HttpResponse::Ok().json(vec![OTAPlatform::Wt32Eth01]))
}
#[derive(Debug, MultipartForm)]
pub struct UploadForm {
#[multipart(rename = "firmware")]
firmware: Vec<TempFile>,
}
#[derive(serde::Deserialize)]
pub struct UploadPath {
platform: OTAPlatform,
version: semver::Version,
}
pub async fn upload_firmware(
MultipartForm(form): MultipartForm<UploadForm>,
path: web::Path<UploadPath>,
) -> HttpResult {
if ota_manager::update_exists(path.platform, &path.version)? {
return Ok(HttpResponse::Conflict()
.json("A firmware with the same version has already been uploaded on the platform!"));
}
let Some(file) = form.firmware.first() else {
return Ok(HttpResponse::BadRequest().json("No firmware specified!"));
};
if file.size == 0 {
return Ok(HttpResponse::BadRequest().json("Uploaded file is empty!"));
}
if file.size > constants::MAX_FIRMWARE_SIZE {
return Ok(HttpResponse::BadRequest().json("Uploaded file is too heavy!"));
}
let content = std::fs::read(file.file.path())?;
ota_manager::save_update(path.platform, &path.version, &content)?;
Ok(HttpResponse::Accepted().body("OTA update successfully saved."))
}