diff --git a/src/main.rs b/src/main.rs index e08daec..99a4b11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use bytesize::ByteSize; use log::{error, warn}; use password_hash::PasswordHashString; use serde::{Deserialize, Serialize}; -use state::StateDb; +use state::{StateDb, prelude::SizedFile}; use store::{StoredFile, StoredFiles}; use tokio::fs::File; diff --git a/src/state.rs b/src/state.rs index b7e6635..352971a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,6 @@ use jsondb::JsonDb; -mod prelude { +pub mod prelude { pub use std::collections::HashMap; pub use jsondb::Schema; @@ -8,8 +8,17 @@ mod prelude { pub use serde_with::serde_as; pub use serde_with::skip_serializing_none; pub use time::OffsetDateTime; + + pub trait SizedFile { + fn size(&self) -> u64; + + fn formatted_size(&self) -> String { + bytesize::to_string(self.size(), false).replace(" ", "") + } + } } + mod v0; pub mod v1 { @@ -42,6 +51,9 @@ pub mod v1 { } } } + impl SizedFile for UploadedFile { + fn size(&self) -> u64 { self.size } + } #[derive(Debug, Clone, Deserialize, Serialize)] pub struct FileSet { @@ -70,6 +82,9 @@ pub mod v1 { pub expiry: OffsetDateTime, pub contents: Option, } + impl SizedFile for StoredFile { + fn size(&self) -> u64 { self.size } + } #[derive(Debug, Clone, Deserialize, Serialize)] pub struct StoredFileWithPassword { diff --git a/templates/download.html b/templates/download.html index 1c6e13c..91fbc48 100644 --- a/templates/download.html +++ b/templates/download.html @@ -4,7 +4,7 @@ {% block og_title %}{{ info.file.name }}{% endblock %} {% block og_description -%} - {% let formatted_total_size = bytesize::to_string(info.file.size.clone(), false).replace(" ", "") -%} + {% let formatted_total_size = info.file.formatted_size() -%} {% match info.file.contents -%} {% when Some with (files) -%} {{ files.files.len() }} files, {{ formatted_total_size }} total @@ -26,7 +26,7 @@ {% block body %}
{{ info.file.name }}
-
{{ bytesize::to_string(info.file.size.clone(), false).replace(" ", "") }}
+
{{ info.file.formatted_size() }}
Expires {{ info.file.expiry.format(DATE_DISPLAY_FORMAT).unwrap() }}
@@ -39,7 +39,7 @@ {% let offsets = info.offsets.as_ref().unwrap() %} {% for f in files.files %} - {{ bytesize::to_string(f.size.clone(), false).replace(" ", "") }} + {{ f.formatted_size() }} {{ f.name }}