Can manually download an OTA update
This commit is contained in:
		@@ -191,13 +191,16 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
 | 
			
		||||
                "/web_api/ota/{platform}/{version}",
 | 
			
		||||
                web::post().to(ota_controller::upload_firmware),
 | 
			
		||||
            )
 | 
			
		||||
            .route(
 | 
			
		||||
                "/web_api/ota/{platform}/{version}",
 | 
			
		||||
                web::get().to(ota_controller::download_firmware),
 | 
			
		||||
            )
 | 
			
		||||
            // TODO : delete an OTA file
 | 
			
		||||
            .route("/web_api/ota", web::get().to(ota_controller::list_all_ota))
 | 
			
		||||
            .route(
 | 
			
		||||
                "/web_api/ota/{platform}",
 | 
			
		||||
                web::get().to(ota_controller::list_updates_platform),
 | 
			
		||||
            )
 | 
			
		||||
            // TODO : download a OTA file
 | 
			
		||||
            // TODO : delete an OTA file
 | 
			
		||||
            .route(
 | 
			
		||||
                "/web_api/ota/set_desired_version",
 | 
			
		||||
                web::post().to(ota_controller::set_desired_version),
 | 
			
		||||
 
 | 
			
		||||
@@ -20,14 +20,15 @@ pub struct UploadForm {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Deserialize)]
 | 
			
		||||
pub struct UploadPath {
 | 
			
		||||
pub struct SpecificOTAVersionPath {
 | 
			
		||||
    platform: OTAPlatform,
 | 
			
		||||
    version: semver::Version,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Upload a new firmware update
 | 
			
		||||
pub async fn upload_firmware(
 | 
			
		||||
    MultipartForm(form): MultipartForm<UploadForm>,
 | 
			
		||||
    path: web::Path<UploadPath>,
 | 
			
		||||
    path: web::Path<SpecificOTAVersionPath>,
 | 
			
		||||
) -> HttpResult {
 | 
			
		||||
    if ota_manager::update_exists(path.platform, &path.version)? {
 | 
			
		||||
        return Ok(HttpResponse::Conflict()
 | 
			
		||||
@@ -53,6 +54,26 @@ pub async fn upload_firmware(
 | 
			
		||||
    Ok(HttpResponse::Accepted().body("OTA update successfully saved."))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Download a firmware update
 | 
			
		||||
pub async fn download_firmware(path: web::Path<SpecificOTAVersionPath>) -> HttpResult {
 | 
			
		||||
    if !ota_manager::update_exists(path.platform, &path.version)? {
 | 
			
		||||
        return Ok(HttpResponse::NotFound().json("The requested firmware was not found!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let firmware = ota_manager::get_ota_update(path.platform, &path.version)?;
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok()
 | 
			
		||||
        .content_type("application/octet-stream")
 | 
			
		||||
        .append_header((
 | 
			
		||||
            "content-disposition",
 | 
			
		||||
            format!(
 | 
			
		||||
                "attachment; filename=\"{}-{}.bin\"",
 | 
			
		||||
                path.platform, path.version
 | 
			
		||||
            ),
 | 
			
		||||
        ))
 | 
			
		||||
        .body(firmware))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the list of all OTA updates
 | 
			
		||||
pub async fn list_all_ota() -> HttpResult {
 | 
			
		||||
    Ok(HttpResponse::Ok().json(ota_manager::get_all_ota_updates()?))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user