aboutsummaryrefslogtreecommitdiff
path: root/src/assets.rs
blob: 36fab55830aaf805a496e8a88cdb25ffa0f334c7 (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
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs;
use std::iter::FromIterator;
use std::fmt::Write;

pub fn get() -> HashMap<String, String> {
    let paths = fs::read_dir("assets").unwrap().map(|e| {
        let path = format!("{}", e.unwrap().path().display());
        let file = fs::read(&path).unwrap();
        let mut path_iter = path.split('/');
        path_iter.next();
        let name = path_iter.collect::<Vec<&str>>().join("/");
        let hashed = format!("/assets/{}/{}", sha256(file), name);
        (name, hashed)
    });
    HashMap::from_iter(paths)
}

fn sha256(input: Vec<u8>) -> String {
    let mut hasher = Sha256::new();
    hasher.update(input);

    let mut res = String::new();
    for byte in hasher.finalize().iter() {
        write!(&mut res, "{:x}", byte).unwrap();
    }
    res
}