2022-06-03 17:24:34 +01:00
|
|
|
use actix_web::{get, web, HttpResponse};
|
|
|
|
|
|
|
|
use crate::args::ARGS;
|
|
|
|
use crate::endpoints::errors::ErrorTemplate;
|
2022-07-31 19:18:07 +01:00
|
|
|
use crate::pasta::PastaFile;
|
2022-06-03 17:24:34 +01:00
|
|
|
use crate::util::animalnumbers::to_u64;
|
|
|
|
use crate::util::misc::remove_expired;
|
|
|
|
use crate::AppState;
|
|
|
|
use askama::Template;
|
2022-07-31 19:18:07 +01:00
|
|
|
use std::fs;
|
2022-06-03 17:24:34 +01:00
|
|
|
|
|
|
|
#[get("/remove/{id}")]
|
|
|
|
pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
|
|
|
if ARGS.readonly {
|
2022-06-04 22:21:22 +01:00
|
|
|
return HttpResponse::Found()
|
|
|
|
.append_header(("Location", "/"))
|
|
|
|
.finish();
|
2022-06-03 17:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut pastas = data.pastas.lock().unwrap();
|
|
|
|
|
|
|
|
let id = to_u64(&*id.into_inner()).unwrap_or(0);
|
|
|
|
|
|
|
|
for (i, pasta) in pastas.iter().enumerate() {
|
|
|
|
if pasta.id == id {
|
2022-07-31 19:18:07 +01:00
|
|
|
// remove the file itself
|
|
|
|
if let Some(PastaFile { name, .. }) = &pasta.file {
|
|
|
|
if fs::remove_file(format!("./pasta_data/{}/{}", pasta.id_as_animals(), name))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
log::error!("Failed to delete file {}!", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// and remove the containing directory
|
|
|
|
if fs::remove_dir(format!("./pasta_data/{}/", pasta.id_as_animals())).is_err() {
|
|
|
|
log::error!("Failed to delete directory {}!", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove it from in-memory pasta list
|
2022-06-03 17:24:34 +01:00
|
|
|
pastas.remove(i);
|
2022-06-04 22:21:22 +01:00
|
|
|
return HttpResponse::Found()
|
2022-06-03 17:24:34 +01:00
|
|
|
.append_header(("Location", "/pastalist"))
|
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 19:18:07 +01:00
|
|
|
remove_expired(&mut pastas);
|
|
|
|
|
2022-06-04 21:50:34 +01:00
|
|
|
HttpResponse::Ok()
|
2022-06-03 17:24:34 +01:00
|
|
|
.content_type("text/html")
|
|
|
|
.body(ErrorTemplate { args: &ARGS }.render().unwrap())
|
|
|
|
}
|