Skip to content

Commit

Permalink
🎨 display static file size to mb kb gb tb pb eb (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
gakaki authored Mar 1, 2024
1 parent 9eb8a0b commit 0044d2f
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion crates/serve-static/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,29 @@ fn list_xml(current: &CurrentInfo) -> String {
ftxt.push_str("</list>");
ftxt
}
fn human_size(x: u64) -> String {
let unit = 1000; // mac unix 1000 windows 1024

let units = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let mut l = 0;
let mut n_float = 0.00; // just for integer part

let mut n = x;
while n >= unit && l < units.len() {
n_float = (n as f64) / (unit as f64);
n /= unit;
l += 1;
}

let part_integer = format!("{}", n);
let mut res = format!("{}", part_integer);
if n_float.fract() != 0.0 {
let s = format!("{}", n_float.fract());
let substring = &s[2..3]; // 0.99 get 9
res = format!("{}.{}", part_integer, substring)
}
format!("{} {}", res, units[l]) // integer part add digit
}
fn list_html(current: &CurrentInfo) -> String {
fn header_links(path: &str) -> String {
let segments = path.trim_start_matches('/').trim_end_matches('/').split('/');
Expand Down Expand Up @@ -557,7 +580,7 @@ fn list_html(current: &CurrentInfo) -> String {
encode_url_path(&file.name),
file.name,
file.modified.format(&format).expect("format time failed"),
file.size
human_size(file.size)
)
.ok();
}
Expand Down Expand Up @@ -611,3 +634,29 @@ const HTML_STYLE: &str = r#"
const DIR_ICON: &str = r#"<svg aria-label="Directory" data-icon="dir" width="20" height="20" viewBox="0 0 512 512" version="1.1" role="img"><path fill="currentColor" d="M464 128H272l-64-64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V176c0-26.51-21.49-48-48-48z"></path></svg>"#;
const FILE_ICON: &str = r#"<svg aria-label="File" data-icon="file" width="20" height="20" viewBox="0 0 384 512" version="1.1" role="img"><path d="M369.9 97.9L286 14C277 5 264.8-.1 252.1-.1H48C21.5 0 0 21.5 0 48v416c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V131.9c0-12.7-5.1-25-14.1-34zM332.1 128H256V51.9l76.1 76.1zM48 464V48h160v104c0 13.3 10.7 24 24 24h104v288H48z"/></svg>"#;
const HOME_ICON: &str = r#"<svg aria-hidden="true" data-icon="home" viewBox="0 0 576 512"><path fill="currentColor" d="M280.37 148.26L96 300.11V464a16 16 0 0 0 16 16l112.06-.29a16 16 0 0 0 15.92-16V368a16 16 0 0 1 16-16h64a16 16 0 0 1 16 16v95.64a16 16 0 0 0 16 16.05L464 480a16 16 0 0 0 16-16V300L295.67 148.26a12.19 12.19 0 0 0-15.3 0zM571.6 251.47L488 182.56V44.05a12 12 0 0 0-12-12h-56a12 12 0 0 0-12 12v72.61L318.47 43a48 48 0 0 0-61 0L4.34 251.47a12 12 0 0 0-1.6 16.9l25.5 31A12 12 0 0 0 45.15 301l235.22-193.74a12.19 12.19 0 0 1 15.3 0L530.9 301a12 12 0 0 0 16.9-1.6l25.5-31a12 12 0 0 0-1.7-16.93z"></path></svg>"#;

#[cfg(test)]
mod tests {
use crate::dir::human_size;

#[tokio::test]
async fn test_convert_bytes_to_units() {
println!("{}", human_size(98595176));// 98.59 MB on mac

let unit = 1000;
println!("{}", human_size(unit)); // 1 KB
println!("{}", human_size(unit - 1)); // 1023.9 B

println!("{}", human_size(unit * unit)); // 1 MB
println!("{}", human_size(unit * unit - 1)); // 1023.9 KB

println!("{}", human_size(unit * unit * unit)); // 1 GB
println!("{}", human_size(unit * unit * unit - 1)); // 1023.9 MB

println!("{}", human_size(unit * unit * unit * unit)); // 1 TB
println!("{}", human_size(unit * unit * unit * unit - 1)); // 1023.9 GB

println!("{}", human_size(unit * unit * unit * unit * unit)); // 1 PB
println!("{}", human_size(unit * unit * unit * unit * unit - 1)); //1023.9 TB
}
}

0 comments on commit 0044d2f

Please sign in to comment.