Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Accept KB sizes in file list filters

laurensprieme5ddd0d2ad01
diff --git a/docs/docs/cli/photos.md b/docs/docs/cli/photos.md
index 57fc86c2f4..aa775a3733 100644
--- a/docs/docs/cli/photos.md
+++ b/docs/docs/cli/photos.md
@@ -103,7 +103,7 @@ ente photos file list --type video --min-size 500MB
- `--since` and `--before` compare `createdAt`, which includes dates edited in Ente. `--since` includes the given instant and `--before` excludes it. Both accept `YYYY-MM-DD` (midnight UTC) or RFC 3339 with a timezone, such as `2024-06-01T00:00:00+02:00`.
- `--search` finds files where every word occurs in either the name or the caption, ignoring case.
- `--no-location` keeps files without a location.
-- `--min-size` and `--max-size` keep files whose `sizeBytes` is at least or at most the given size. Both accept MB, GB or TB in powers of 1024, such as `500MB` or `1.5GB`. `sizeBytes` is the size of the stored encrypted file, slightly larger than the original. Files with an unknown size are left out.
+- `--min-size` and `--max-size` keep files whose `sizeBytes` is at least or at most the given size. Both accept KB, MB or GB in powers of 1024, such as `500KB`, `100MB` or `1.5GB`. `sizeBytes` is the size of the stored encrypted file, slightly larger than the original. Files with an unknown size are left out.
A file must match every filter you pass. `--sort created` lists the newest files first and `--sort size` the largest first; add `--reverse` for the opposite order. Files with an unknown size sort as the smallest, and ties keep ID order. Without `--sort`, files are listed by ID.
diff --git a/rust/apps/cli/src/admin.rs b/rust/apps/cli/src/admin.rs
index 8e0ce24e04..c97600e827 100644
--- a/rust/apps/cli/src/admin.rs
+++ b/rust/apps/cli/src/admin.rs
@@ -264,15 +264,36 @@ fn prompt_change(label: &str, parse: fn(&str) -> Result<i64, String>) -> Result<
}
pub fn parse_size(input: &str) -> Result<i64, String> {
+ parse_scaled_size(
+ input,
+ &[
+ ("MB", 1024_i64.pow(2)),
+ ("GB", 1024_i64.pow(3)),
+ ("TB", 1024_i64.pow(4)),
+ ],
+ "use a size such as 500GB or 1.5TB (MB/GB/TB use powers of 1024)",
+ )
+}
+
+pub fn parse_file_size(input: &str) -> Result<u64, String> {
+ let bytes = parse_scaled_size(
+ input,
+ &[
+ ("KB", 1024_i64),
+ ("MB", 1024_i64.pow(2)),
+ ("GB", 1024_i64.pow(3)),
+ ],
+ "use a size such as 500MB or 1.5GB (KB/MB/GB use powers of 1024)",
+ )?;
+ u64::try_from(bytes).map_err(|error| error.to_string())
+}
+
+fn parse_scaled_size(input: &str, units: &[(&str, i64)], hint: &str) -> Result<i64, String> {
let input = input.to_ascii_uppercase();
- let (number, multiplier) = [
- ("MB", 1024_i64.pow(2)),
- ("GB", 1024_i64.pow(3)),
- ("TB", 1024_i64.pow(4)),
- ]
- .into_iter()
- .find_map(|(unit, multiplier)| input.strip_suffix(unit).map(|number| (number, multiplier)))
- .ok_or("use a size such as 500GB or 1.5TB (MB/GB/TB use powers of 1024)")?;
+ let (number, multiplier) = units
+ .iter()
+ .find_map(|&(unit, multiplier)| input.strip_suffix(unit).map(|number| (number, multiplier)))
+ .ok_or(hint)?;
let value = number.parse::<f64>().map_err(|_| "invalid size")?;
let bytes = value * multiplier as f64;
if !number.chars().all(|c| c.is_ascii_digit() || c == '.')
@@ -298,7 +319,7 @@ pub fn parse_date(input: &str) -> Result<i64, String> {
#[cfg(test)]
mod tests {
- use super::{parse_date, parse_size};
+ use super::{parse_date, parse_file_size, parse_size};
#[test]
fn storage_uses_go_units_and_dates_use_utc_or_explicit_offsets() {
@@ -309,7 +330,15 @@ mod tests {
] {
assert_eq!(parse_size(input).unwrap(), bytes);
}
- for input in ["0GB", "-1GB", "1e2GB", "1GiB", "NaNGB", "999999999999TB"] {
+ for input in [
+ "0GB",
+ "-1GB",
+ "1e2GB",
+ "1GiB",
+ "NaNGB",
+ "999999999999TB",
+ "500KB",
+ ] {
assert!(parse_size(input).is_err(), "{input}");
}
for input in [
@@ -323,4 +352,19 @@ mod tests {
assert!(parse_date(input).is_err(), "{input}");
}
}
+
+ #[test]
+ fn file_sizes_use_kb_mb_and_gb() {
+ for (input, bytes) in [
+ ("3KB", 3072),
+ ("1.5kb", 1536),
+ ("5MB", 5242880),
+ ("1.5gB", 1610612736),
+ ] {
+ assert_eq!(parse_file_size(input).unwrap(), bytes);
+ }
+ for input in ["0KB", "1TB", "1KiB", "100B", "0.0005KB"] {
+ assert!(parse_file_size(input).is_err(), "{input}");
+ }
+ }
}
diff --git a/rust/apps/cli/src/args.rs b/rust/apps/cli/src/args.rs
index 8e775d7653..233bd6cd55 100644
--- a/rust/apps/cli/src/args.rs
+++ b/rust/apps/cli/src/args.rs
@@ -619,17 +619,17 @@ pub struct FileQuery {
#[arg(
long,
value_name = "SIZE",
- value_parser = file_size,
+ value_parser = crate::admin::parse_file_size,
help = "Include files of at least this size, e.g. 100MB",
- long_help = "Include files whose sizeBytes is at least SIZE. Accepts MB, GB and TB, case-insensitive, using powers of 1024; fractions are allowed, such as 0.5MB. Files with an unknown size are left out."
+ long_help = "Include files whose sizeBytes is at least SIZE. Accepts KB, MB and GB, case-insensitive, using powers of 1024; fractions are allowed, such as 1.5GB. Files with an unknown size are left out."
)]
pub min_size: Option<u64>,
#[arg(
long,
value_name = "SIZE",
- value_parser = file_size,
+ value_parser = crate::admin::parse_file_size,
help = "Include files of at most this size, e.g. 1.5GB",
- long_help = "Include files whose sizeBytes is at most SIZE. Accepts MB, GB and TB, case-insensitive, using powers of 1024; fractions are allowed, such as 0.5MB. Files with an unknown size are left out."
+ long_help = "Include files whose sizeBytes is at most SIZE. Accepts KB, MB and GB, case-insensitive, using powers of 1024; fractions are allowed, such as 1.5GB. Files with an unknown size are left out."
)]
pub max_size: Option<u64>,
#[arg(
@@ -666,11 +666,6 @@ fn nonempty_search(text: &str) -> Result<String, String> {
}
}
-fn file_size(input: &str) -> Result<u64, String> {
- let bytes = crate::admin::parse_size(input)?;
- u64::try_from(bytes).map_err(|error| error.to_string())
-}
-
const ACCOUNT_HELP: &str = "Reads accounts stored on this device without network access.
With --json, list returns an array and view returns one object.
Fields: id, name, email, host (strings); apps (string array); selected (boolean).";
diff --git a/rust/apps/cli/src/output.rs b/rust/apps/cli/src/output.rs
index 8c44b0b674..65ebaafd3f 100644
--- a/rust/apps/cli/src/output.rs
+++ b/rust/apps/cli/src/output.rs
@@ -315,7 +315,7 @@ pub fn file(file: &FileView) -> Result<()> {
"Size",
file.size_bytes
.and_then(|bytes| i64::try_from(bytes).ok())
- .map(storage_size),
+ .map(file_size),
),
("Hash", file.hash.as_deref().map(escape_controls)),
] {
@@ -366,11 +366,29 @@ pub fn admin_user(details: &UserDetails, as_json: bool) -> Result<()> {
}
pub fn storage_size(bytes: i64) -> String {
- for (unit, divisor) in [
- ("TB", 1024_i64.pow(4)),
- ("GB", 1024_i64.pow(3)),
- ("MB", 1024_i64.pow(2)),
- ] {
+ scaled_size(
+ bytes,
+ &[
+ ("TB", 1024_i64.pow(4)),
+ ("GB", 1024_i64.pow(3)),
+ ("MB", 1024_i64.pow(2)),
+ ],
+ )
+}
+
+fn file_size(bytes: i64) -> String {
+ scaled_size(
+ bytes,
+ &[
+ ("GB", 1024_i64.pow(3)),
+ ("MB", 1024_i64.pow(2)),
+ ("KB", 1024_i64),
+ ],
+ )
+}
+
+fn scaled_size(bytes: i64, units: &[(&str, i64)]) -> String {
+ for &(unit, divisor) in units {
if bytes >= divisor {
let size = format!("{:.2}", bytes as f64 / divisor as f64);
return format!(
diff --git a/rust/apps/cli/tests/cli/admin.rs b/rust/apps/cli/tests/cli/admin.rs
index d7ef477af2..03d1b2fd12 100644
--- a/rust/apps/cli/tests/cli/admin.rs
+++ b/rust/apps/cli/tests/cli/admin.rs
@@ -322,6 +322,10 @@ fn admin_email_and_input_contract() {
"cannot be used with '--expires <WHEN>'",
),
(vec!["--storage", "2GiB"], "use a size such as"),
+ (
+ vec!["--storage", "500KB"],
+ "use a size such as 500GB or 1.5TB",
+ ),
(vec!["--expires", "2030-01-01 12:00:00"], "use YYYY-MM-DD"),
] {
let mut args = vec!["admin", "user", "update-subscription", EMAIL];
diff --git a/rust/apps/cli/tests/cli/files.rs b/rust/apps/cli/tests/cli/files.rs
index dc012c6f8b..1d1893539b 100644
--- a/rust/apps/cli/tests/cli/files.rs
+++ b/rust/apps/cli/tests/cli/files.rs
@@ -693,6 +693,8 @@ fn file_lists_filter_and_sort_synced_metadata() {
(vec!["--min-size", "0.5mb"], vec![2]),
(vec!["--max-size", "5MB"], vec![1, 2, 4]),
(vec!["--max-size", "1MB"], vec![1, 4]),
+ (vec!["--min-size", "1kb"], vec![1, 2, 4]),
+ (vec!["--max-size", "2KB"], vec![1, 4]),
(vec!["--min-size", "1MB", "--max-size", "4MB"], vec![]),
(vec!["--max-size", "1MB", "--sort", "created"], vec![4, 1]),
(vec!["--sort", "created"], vec![3, 2, 4, 1]),
@@ -727,7 +729,7 @@ fn file_lists_filter_and_sort_synced_metadata() {
assert!(lines.next().unwrap().starts_with("2 "));
assert!(String::from_utf8_lossy(&output.stderr).contains("Showing first 1"));
let output = success(home.run(&["--offline", "photos", "file", "view", "1"]));
- assert!(String::from_utf8_lossy(&output.stdout).contains("1200 bytes"));
+ assert!(String::from_utf8_lossy(&output.stdout).contains("1.17 KB (1200 bytes)"));
for args in [
vec!["--since", "yesterday"],
@@ -736,7 +738,8 @@ fn file_lists_filter_and_sort_synced_metadata() {
vec!["--type", "gif"],
vec!["--sort", "name"],
vec!["--reverse"],
- vec!["--min-size", "100KB"],
+ vec!["--min-size", "100B"],
+ vec!["--max-size", "1TB"],
vec!["--max-size", "0MB"],
] {
let mut command = vec!["--offline", "photos", "file", "list"];
diff --git a/rust/apps/cli/tests/cli/museum.rs b/rust/apps/cli/tests/cli/museum.rs
index 559425c519..f3d18dee39 100644
--- a/rust/apps/cli/tests/cli/museum.rs
+++ b/rust/apps/cli/tests/cli/museum.rs
@@ -362,17 +362,10 @@ fn file_lists_filter_and_sort_server_metadata() -> TestResult {
(vec!["--search", "family dinner"], vec![live]),
(vec!["--no-location"], by_id(vec![clip, holiday])),
(vec!["--album", "Family", "--no-location"], vec![holiday]),
- (vec!["--min-size", "0.0015MB"], by_id(vec![clip, holiday])),
- (vec!["--max-size", "0.0015MB"], by_id(vec![beach, live])),
+ (vec!["--min-size", "1.5KB"], by_id(vec![clip, holiday])),
+ (vec!["--max-size", "1.5KB"], by_id(vec![beach, live])),
(
- vec![
- "--album",
- "Trips",
- "--min-size",
- "0.0015MB",
- "--sort",
- "size",
- ],
+ vec!["--album", "Trips", "--min-size", "1.5KB", "--sort", "size"],
vec![clip, holiday],
),
(vec!["--sort", "size"], vec![clip, holiday, live, beach]),