2022-06-03 17:24:34 +01:00
|
|
|
use crate::args::Args;
|
|
|
|
use crate::dbio::save_to_file;
|
|
|
|
use crate::endpoints::errors::ErrorTemplate;
|
|
|
|
use crate::util::animalnumbers::to_u64;
|
2022-11-01 21:15:13 +08:00
|
|
|
use crate::util::hashids::to_u64 as hashid_to_u64;
|
2022-06-03 17:24:34 +01:00
|
|
|
use crate::util::misc::remove_expired;
|
|
|
|
use crate::{AppState, Pasta, ARGS};
|
|
|
|
use actix_multipart::Multipart;
|
|
|
|
use actix_web::{get, post, web, Error, HttpResponse};
|
|
|
|
use askama::Template;
|
|
|
|
use futures::TryStreamExt;
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "edit.html", escape = "none")]
|
|
|
|
struct EditTemplate<'a> {
|
|
|
|
pasta: &'a Pasta,
|
|
|
|
args: &'a Args,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/edit/{id}")]
|
|
|
|
pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
|
|
|
let mut pastas = data.pastas.lock().unwrap();
|
|
|
|
|
2022-11-01 21:15:13 +08:00
|
|
|
let id = if ARGS.hash_ids {
|
|
|
|
hashid_to_u64(&*id).unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
to_u64(&*id.into_inner()).unwrap_or(0)
|
|
|
|
};
|
2022-06-03 17:24:34 +01:00
|
|
|
|
|
|
|
remove_expired(&mut pastas);
|
|
|
|
|
|
|
|
for pasta in pastas.iter() {
|
|
|
|
if pasta.id == id {
|
|
|
|
if !pasta.editable {
|
|
|
|
return HttpResponse::Found()
|
2022-10-12 17:13:21 +02:00
|
|
|
.append_header(("Location", format!("{}/", ARGS.public_path)))
|
2022-06-03 17:24:34 +01:00
|
|
|
.finish();
|
|
|
|
}
|
2022-06-04 21:50:34 +01:00
|
|
|
return HttpResponse::Ok().content_type("text/html").body(
|
2022-06-03 17:24:34 +01:00
|
|
|
EditTemplate {
|
|
|
|
pasta: &pasta,
|
|
|
|
args: &ARGS,
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/edit/{id}")]
|
|
|
|
pub async fn post_edit(
|
|
|
|
data: web::Data<AppState>,
|
|
|
|
id: web::Path<String>,
|
|
|
|
mut payload: Multipart,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
if ARGS.readonly {
|
|
|
|
return Ok(HttpResponse::Found()
|
2022-10-12 17:13:21 +02:00
|
|
|
.append_header(("Location", format!("{}/", ARGS.public_path)))
|
2022-06-03 17:24:34 +01:00
|
|
|
.finish());
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:15:13 +08:00
|
|
|
let id = if ARGS.hash_ids {
|
|
|
|
hashid_to_u64(&*id).unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
to_u64(&*id.into_inner()).unwrap_or(0)
|
|
|
|
};
|
2022-06-03 17:24:34 +01:00
|
|
|
|
|
|
|
let mut pastas = data.pastas.lock().unwrap();
|
|
|
|
|
|
|
|
remove_expired(&mut pastas);
|
|
|
|
|
|
|
|
let mut new_content = String::from("");
|
|
|
|
|
|
|
|
while let Some(mut field) = payload.try_next().await? {
|
|
|
|
match field.name() {
|
|
|
|
"content" => {
|
|
|
|
while let Some(chunk) = field.try_next().await? {
|
|
|
|
new_content = std::str::from_utf8(&chunk).unwrap().to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, pasta) in pastas.iter().enumerate() {
|
|
|
|
if pasta.id == id {
|
|
|
|
if pasta.editable {
|
|
|
|
pastas[i].content.replace_range(.., &*new_content);
|
|
|
|
save_to_file(&pastas);
|
|
|
|
|
|
|
|
return Ok(HttpResponse::Found()
|
2022-11-01 21:15:13 +08:00
|
|
|
.append_header((
|
|
|
|
"Location",
|
|
|
|
format!("{}/pasta/{}", ARGS.public_path, pastas[i].id_as_animals()),
|
|
|
|
))
|
2022-06-03 17:24:34 +01:00
|
|
|
.finish());
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 21:50:34 +01:00
|
|
|
Ok(HttpResponse::Ok()
|
2022-06-03 17:24:34 +01:00
|
|
|
.content_type("text/html")
|
|
|
|
.body(ErrorTemplate { args: &ARGS }.render().unwrap()))
|
|
|
|
}
|