Implement manual deletion behaviour and fix #35

This commit is contained in:
Daniel Szabo 2022-07-31 19:18:07 +01:00
parent cc09d1b529
commit 435c07d75e

View file

@ -2,10 +2,12 @@ use actix_web::{get, web, HttpResponse};
use crate::args::ARGS; use crate::args::ARGS;
use crate::endpoints::errors::ErrorTemplate; use crate::endpoints::errors::ErrorTemplate;
use crate::pasta::PastaFile;
use crate::util::animalnumbers::to_u64; use crate::util::animalnumbers::to_u64;
use crate::util::misc::remove_expired; use crate::util::misc::remove_expired;
use crate::AppState; use crate::AppState;
use askama::Template; use askama::Template;
use std::fs;
#[get("/remove/{id}")] #[get("/remove/{id}")]
pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse { pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
@ -19,10 +21,22 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
let id = to_u64(&*id.into_inner()).unwrap_or(0); let id = to_u64(&*id.into_inner()).unwrap_or(0);
remove_expired(&mut pastas);
for (i, pasta) in pastas.iter().enumerate() { for (i, pasta) in pastas.iter().enumerate() {
if pasta.id == id { if pasta.id == id {
// 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
pastas.remove(i); pastas.remove(i);
return HttpResponse::Found() return HttpResponse::Found()
.append_header(("Location", "/pastalist")) .append_header(("Location", "/pastalist"))
@ -30,6 +44,8 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
} }
} }
remove_expired(&mut pastas);
HttpResponse::Ok() HttpResponse::Ok()
.content_type("text/html") .content_type("text/html")
.body(ErrorTemplate { args: &ARGS }.render().unwrap()) .body(ErrorTemplate { args: &ARGS }.render().unwrap())