From be3ac279207de7f370bf09bd499ec5f1213c65fa Mon Sep 17 00:00:00 2001 From: Daniel Szabo Date: Sat, 4 Jun 2022 21:50:34 +0100 Subject: [PATCH] Quick patch: - Changed 302 responses to 200 where needed - Fixed a bug where expiring pastas cause MicroBin to crahs - Fixed a bug where water.css didn't have the correct MIME type - Fixed a bug where missing doctype declaration caused styling issues in Firefox --- src/endpoints/create.rs | 2 +- src/endpoints/edit.rs | 6 +++--- src/endpoints/errors.rs | 2 +- src/endpoints/help.rs | 2 +- src/endpoints/pasta.rs | 10 +++++----- src/endpoints/pastalist.rs | 2 +- src/endpoints/remove.rs | 8 +++----- src/endpoints/static_resources.rs | 2 +- src/util/misc.rs | 22 +++++++++++++++------- templates/header.html | 1 + 10 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/endpoints/create.rs b/src/endpoints/create.rs index 64c6ab6..1cbc20d 100644 --- a/src/endpoints/create.rs +++ b/src/endpoints/create.rs @@ -18,7 +18,7 @@ struct IndexTemplate<'a> { #[get("/")] pub async fn index() -> impl Responder { - HttpResponse::Found() + HttpResponse::Ok() .content_type("text/html") .body(IndexTemplate { args: &ARGS }.render().unwrap()) } diff --git a/src/endpoints/edit.rs b/src/endpoints/edit.rs index 21693d5..f928324 100644 --- a/src/endpoints/edit.rs +++ b/src/endpoints/edit.rs @@ -31,7 +31,7 @@ pub async fn get_edit(data: web::Data, id: web::Path) -> HttpR .append_header(("Location", "/")) .finish(); } - return HttpResponse::Found().content_type("text/html").body( + return HttpResponse::Ok().content_type("text/html").body( EditTemplate { pasta: &pasta, args: &ARGS, @@ -42,7 +42,7 @@ pub async fn get_edit(data: web::Data, id: web::Path) -> HttpR } } - HttpResponse::Found() + HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } @@ -93,7 +93,7 @@ pub async fn post_edit( } } - Ok(HttpResponse::Found() + Ok(HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap())) } diff --git a/src/endpoints/errors.rs b/src/endpoints/errors.rs index ceda5b2..04626b4 100644 --- a/src/endpoints/errors.rs +++ b/src/endpoints/errors.rs @@ -10,7 +10,7 @@ pub struct ErrorTemplate<'a> { } pub async fn not_found() -> Result { - Ok(HttpResponse::Found() + Ok(HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap())) } diff --git a/src/endpoints/help.rs b/src/endpoints/help.rs index f3aa6fd..2ca8f38 100644 --- a/src/endpoints/help.rs +++ b/src/endpoints/help.rs @@ -12,7 +12,7 @@ struct Help<'a> { #[get("/help")] pub async fn help() -> HttpResponse { - HttpResponse::Found().content_type("text/html").body( + HttpResponse::Ok().content_type("text/html").body( Help { args: &ARGS, _marker: Default::default(), diff --git a/src/endpoints/pasta.rs b/src/endpoints/pasta.rs index 8099e4c..2222553 100644 --- a/src/endpoints/pasta.rs +++ b/src/endpoints/pasta.rs @@ -27,7 +27,7 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR for pasta in pastas.iter() { if pasta.id == id { - return HttpResponse::Found().content_type("text/html").body( + return HttpResponse::Ok().content_type("text/html").body( PastaTemplate { pasta: &pasta, args: &ARGS, @@ -38,7 +38,7 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR } } - HttpResponse::Found() + HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } @@ -54,18 +54,18 @@ pub async fn redirecturl(data: web::Data, id: web::Path) -> Ht for pasta in pastas.iter() { if pasta.id == id { if pasta.pasta_type == "url" { - return HttpResponse::Found() + return HttpResponse::Ok() .append_header(("Location", String::from(&pasta.content))) .finish(); } else { - return HttpResponse::Found() + return HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap()); } } } - HttpResponse::Found() + HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } diff --git a/src/endpoints/pastalist.rs b/src/endpoints/pastalist.rs index 876017e..3e3dec4 100644 --- a/src/endpoints/pastalist.rs +++ b/src/endpoints/pastalist.rs @@ -25,7 +25,7 @@ pub async fn list(data: web::Data) -> HttpResponse { remove_expired(&mut pastas); - HttpResponse::Found().content_type("text/html").body( + HttpResponse::Ok().content_type("text/html").body( PastaListTemplate { pastas: &pastas, args: &ARGS, diff --git a/src/endpoints/remove.rs b/src/endpoints/remove.rs index f04217d..4344b49 100644 --- a/src/endpoints/remove.rs +++ b/src/endpoints/remove.rs @@ -10,9 +10,7 @@ use askama::Template; #[get("/remove/{id}")] pub async fn remove(data: web::Data, id: web::Path) -> HttpResponse { if ARGS.readonly { - return HttpResponse::Found() - .append_header(("Location", "/")) - .finish(); + return HttpResponse::Ok().append_header(("Location", "/")).finish(); } let mut pastas = data.pastas.lock().unwrap(); @@ -24,13 +22,13 @@ pub async fn remove(data: web::Data, id: web::Path) -> HttpRes for (i, pasta) in pastas.iter().enumerate() { if pasta.id == id { pastas.remove(i); - return HttpResponse::Found() + return HttpResponse::Ok() .append_header(("Location", "/pastalist")) .finish(); } } - HttpResponse::Found() + HttpResponse::Ok() .content_type("text/html") .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } diff --git a/src/endpoints/static_resources.rs b/src/endpoints/static_resources.rs index b116852..9a5318e 100644 --- a/src/endpoints/static_resources.rs +++ b/src/endpoints/static_resources.rs @@ -11,7 +11,7 @@ struct WaterCSS<'a> { #[get("/static/{resource}")] pub async fn static_resources(resource_id: web::Path) -> HttpResponse { match resource_id.into_inner().as_str() { - "water.css" => HttpResponse::Found().content_type("text/html").body( + "water.css" => HttpResponse::Ok().content_type("text/css").body( WaterCSS { _marker: Default::default(), } diff --git a/src/util/misc.rs b/src/util/misc.rs index 233f048..449f97f 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -3,7 +3,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use linkify::{LinkFinder, LinkKind}; use std::fs; -use crate::Pasta; +use crate::{dbio, Pasta}; pub fn remove_expired(pastas: &mut Vec) { // get current time - this will be needed to check which pastas have expired @@ -22,17 +22,25 @@ pub fn remove_expired(pastas: &mut Vec) { true } else { // remove the file itself - fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file)) - .expect(&*format!("Failed to delete file {}!", p.file)); + match fs::remove_file(format!("./pasta_data/{}/{}", p.id_as_animals(), p.file)) { + Ok(_) => {} + Err(_) => { + log::error!("Failed to delete file {}!", p.file) + } + } // and remove the containing directory - fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals())).expect(&*format!( - "Failed to delete directory {}!", - p.id_as_animals() - )); + match fs::remove_dir(format!("./pasta_data/{}/", p.id_as_animals())) { + Ok(_) => {} + Err(_) => { + log::error!("Failed to delete directory {}!", p.file) + } + } // remove false } }); + + dbio::save_to_file(pastas); } pub fn is_valid_url(url: &str) -> bool { diff --git a/templates/header.html b/templates/header.html index 885ad35..d5757ba 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,3 +1,4 @@ + {% if args.footer_text.as_ref().is_none() %}