use std::fs::File; use std::path::Path; use actix_web::{get, post}; use actix_web::{HttpResponse, web}; use serde::{Deserialize, Serialize}; use crate::dump::{DumpInfo, DumpStatus, compressed_dumps_dir, init_dump_process}; use crate::Data; use crate::error::{Error, ResponseError}; use crate::helpers::Authentication; pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(trigger_dump) .service(get_dump_status); } #[post("/dumps", wrap = "Authentication::Private")] async fn trigger_dump( data: web::Data, ) -> Result { let dumps_dir = Path::new(&data.dumps_dir); match init_dump_process(&data, &dumps_dir) { Ok(resume) => Ok(HttpResponse::Accepted().json(resume)), Err(e) => Err(e.into()) } } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct DumpStatusResponse { status: String, } #[derive(Deserialize)] struct DumpParam { dump_uid: String, } #[get("/dumps/{dump_uid}/status", wrap = "Authentication::Private")] async fn get_dump_status( data: web::Data, path: web::Path, ) -> Result { let dumps_dir = Path::new(&data.dumps_dir); let dump_uid = &path.dump_uid; if let Some(resume) = DumpInfo::get_current() { if &resume.uid == dump_uid { return Ok(HttpResponse::Ok().json(resume)); } } if File::open(compressed_dumps_dir(Path::new(dumps_dir), dump_uid)).is_ok() { let resume = DumpInfo::new( dump_uid.into(), DumpStatus::Done ); Ok(HttpResponse::Ok().json(resume)) } else { Err(Error::not_found("dump does not exist").into()) } }