aboutsummaryrefslogtreecommitdiff
path: root/src/model.rs
blob: 12b0433618d069df9e2cdee2ceca5c6922f6fd6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
use rand::{distributions::Alphanumeric, Rng};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
    pub id: String,
    pub name: String,
    pub expires_at: DateTime<Local>,
    pub content_length: usize,
}

pub fn local_time() -> DateTime<Local> {
    let dt = Local::now();
    match decode_datetime(&encode_datetime(dt)) {
        Some(res) => res,
        None => dt,
    }
}

// Using 28 Base 62 characters, which corresponds to 166 bits of entropy
// https://owasp.org/www-community/vulnerabilities/Insufficient_Session-ID_Length
// https://www.rfc-editor.org/rfc/rfc6749.html#section-10.10
const FILE_ID_CHARS: usize = 28;

pub fn generate_file_id() -> String {
    rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(FILE_ID_CHARS)
        .map(char::from)
        .collect()
}

const FORMAT: &str = "%Y-%m-%d %H:%M:%S";

pub fn encode_datetime(dt: DateTime<Local>) -> String {
    dt.naive_utc().format(FORMAT).to_string()
}

pub fn decode_datetime(str: &str) -> Option<DateTime<Local>> {
    let naive_time = NaiveDateTime::parse_from_str(str, FORMAT).ok()?;
    Some(Local.from_utc_datetime(&naive_time))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_datetime_serialization() {
        let dt = local_time();
        assert_eq!(decode_datetime(&encode_datetime(dt)), Some(dt))
    }
}