Merge branch 'master' into szabodanika

This commit is contained in:
Daniel Szabo 2022-11-06 23:10:41 +02:00
commit c6e5c6f018
10 changed files with 91 additions and 18 deletions

View file

@ -1,6 +1,7 @@
use crate::dbio::save_to_file;
use crate::pasta::PastaFile;
use crate::util::animalnumbers::to_animal_names;
use crate::util::hashids::to_hashids;
use crate::util::misc::is_valid_url;
use crate::{AppState, Pasta, ARGS};
use actix_multipart::Multipart;
@ -188,10 +189,12 @@ pub async fn create(
save_to_file(&pastas);
let slug = if ARGS.hash_ids {
to_hashids(id)
} else {
to_animal_names(id)
};
Ok(HttpResponse::Found()
.append_header((
"Location",
format!("{}/pasta/{}", ARGS.public_path, to_animal_names(id)),
))
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, slug)))
.finish())
}

View file

@ -2,6 +2,7 @@ use crate::args::Args;
use crate::dbio::save_to_file;
use crate::endpoints::errors::ErrorTemplate;
use crate::util::animalnumbers::to_u64;
use crate::util::hashids::to_u64 as hashid_to_u64;
use crate::util::misc::remove_expired;
use crate::{AppState, Pasta, ARGS};
use actix_multipart::Multipart;
@ -20,7 +21,11 @@ struct EditTemplate<'a> {
pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
let mut pastas = data.pastas.lock().unwrap();
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
remove_expired(&mut pastas);
@ -59,7 +64,11 @@ pub async fn post_edit(
.finish());
}
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
let mut pastas = data.pastas.lock().unwrap();
@ -85,7 +94,10 @@ pub async fn post_edit(
save_to_file(&pastas);
return Ok(HttpResponse::Found()
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, pastas[i].id_as_animals())))
.append_header((
"Location",
format!("{}/pasta/{}", ARGS.public_path, pastas[i].id_as_animals()),
))
.finish());
} else {
break;

View file

@ -3,6 +3,7 @@ use crate::dbio::save_to_file;
use crate::endpoints::errors::ErrorTemplate;
use crate::pasta::Pasta;
use crate::util::animalnumbers::to_u64;
use crate::util::hashids::to_u64 as hashid_to_u64;
use crate::util::misc::remove_expired;
use crate::AppState;
use actix_web::rt::time;
@ -22,8 +23,11 @@ pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
// get access to the pasta collection
let mut pastas = data.pastas.lock().unwrap();
// get the u64 id from the animal names in the path
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
// remove expired pastas (including this one if needed)
remove_expired(&mut pastas);
@ -83,8 +87,11 @@ pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> Ht
// get access to the pasta collection
let mut pastas = data.pastas.lock().unwrap();
// get the u64 id from the animal names in the path
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
// remove expired pastas (including this one if needed)
remove_expired(&mut pastas);
@ -92,6 +99,7 @@ pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> Ht
// find the index of the pasta in the collection based on u64 id
let mut index: usize = 0;
let mut found: bool = false;
for (i, pasta) in pastas.iter().enumerate() {
if pasta.id == id {
index = i;
@ -146,8 +154,11 @@ pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> St
// get access to the pasta collection
let mut pastas = data.pastas.lock().unwrap();
// get the u64 id from the animal names in the path
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
// remove expired pastas (including this one if needed)
remove_expired(&mut pastas);

View file

@ -4,6 +4,7 @@ use crate::args::ARGS;
use crate::endpoints::errors::ErrorTemplate;
use crate::pasta::PastaFile;
use crate::util::animalnumbers::to_u64;
use crate::util::hashids::to_u64 as hashid_to_u64;
use crate::util::misc::remove_expired;
use crate::AppState;
use askama::Template;
@ -19,20 +20,30 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
let mut pastas = data.pastas.lock().unwrap();
let id = to_u64(&*id.into_inner()).unwrap_or(0);
let id = if ARGS.hash_ids {
hashid_to_u64(&*id).unwrap_or(0)
} else {
to_u64(&*id.into_inner()).unwrap_or(0)
};
for (i, pasta) in pastas.iter().enumerate() {
if pasta.id == id {
// remove the file itself
if let Some(PastaFile { name, .. }) = &pasta.file {
if fs::remove_file(format!("./pasta_data/public/{}/{}", pasta.id_as_animals(), name))
.is_err()
if fs::remove_file(format!(
"./pasta_data/public/{}/{}",
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/public/{}/", pasta.id_as_animals())).is_err() {
if fs::remove_dir(format!("./pasta_data/public/{}/", pasta.id_as_animals()))
.is_err()
{
log::error!("Failed to delete directory {}!", name)
}
}