From b1f6c6cdd5af9c64ba5b0467b54c5b0d26c619ae Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sun, 15 Sep 2024 12:48:29 +0200 Subject: [PATCH] refactor(api): delete pdf file after conversion and rename function download_pdf to get_csv --- src/api/mod.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index fcfa580..9c95163 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -71,7 +71,7 @@ pub async fn pdf() -> impl IntoResponse { } } -async fn download_pdf(id: u8) -> Result { +async fn get_csv(id: u8) -> Result { let links = get_pdf().await.unwrap(); if id as usize >= links.len() { return Err(failure::err_msg("Invalid ID")); @@ -89,7 +89,7 @@ async fn download_pdf(id: u8) -> Result { let pdf_path = format!("pdf/{}.pdf", filename); let csv_path = format!("csv/{}.csv", filename); - if fs::metadata(&pdf_path).is_err() { + if fs::metadata(&csv_path).is_err() { let response = reqwest::get(url).await?; let body = response.bytes().await?; std::fs::write(&pdf_path, body)?; @@ -101,6 +101,8 @@ async fn download_pdf(id: u8) -> Result { .spawn() .expect("Failed to generate csv file") .wait()?; + + fs::remove_file(&pdf_path)?; } Ok(csv_path) @@ -118,7 +120,7 @@ pub struct Header { } pub async fn get_teacher(Query(params): Query) -> impl IntoResponse { - let filename = match download_pdf(params.id).await { + let filename = match get_csv(params.id).await { Ok(x) => x, Err(_) => { return ( @@ -160,7 +162,7 @@ pub struct FilterByClassQuery { } pub async fn get_class(Query(params): Query) -> impl IntoResponse { - let filename = match download_pdf(params.id).await { + let filename = match get_csv(params.id).await { Ok(x) => x, Err(_) => { return ( @@ -249,7 +251,7 @@ pub struct GetTeachersQuery { } pub async fn get_teachers(Query(params): Query) -> impl IntoResponse { - let filename = match download_pdf(params.id).await { + let filename = match get_csv(params.id).await { Ok(x) => x, Err(_) => { return ( @@ -300,7 +302,7 @@ fn is_valid_class(class: &str) -> bool { } pub async fn get_classes(Query(params): Query) -> impl IntoResponse { - let filename = match download_pdf(params.id).await { + let filename = match get_csv(params.id).await { Ok(x) => x, Err(_) => { return (