diff --git a/src/handlers/list_files.rs b/src/handlers/list_files.rs index 54cdc79f6739b6fc2775a115e77981b4219a9127..942735b1312690c0eb4f3d6403afb9b71684f0fc 100644 --- a/src/handlers/list_files.rs +++ b/src/handlers/list_files.rs @@ -1,6 +1,6 @@ use salvo::{handler, Request, Response, hyper::header::HOST, prelude::StatusCode}; -use crate::{db, SQLITE, handlers::{TemplateStruct, render_template, convert_file_size, convert_unix_timestamp}}; +use crate::{db, SQLITE, handlers::{TemplateStruct, render_template, convert_file_size, convert_unix_timestamp, count_file_metrics}}; use super::guess_ip; @@ -24,40 +24,66 @@ pub async fn list_files(req: &mut Request, res: &mut Response) { let mut html = String::new(); // For each file in the list, add the html to the rendered string - for f in files.unwrap() { - // Change the filename to a file url - let fileurl = format!("/{}", &f.filename); - - // Change the filesize to a human readable value - let human_size = convert_file_size(f.filesize); - - // Change the expiry to a human readable value - let human_time = convert_unix_timestamp(f.expiry); - - // If isDeleted, change colour to red, else green - let mut colour: String = String::new(); - if f.is_deleted { - colour = "background-color: #ff00000a;".to_string() - } else { - colour = "background-color: #00ff100a;".to_string() + match files.as_ref() { + Ok(file_metrics) => { + for f in file_metrics { + // Change the filename to a file url + let fileurl = format!("/{}", &f.filename); + + // Change the filesize to a human readable value + let human_size = convert_file_size(f.filesize); + + // Change the expiry to a human readable value + let human_time = convert_unix_timestamp(f.expiry); + + // If isDeleted, change colour to red, else green + let mut colour: String = String::new(); + if f.is_deleted { + colour = "background-color: #ff00000a;".to_string() + } else { + colour = "background-color: #00ff100a;".to_string() + } + + // Add a new file to the rendered string + html.push_str(&format!("<tr style='{}'><td><a href={}>{}</a></td><td data-sort='{}'>{}</td><td>{}</td><td>{}</td><td data-sort='{}'>{}</td></tr>", + colour, + fileurl.as_str(), + f.filename.as_str(), + f.filesize, + human_size.as_str(), + f.mimetype.as_str(), + f.views.to_owned(), + f.expiry.to_owned(), + human_time.to_owned(), + )); + } } + Err(err) => { + // Log error to console + tracing::error!("Error retrieving file metrics: {:?}", err); - // Add a new file to the rendered string - html.push_str(&format!("<tr style='{}'><td><a href={}>{}</a></td><td data-sort='{}'>{}</td><td>{}</td><td>{}</td><td data-sort='{}'>{}</td></tr>", - colour, - fileurl.as_str(), - f.filename.as_str(), - f.filesize, - human_size.as_str(), - f.mimetype.as_str(), - f.views.to_owned(), - f.expiry.to_owned(), - human_time.to_owned(), - )); + // Render a nice error page for the poor user + let template_filename = "error.html"; + let template = TemplateStruct { + domain: String::from(headers[HOST].to_str().unwrap()), + message1: String::from("Error 500: Internal Server Error"), + message2: String::from( + "Something went wrong while reading the result from the database, uh oh :(", + ), + ..Default::default() + }; + + let status_code = StatusCode::INTERNAL_SERVER_ERROR; + render_template(res, headers, template_filename, template, status_code).await; + } } + // Close the table when all files are 'rendered' html.push_str("</table>"); + // Log this to the server console + tracing::info!("New Request: /my_files ({} Files", count_file_metrics(&files) ); + let template_filename = "my_files.html"; let template = TemplateStruct { domain: String::from(headers[HOST].to_str().unwrap()), diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 549bda94e321b010577a7e4b7fde0c90e4e844a1..eca8f9423532f8c1c79908a18cd8efb866e3b085 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -10,8 +10,9 @@ use salvo::{ }; use chrono::{DateTime, Utc}; use chrono_humanize::Humanize; +use sqlx::Error; -use crate::CONFIG; +use crate::{CONFIG, db::FileMetric}; // Submodules for each request handler pub mod delete_file; @@ -197,4 +198,13 @@ fn convert_unix_timestamp(unix_timestamp: i64) -> String { let relative_date = datetime.humanize(); relative_date.to_string() +} + +/// Calculate the number of files in the FileMetric result +// I'm lazy so this doesn't really need to be it's own function but it was easier to do this. +fn count_file_metrics(file_metrics: &Result<Vec<FileMetric>, Error>) -> usize { + match file_metrics { + Ok(metrics) => metrics.len(), + Err(_) => 0, + } } \ No newline at end of file