Skip to content

Commit

Permalink
fix: implementation of file size formatting (#379)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Antosser and EstebanBorai committed Nov 15, 2023
1 parent 70fc494 commit 5f8796e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 47 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
structopt = { version = "0.3.26", default-features = false }
termcolor = "1.1.3"
tokio = { version = "1.29.1", features = ["fs", "rt-multi-thread", "signal", "macros"] }
tokio = { version = "1.29.1", features = [
"fs",
"rt-multi-thread",
"signal",
"macros",
] }
tokio-rustls = "0.23.4"
toml = "0.7.6"
humansize = "2.1.3"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] }
Expand Down
3 changes: 1 addition & 2 deletions src/addon/file_server/directory_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use std::cmp::{Ord, Ordering};
pub struct DirectoryEntry {
pub(crate) display_name: String,
pub(crate) is_dir: bool,
pub(crate) size: String,
pub(crate) len: u64,
pub(crate) size_bytes: u64,
pub(crate) entry_path: String,
pub(crate) date_created: Option<DateTime<Local>>,
pub(crate) date_modified: Option<DateTime<Local>>,
Expand Down
10 changes: 6 additions & 4 deletions src/addon/file_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod scoped_file_system;
use chrono::{DateTime, Local};

pub use file::{File, FILE_BUFFER_SIZE};
use humansize::{format_size, DECIMAL};
pub use scoped_file_system::{Directory, Entry, ScopedFileSystem};

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

use crate::utils::fmt::format_bytes;
use crate::utils::url_encode::{decode_uri, encode_uri, PERCENT_ENCODE_SET};

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

handlebars_helper!(size: |bytes: u64| format_size(bytes, DECIMAL));
handlebars.register_helper("size", Box::new(size));

handlebars_helper!(sort_name: |sort: Sort| sort == Sort::Name);
handlebars.register_helper("sort_name", Box::new(sort_name));

Expand Down Expand Up @@ -264,8 +267,7 @@ impl<'a> FileServer {
.context("Unable to gather file name into a String")?
.to_string(),
is_dir: metadata.is_dir(),
size: format_bytes(metadata.len() as f64),
len: metadata.len(),
size_bytes: metadata.len(),
entry_path: FileServer::make_dir_entry_link(&root_dir, &entry.path()),
date_created,
date_modified,
Expand All @@ -278,7 +280,7 @@ impl<'a> FileServer {
SortBy::Name => {
directory_entries.sort_by_key(|entry| entry.display_name.clone());
}
SortBy::Size => directory_entries.sort_by_key(|entry| entry.len),
SortBy::Size => directory_entries.sort_by_key(|entry| entry.size_bytes),
SortBy::DateCreated => {
directory_entries.sort_by_key(|entry| entry.date_created)
}
Expand Down
2 changes: 1 addition & 1 deletion src/addon/file_server/template/explorer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
{{/if}}
</span>
<span class="bcol">{{display_name}}</span>
<span class="bcol">{{size}}</span>
<span class="bcol">{{size size_bytes}}</span>
<span class="bcol">{{date date_created}}</span>
<span class="bcol">{{date date_modified}}</span>
</a>
Expand Down
38 changes: 0 additions & 38 deletions src/utils/fmt.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod error;
pub mod fmt;
pub mod signal;
pub mod url_encode;

0 comments on commit 5f8796e

Please sign in to comment.