refactor(api): delete pdf file after conversion and rename function download_pdf to get_csv
This commit is contained in:
parent
6b6d1a27d5
commit
b1f6c6cdd5
@ -71,7 +71,7 @@ pub async fn pdf() -> impl IntoResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download_pdf(id: u8) -> Result<String, Error> {
|
async fn get_csv(id: u8) -> Result<String, Error> {
|
||||||
let links = get_pdf().await.unwrap();
|
let links = get_pdf().await.unwrap();
|
||||||
if id as usize >= links.len() {
|
if id as usize >= links.len() {
|
||||||
return Err(failure::err_msg("Invalid ID"));
|
return Err(failure::err_msg("Invalid ID"));
|
||||||
@ -89,7 +89,7 @@ async fn download_pdf(id: u8) -> Result<String, Error> {
|
|||||||
let pdf_path = format!("pdf/{}.pdf", filename);
|
let pdf_path = format!("pdf/{}.pdf", filename);
|
||||||
let csv_path = format!("csv/{}.csv", 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 response = reqwest::get(url).await?;
|
||||||
let body = response.bytes().await?;
|
let body = response.bytes().await?;
|
||||||
std::fs::write(&pdf_path, body)?;
|
std::fs::write(&pdf_path, body)?;
|
||||||
@ -101,6 +101,8 @@ async fn download_pdf(id: u8) -> Result<String, Error> {
|
|||||||
.spawn()
|
.spawn()
|
||||||
.expect("Failed to generate csv file")
|
.expect("Failed to generate csv file")
|
||||||
.wait()?;
|
.wait()?;
|
||||||
|
|
||||||
|
fs::remove_file(&pdf_path)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(csv_path)
|
Ok(csv_path)
|
||||||
@ -118,7 +120,7 @@ pub struct Header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_teacher(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
|
pub async fn get_teacher(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
|
||||||
let filename = match download_pdf(params.id).await {
|
let filename = match get_csv(params.id).await {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
@ -160,7 +162,7 @@ pub struct FilterByClassQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
|
pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
|
||||||
let filename = match download_pdf(params.id).await {
|
let filename = match get_csv(params.id).await {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
@ -249,7 +251,7 @@ pub struct GetTeachersQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoResponse {
|
pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoResponse {
|
||||||
let filename = match download_pdf(params.id).await {
|
let filename = match get_csv(params.id).await {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
@ -300,7 +302,7 @@ fn is_valid_class(class: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_classes(Query(params): Query<GetClassesQuery>) -> impl IntoResponse {
|
pub async fn get_classes(Query(params): Query<GetClassesQuery>) -> impl IntoResponse {
|
||||||
let filename = match download_pdf(params.id).await {
|
let filename = match get_csv(params.id).await {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user