Skip to content

Commit 5f8796e

Browse files
AntosserLeoBorai
andauthored
fix: implementation of file size formatting (#379)
This PR removes `src/utils/fmt.rs`, since it only had the `format_bytes` function, which can also be used from another library. The implementation of `format_bytes` is also incorrect. `1024 bytes` is not equal to `1KB`, as the test indicates. `1024 bytes` is equal to `1KiB`, and means `Kibibyte`, where `bi` stands for binary. Why not just use a library for that? That would eliminate such bugs. The PR changes the file size type from `String` to `u64` and adds a `_bytes` postfix to the property name. ```diff - pub(crate) size: String, + pub(crate) size_bytes: u64, ``` I would love to store the file size as the `Size` struct provided by the `size` crate and remove the postfix, but `Size` doesn't implement the `Serialize` trait, and we'd have to wait for that before we can do that. --------- Co-authored-by: Esteban Borai <estebanborai@gmail.com>
1 parent 70fc494 commit 5f8796e

File tree

7 files changed

+31
-47
lines changed

7 files changed

+31
-47
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ serde = { version = "1.0.192", features = ["derive"] }
4949
serde_json = "1.0.108"
5050
structopt = { version = "0.3.26", default-features = false }
5151
termcolor = "1.1.3"
52-
tokio = { version = "1.29.1", features = ["fs", "rt-multi-thread", "signal", "macros"] }
52+
tokio = { version = "1.29.1", features = [
53+
"fs",
54+
"rt-multi-thread",
55+
"signal",
56+
"macros",
57+
] }
5358
tokio-rustls = "0.23.4"
5459
toml = "0.7.6"
60+
humansize = "2.1.3"
5561

5662
[dev-dependencies]
5763
criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] }

src/addon/file_server/directory_entry.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::cmp::{Ord, Ordering};
99
pub struct DirectoryEntry {
1010
pub(crate) display_name: String,
1111
pub(crate) is_dir: bool,
12-
pub(crate) size: String,
13-
pub(crate) len: u64,
12+
pub(crate) size_bytes: u64,
1413
pub(crate) entry_path: String,
1514
pub(crate) date_created: Option<DateTime<Local>>,
1615
pub(crate) date_modified: Option<DateTime<Local>>,

src/addon/file_server/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod scoped_file_system;
77
use chrono::{DateTime, Local};
88

99
pub use file::{File, FILE_BUFFER_SIZE};
10+
use humansize::{format_size, DECIMAL};
1011
pub use scoped_file_system::{Directory, Entry, ScopedFileSystem};
1112

1213
use anyhow::{Context, Result};
@@ -20,7 +21,6 @@ use std::path::{Component, Path, PathBuf};
2021
use std::str::FromStr;
2122
use std::sync::Arc;
2223

23-
use crate::utils::fmt::format_bytes;
2424
use crate::utils::url_encode::{decode_uri, encode_uri, PERCENT_ENCODE_SET};
2525

2626
use self::directory_entry::{BreadcrumbItem, DirectoryEntry, DirectoryIndex, Sort};
@@ -68,6 +68,9 @@ impl<'a> FileServer {
6868
});
6969
handlebars.register_helper("date", Box::new(date));
7070

71+
handlebars_helper!(size: |bytes: u64| format_size(bytes, DECIMAL));
72+
handlebars.register_helper("size", Box::new(size));
73+
7174
handlebars_helper!(sort_name: |sort: Sort| sort == Sort::Name);
7275
handlebars.register_helper("sort_name", Box::new(sort_name));
7376

@@ -264,8 +267,7 @@ impl<'a> FileServer {
264267
.context("Unable to gather file name into a String")?
265268
.to_string(),
266269
is_dir: metadata.is_dir(),
267-
size: format_bytes(metadata.len() as f64),
268-
len: metadata.len(),
270+
size_bytes: metadata.len(),
269271
entry_path: FileServer::make_dir_entry_link(&root_dir, &entry.path()),
270272
date_created,
271273
date_modified,
@@ -278,7 +280,7 @@ impl<'a> FileServer {
278280
SortBy::Name => {
279281
directory_entries.sort_by_key(|entry| entry.display_name.clone());
280282
}
281-
SortBy::Size => directory_entries.sort_by_key(|entry| entry.len),
283+
SortBy::Size => directory_entries.sort_by_key(|entry| entry.size_bytes),
282284
SortBy::DateCreated => {
283285
directory_entries.sort_by_key(|entry| entry.date_created)
284286
}

src/addon/file_server/template/explorer.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
{{/if}}
224224
</span>
225225
<span class="bcol">{{display_name}}</span>
226-
<span class="bcol">{{size}}</span>
226+
<span class="bcol">{{size size_bytes}}</span>
227227
<span class="bcol">{{date date_created}}</span>
228228
<span class="bcol">{{date date_modified}}</span>
229229
</a>

src/utils/fmt.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/utils/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod error;
2-
pub mod fmt;
32
pub mod signal;
43
pub mod url_encode;

0 commit comments

Comments
 (0)