Skip to content
Snippets Groups Projects
Verified Commit 8e4b4d6c authored by Volkor Barbarian Warrior's avatar Volkor Barbarian Warrior
Browse files

fix: list the files in a more sensible way

it even generates an nice error!
parent 2b7251fd
No related branches found
No related tags found
No related merge requests found
use salvo::{handler, Request, Response, hyper::header::HOST, prelude::StatusCode}; 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; use super::guess_ip;
...@@ -24,40 +24,66 @@ pub async fn list_files(req: &mut Request, res: &mut Response) { ...@@ -24,40 +24,66 @@ pub async fn list_files(req: &mut Request, res: &mut Response) {
let mut html = String::new(); let mut html = String::new();
// For each file in the list, add the html to the rendered string // For each file in the list, add the html to the rendered string
for f in files.unwrap() { match files.as_ref() {
// Change the filename to a file url Ok(file_metrics) => {
let fileurl = format!("/{}", &f.filename); for f in file_metrics {
// Change the filename to a file url
// Change the filesize to a human readable value let fileurl = format!("/{}", &f.filename);
let human_size = convert_file_size(f.filesize);
// Change the filesize to a human readable value
// Change the expiry to a human readable value let human_size = convert_file_size(f.filesize);
let human_time = convert_unix_timestamp(f.expiry);
// Change the expiry to a human readable value
// If isDeleted, change colour to red, else green let human_time = convert_unix_timestamp(f.expiry);
let mut colour: String = String::new();
if f.is_deleted { // If isDeleted, change colour to red, else green
colour = "background-color: #ff00000a;".to_string() let mut colour: String = String::new();
} else { if f.is_deleted {
colour = "background-color: #00ff100a;".to_string() 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 // Render a nice error page for the poor user
html.push_str(&format!("<tr style='{}'><td><a href={}>{}</a></td><td data-sort='{}'>{}</td><td>{}</td><td>{}</td><td data-sort='{}'>{}</td></tr>", let template_filename = "error.html";
colour, let template = TemplateStruct {
fileurl.as_str(), domain: String::from(headers[HOST].to_str().unwrap()),
f.filename.as_str(), message1: String::from("Error 500: Internal Server Error"),
f.filesize, message2: String::from(
human_size.as_str(), "Something went wrong while reading the result from the database, uh oh :(",
f.mimetype.as_str(), ),
f.views.to_owned(), ..Default::default()
f.expiry.to_owned(), };
human_time.to_owned(),
)); 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' // Close the table when all files are 'rendered'
html.push_str("</table>"); 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_filename = "my_files.html";
let template = TemplateStruct { let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()), domain: String::from(headers[HOST].to_str().unwrap()),
......
...@@ -10,8 +10,9 @@ use salvo::{ ...@@ -10,8 +10,9 @@ use salvo::{
}; };
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use chrono_humanize::Humanize; use chrono_humanize::Humanize;
use sqlx::Error;
use crate::CONFIG; use crate::{CONFIG, db::FileMetric};
// Submodules for each request handler // Submodules for each request handler
pub mod delete_file; pub mod delete_file;
...@@ -197,4 +198,13 @@ fn convert_unix_timestamp(unix_timestamp: i64) -> String { ...@@ -197,4 +198,13 @@ fn convert_unix_timestamp(unix_timestamp: i64) -> String {
let relative_date = datetime.humanize(); let relative_date = datetime.humanize();
relative_date.to_string() 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment