save delete mode

This commit is contained in:
blek 2023-10-13 09:25:51 +10:00
parent cbe4f379da
commit b281934203
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 28 additions and 4 deletions

View File

@ -18,10 +18,21 @@ pub struct File {
pub name: Option<String>,
pub mime: String,
pub delete_at: DateTime<Local>,
pub delete_mode: DeleteMode,
sha512: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeleteMode {
Time,
TimeOrDownload
}
impl File {
pub fn expired(self: &Self) -> bool {
self.delete_at < chrono::Local::now()
}
pub fn comp_hash(self: &Self, other: &Sha512) -> bool {
let mut hash = other.clone();
hex::encode(hash.finalize_fixed()) == self.sha512
@ -63,7 +74,7 @@ impl File {
Ok(ndata)
}
pub async fn create(data: Vec<u8>, mime: String, name: Option<String>, env: Env) -> Result<File, Box<dyn Error>> {
pub async fn create(data: Vec<u8>, mime: String, name: Option<String>, env: Env, delete_mode: DeleteMode) -> Result<File, Box<dyn Error>> {
let mut filename = String::new();
let mut hash = Sha512::new();
@ -90,6 +101,7 @@ impl File {
name: Some(filename),
mime,
delete_at: expires,
delete_mode,
sha512: hash
}
)

View File

@ -11,7 +11,7 @@ use futures_util::TryStreamExt;
use bytes::BufMut;
use serde::Serialize;
use crate::files::{File, lookup::LookupKind};
use crate::files::{File, lookup::LookupKind, DeleteMode};
use super::{state::SharedState, pages::{BadActionReq, UploadSuccessPage}, rejection::HttpReject};
@ -64,10 +64,15 @@ pub async fn upload(form: FormData, state: SharedState) -> Result<Box<dyn Reply>
}
let data = params.get("file").unwrap();
let _delmode = params.get("delmode").unwrap();
let delmode = params.get("delmode").unwrap();
let named = params.get("named");
let filename = params.get("filename").unwrap();
let mut is_named = named.is_none();
let delmode = delmode.as_str_or_reject()?;
if delmode != "30" && delmode != "dl" {
return Err(warp::reject::custom(HttpReject::StringError("delmode is neither 30 or dl!".into())));
}
if named.is_some() {
is_named = named.unwrap().as_str_or_reject()? == "on";
@ -87,7 +92,14 @@ pub async fn upload(form: FormData, state: SharedState) -> Result<Box<dyn Reply>
},
None => None
},
state.env.clone()
state.env.clone(),
{
if delmode == "30" {
DeleteMode::Time
} else {
DeleteMode::TimeOrDownload
}
}
).await
.map_err(|err| warp::reject::custom(HttpReject::StringError(err.to_string())))?;