From 2ff464915c917c3d7f85de982230781c58159e04 Mon Sep 17 00:00:00 2001 From: neri Date: Fri, 10 Sep 2021 17:44:37 +0200 Subject: [PATCH] simpify db query code --- src/download.rs | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/download.rs b/src/download.rs index 13f63db..9783c26 100644 --- a/src/download.rs +++ b/src/download.rs @@ -7,12 +7,8 @@ use actix_web::{ web, Error, HttpRequest, HttpResponse, }; use async_std::{fs, path::Path}; -use futures::TryStreamExt; use mime::Mime; -use sqlx::{ - postgres::{PgPool, PgRow}, - Row, -}; +use sqlx::postgres::PgPool; use url::Url; use crate::deleter; @@ -54,24 +50,17 @@ async fn load_file_info( id: &str, db: &web::Data>, ) -> Result<(String, String, String, bool), Error> { - let mut rows = sqlx::query( + sqlx::query_as( "SELECT file_id, file_name, kind, delete_on_download from files WHERE file_id = $1", ) .bind(id) - .fetch(db.as_ref()); - let row: PgRow = rows - .try_next() - .await - .map_err(|db_err| { - log::error!("could not run select statement {:?}", db_err); - error::ErrorInternalServerError("could not run select statement") - })? - .ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?; - let file_id: String = row.get("file_id"); - let file_name: String = row.get("file_name"); - let file_kind: String = row.get("kind"); - let delete_on_download: bool = row.get("delete_on_download"); - Ok((file_id, file_name, file_kind, delete_on_download)) + .fetch_optional(db.as_ref()) + .await + .map_err(|db_err| { + log::error!("could not run select statement {:?}", db_err); + error::ErrorInternalServerError("could not run select statement") + })? + .ok_or_else(|| error::ErrorNotFound("file does not exist or has expired")) } fn get_content_type(path: &Path) -> Mime {