diff options
author | Daniel GarcĂa <[email protected]> | 2024-01-27 02:43:26 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-01-27 02:43:26 +0100 |
commit | edf7484a70942de387d59c5d5cf849f24180ba66 (patch) | |
tree | b5cd3c96048e8bd77c895a099f9fbca147ac244e /src/util.rs | |
parent | 8b66e3441571fdb71f0610882d1ee47dccc8c0a3 (diff) | |
download | vaultwarden-edf7484a70942de387d59c5d5cf849f24180ba66.tar.gz vaultwarden-edf7484a70942de387d59c5d5cf849f24180ba66.zip |
Improve file limit handling (#4242)
* Improve file limit handling
* Oops
* Update PostgreSQL migration
* Review comments
---------
Co-authored-by: BlackDex <[email protected]>
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/util.rs b/src/util.rs index b1535301..a49682dd 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,6 +7,7 @@ use std::{ ops::Deref, }; +use num_traits::ToPrimitive; use rocket::{ fairing::{Fairing, Info, Kind}, http::{ContentType, Header, HeaderMap, Method, Status}, @@ -367,10 +368,14 @@ pub fn delete_file(path: &str) -> IOResult<()> { fs::remove_file(path) } -pub fn get_display_size(size: i32) -> String { +pub fn get_display_size(size: i64) -> String { const UNITS: [&str; 6] = ["bytes", "KB", "MB", "GB", "TB", "PB"]; - let mut size: f64 = size.into(); + // If we're somehow too big for a f64, just return the size in bytes + let Some(mut size) = size.to_f64() else { + return format!("{size} bytes"); + }; + let mut unit_counter = 0; loop { @@ -638,6 +643,47 @@ fn _process_key(key: &str) -> String { } } +#[derive(Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum NumberOrString { + Number(i64), + String(String), +} + +impl NumberOrString { + pub fn into_string(self) -> String { + match self { + NumberOrString::Number(n) => n.to_string(), + NumberOrString::String(s) => s, + } + } + + #[allow(clippy::wrong_self_convention)] + pub fn into_i32(&self) -> Result<i32, crate::Error> { + use std::num::ParseIntError as PIE; + match self { + NumberOrString::Number(n) => match n.to_i32() { + Some(n) => Ok(n), + None => err!("Number does not fit in i32"), + }, + NumberOrString::String(s) => { + s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())) + } + } + } + + #[allow(clippy::wrong_self_convention)] + pub fn into_i64(&self) -> Result<i64, crate::Error> { + use std::num::ParseIntError as PIE; + match self { + NumberOrString::Number(n) => Ok(*n), + NumberOrString::String(s) => { + s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())) + } + } + } +} + // // Retry methods // |