Fix listing of S3 prefixes not terminated by a slash #3
45
src/main.rs
45
src/main.rs
|
@ -2,6 +2,7 @@
|
||||||
// SPDX-License-Identifier: EUPL-1.2
|
// SPDX-License-Identifier: EUPL-1.2
|
||||||
|
|
||||||
mod settings;
|
mod settings;
|
||||||
|
mod sizes;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
anyhow::Result,
|
anyhow::Result,
|
||||||
|
@ -143,7 +144,7 @@ async fn s3_fileview(path: &PathBuf, settings: &Settings) -> Result<Vec<FileView
|
||||||
path.map(|path| FileViewItem {
|
path.map(|path| FileViewItem {
|
||||||
path: path.to_owned(),
|
path: path.to_owned(),
|
||||||
size_bytes: obj.size,
|
size_bytes: obj.size,
|
||||||
size: size_bytes_to_human(obj.size),
|
size: sizes::bytes_to_human(obj.size),
|
||||||
last_modification: obj.last_modified.clone(),
|
last_modification: obj.last_modified.clone(),
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -156,29 +157,6 @@ async fn s3_fileview(path: &PathBuf, settings: &Settings) -> Result<Vec<FileView
|
||||||
Ok(objects)
|
Ok(objects)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_bytes_to_human(bytes: u64) -> String {
|
|
||||||
use human_size::{Any, SpecificSize};
|
|
||||||
|
|
||||||
let size: f64 = bytes as f64;
|
|
||||||
let digits = size.log10().floor() as u32;
|
|
||||||
let mut order = digits / 3;
|
|
||||||
let unit = match order {
|
|
||||||
0 => Any::Byte,
|
|
||||||
1 => Any::Kilobyte,
|
|
||||||
2 => Any::Megabyte,
|
|
||||||
_ => {
|
|
||||||
order = 3; // Let's stop here.
|
|
||||||
Any::Gigabyte
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
format!(
|
|
||||||
"{:.3}",
|
|
||||||
SpecificSize::new(size / 10u64.pow(order * 3) as f64, unit)
|
|
||||||
.unwrap_or(SpecificSize::new(0., Any::Byte).unwrap())
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
// Workaround for https://github.com/SergioBenitez/Rocket/issues/1792
|
// Workaround for https://github.com/SergioBenitez/Rocket/issues/1792
|
||||||
static ref EMPTY_DIR: tempfile::TempDir = tempfile::tempdir()
|
static ref EMPTY_DIR: tempfile::TempDir = tempfile::tempdir()
|
||||||
|
@ -203,22 +181,3 @@ fn rocket() -> _ {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use rstest::rstest;
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
#[case(1024, "1.024 kB")]
|
|
||||||
#[case(10240, "10.240 kB")]
|
|
||||||
#[case(1024*1024, "1.049 MB")]
|
|
||||||
#[case(1024*1024*1024, "1.074 GB")]
|
|
||||||
#[case(0, "0.000 B")]
|
|
||||||
#[case(u64::MAX, format!("{:.3} GB",u64::MAX as f64/(1_000_000_000.0)))]
|
|
||||||
#[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))]
|
|
||||||
fn size_bytes_to_human(#[case] bytes: u64, #[case] expected: String) {
|
|
||||||
assert_eq!(super::size_bytes_to_human(bytes), expected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
// SPDX-FileCopyrightText: © Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
|
||||||
|
// SPDX-License-Identifier: EUPL-1.2
|
||||||
|
|
||||||
|
pub fn bytes_to_human(bytes: u64) -> String {
|
||||||
|
use human_size::{Any, SpecificSize};
|
||||||
|
|
||||||
|
let size: f64 = bytes as f64;
|
||||||
|
let digits = size.log10().floor() as u32;
|
||||||
|
let mut order = digits / 3;
|
||||||
|
let unit = match order {
|
||||||
|
0 => Any::Byte,
|
||||||
|
1 => Any::Kilobyte,
|
||||||
|
2 => Any::Megabyte,
|
||||||
|
_ => {
|
||||||
|
order = 3; // Let's stop here.
|
||||||
|
Any::Gigabyte
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
format!(
|
||||||
|
"{:.3}",
|
||||||
|
SpecificSize::new(size / 10u64.pow(order * 3) as f64, unit)
|
||||||
|
.unwrap_or(SpecificSize::new(0., Any::Byte).unwrap())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case(1024, "1.024 kB")]
|
||||||
|
#[case(10240, "10.240 kB")]
|
||||||
|
#[case(1024*1024, "1.049 MB")]
|
||||||
|
#[case(1024*1024*1024, "1.074 GB")]
|
||||||
|
#[case(0, "0.000 B")]
|
||||||
|
#[case(u64::MAX, format!("{:.3} GB",u64::MAX as f64/(1_000_000_000.0)))]
|
||||||
|
#[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))]
|
||||||
|
fn bytes_to_human(#[case] bytes: u64, #[case] expected: String) {
|
||||||
|
assert_eq!(super::bytes_to_human(bytes), expected);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue