From 95dacb20b475615f9960d8facff59dfe78637b1d Mon Sep 17 00:00:00 2001 From: neri Date: Fri, 24 Sep 2021 22:51:13 +0200 Subject: [PATCH] fix panic when files table is empty --- init-db.sql | 1 + src/deleter.rs | 8 ++++---- src/multipart.rs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/init-db.sql b/init-db.sql index 1fdcab7..55c5ff2 100644 --- a/init-db.sql +++ b/init-db.sql @@ -8,3 +8,4 @@ CREATE TABLE IF NOT EXISTS files ( ); ALTER TABLE files ADD COLUMN IF NOT EXISTS delete_on_download boolean; +ALTER TABLE files ALTER COLUMN delete_on_download set not null; diff --git a/src/deleter.rs b/src/deleter.rs index 39e3ed0..8fe1192 100644 --- a/src/deleter.rs +++ b/src/deleter.rs @@ -54,13 +54,13 @@ async fn delete_content(file_id: &str, files_dir: &Path) -> Result<(), std::io:: } async fn wait_for_file_expiry(receiver: &Receiver<()>, db: &PgPool) { - let valid_till: Option<(NaiveDateTime,)> = + let valid_till: (Option,) = sqlx::query_as("SELECT MIN(valid_till) as min from files") - .fetch_optional(db) + .fetch_one(db) .await .expect("could not fetch expiring files from database"); - let next_timeout = match valid_till { - Some((valid_till,)) => valid_till.signed_duration_since(Local::now().naive_local()), + let next_timeout = match valid_till.0 { + Some(valid_till) => valid_till.signed_duration_since(Local::now().naive_local()), None => Duration::days(1), }; let positive_timeout = next_timeout diff --git a/src/multipart.rs b/src/multipart.rs index 7656917..4d58872 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -53,7 +53,7 @@ pub(crate) async fn parse_multipart( size = create_file(file_name, field, config.max_file_size).await?; } "delete_on_download" => { - delete_on_download = dbg!(parse_string(name, field).await?) != "false"; + delete_on_download = parse_string(name, field).await? != "false"; } "password" => { password = Some(parse_string(name, field).await?);