diff --git a/virtweb_backend/src/controllers/groups_controller.rs b/virtweb_backend/src/controllers/groups_controller.rs index bab92cc..d3cdb0d 100644 --- a/virtweb_backend/src/controllers/groups_controller.rs +++ b/virtweb_backend/src/controllers/groups_controller.rs @@ -117,13 +117,30 @@ pub async fn vm_reset(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult Ok(HttpResponse::Ok().json(res)) } +/// Get the screenshot of the VMs of a group +pub async fn vm_screenshot(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { + if vms.0.is_empty() { + return Ok(HttpResponse::NoContent().finish()); + } + + let image = if vms.0.len() == 1 { + client.screenshot_domain(vms.0[0].uuid.unwrap()).await? + } else { + return Ok( + HttpResponse::UnprocessableEntity().json("Cannot return multiple VM screenshots!!") + ); + }; + + Ok(HttpResponse::Ok().content_type("image/png").body(image)) +} + /// Get the state of the VMs pub async fn vm_state(client: LibVirtReq, vms: GroupVmIdExtractor) -> HttpResult { let mut states = HashMap::new(); for vm in vms.0 { if let Some(uuid) = vm.uuid { - states.insert(uuid.clone(), client.get_domain_state(uuid).await?); + states.insert(uuid, client.get_domain_state(uuid).await?); } } diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index a8e18f2..c9e9765 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -240,7 +240,10 @@ async fn main() -> std::io::Result<()> { "/api/group/{gid}/vm/reset", web::get().to(groups_controller::vm_reset), ) - // TODO screenshot VM + .route( + "/api/group/{gid}/vm/screenshot", + web::get().to(groups_controller::vm_screenshot), + ) .route( "/api/group/{gid}/vm/state", web::get().to(groups_controller::vm_state),