Skip to content

Commit

Permalink
Merge pull request #146 from rissson/fix-create-no-expiration-data
Browse files Browse the repository at this point in the history
Fix expiration on create if it's not specified and no_eternal_pasta is set
  • Loading branch information
szabodanika committed Mar 26, 2023
2 parents ded125c + d6f5c0c commit 18e0561
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/endpoints/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ pub async fn index() -> impl Responder {
.body(IndexTemplate { args: &ARGS }.render().unwrap())
}

pub fn expiration_to_timestamp(expiration: &str, timenow: i64) -> i64 {
match expiration {
"1min" => timenow + 60,
"10min" => timenow + 60 * 10,
"1hour" => timenow + 60 * 60,
"24hour" => timenow + 60 * 60 * 24,
"3days" => timenow + 60 * 60 * 24 * 3,
"1week" => timenow + 60 * 60 * 24 * 7,
"never" => {
if ARGS.no_eternal_pasta {
timenow + 60 * 60 * 24 * 7
} else {
0
}
}
_ => {
log::error!("{}", "Unexpected expiration time!");
timenow + 60 * 60 * 24 * 7
}
}
}

pub async fn create(
data: web::Data<AppState>,
mut payload: Multipart,
Expand Down Expand Up @@ -59,7 +81,7 @@ pub async fn create(
burn_after_reads: 0,
last_read: timenow,
pasta_type: String::from(""),
expiration: 0,
expiration: expiration_to_timestamp(&ARGS.default_expiry, timenow),
};

while let Some(mut field) = payload.try_next().await? {
Expand All @@ -76,25 +98,7 @@ pub async fn create(
}
"expiration" => {
while let Some(chunk) = field.try_next().await? {
new_pasta.expiration = match std::str::from_utf8(&chunk).unwrap() {
"1min" => timenow + 60,
"10min" => timenow + 60 * 10,
"1hour" => timenow + 60 * 60,
"24hour" => timenow + 60 * 60 * 24,
"3days" => timenow + 60 * 60 * 24 * 3,
"1week" => timenow + 60 * 60 * 24 * 7,
"never" => {
if ARGS.no_eternal_pasta {
timenow + 60 * 60 * 24 * 7
} else {
0
}
}
_ => {
log::error!("{}", "Unexpected expiration time!");
timenow + 60 * 60 * 24 * 7
}
};
new_pasta.expiration = expiration_to_timestamp(std::str::from_utf8(&chunk).unwrap(), timenow);
}

continue;
Expand Down

0 comments on commit 18e0561

Please sign in to comment.