factor out file size display code

This commit is contained in:
xenofem 2022-11-10 12:41:51 -05:00
parent 920b28f5f5
commit d80180956f
3 changed files with 20 additions and 5 deletions

View file

@ -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;

View file

@ -1,6 +1,6 @@
use jsondb::JsonDb;
mod prelude {
pub mod prelude {
pub use std::collections::HashMap;
pub use jsondb::Schema;
@ -8,7 +8,16 @@ 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;
@ -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<FileSet>,
}
impl SizedFile for StoredFile {
fn size(&self) -> u64 { self.size }
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StoredFileWithPassword {

View file

@ -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 %}
<div id="download_toplevel" class="section">
<div class="file_name">{{ info.file.name }}</div>
<div class="file_size">{{ bytesize::to_string(info.file.size.clone(), false).replace(" ", "") }}</div>
<div class="file_size">{{ info.file.formatted_size() }}</div>
<div class="file_download"><a class="download_button" href="download?code={{ info.code }}&download=all"></a></div>
<div class="file_expiry">Expires {{ info.file.expiry.format(DATE_DISPLAY_FORMAT).unwrap() }}</div>
</div>
@ -39,7 +39,7 @@
{% let offsets = info.offsets.as_ref().unwrap() %}
{% for f in files.files %}
<tr class="{% if offsets.get(loop.index0.clone()).unwrap().clone() > info.available %}unavailable{% endif %}">
<td class="file_size">{{ bytesize::to_string(f.size.clone(), false).replace(" ", "") }}</td>
<td class="file_size">{{ f.formatted_size() }}</td>
<td class="file_name">{{ f.name }}</td>
<td class="file_download"><a class="download_button" href="download?code={{ info.code }}&download={{ loop.index0 }}"></a></td>
<td class="file_unavailable"></td>