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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![forbid(unsafe_code)]

use chrono::TimeZone;
use flate2::write::GzEncoder;
use flate2::Compression;
use sha2::digest::generic_array::GenericArray;
use sha2::Digest;
use std::io::Write;
use std::path::Path;
use std::time::SystemTime;
use std::{fs, io};

pub struct FileEntry {
    pub rel_path: String,
    pub full_canonical_path: String,
}

#[cfg(not(feature = "include-exclude"))]
pub fn is_path_included(_path: &str, _includes: &[&str], _excludes: &[&str]) -> bool {
    true
}

#[cfg(feature = "include-exclude")]
pub fn is_path_included(rel_path: &str, includes: &[&str], excludes: &[&str]) -> bool {
    use globset::Glob;

    // ignore path matched by exclusion pattern
    for exclude in excludes {
        let pattern = Glob::new(exclude)
            .unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude))
            .compile_matcher();

        if pattern.is_match(rel_path) {
            return false;
        }
    }

    // accept path if no includes provided
    if includes.is_empty() {
        return true;
    }

    // accept path if matched by inclusion pattern
    for include in includes {
        let pattern = Glob::new(include)
            .unwrap_or_else(|_| panic!("invalid include pattern '{}'", include))
            .compile_matcher();

        if pattern.is_match(rel_path) {
            return true;
        }
    }

    false
}

pub fn get_files<'patterns>(
    folder_path: String,
    includes: &'patterns [&str],
    excludes: &'patterns [&str],
) -> impl Iterator<Item = FileEntry> + 'patterns {
    walkdir::WalkDir::new(&folder_path)
        .follow_links(true)
        .sort_by_file_name()
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter_map(move |e| {
            let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
            let full_canonical_path =
                path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));

            let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
                rel_path.replace('\\', "/")
            } else {
                rel_path
            };

            if is_path_included(&rel_path, includes, excludes) {
                Some(FileEntry {
                    rel_path,
                    full_canonical_path,
                })
            } else {
                None
            }
        })
}

/// A file embedded into the binary
pub struct EmbeddedFile {
    pub data: &'static [u8],
    pub data_gzip: Option<&'static [u8]>,
    pub metadata: Metadata,
}

/// A file, read from the file system
pub struct ReadFile {
    pub data: Vec<u8>,
    pub data_gzip: Vec<u8>,
    pub metadata: ReadMetadata,
}

/// Metadata about an embedded file
pub struct Metadata {
    pub hash: &'static str,
    pub etag: &'static str,
    pub last_modified: Option<&'static str>,
    pub last_modified_timestamp: Option<i64>,
    pub mime_type: Option<&'static str>,
}

/// Metadata about a file read from the filesystem
pub struct ReadMetadata {
    pub hash: String,
    pub etag: String,
    pub last_modified: Option<String>,
    pub last_modified_timestamp: Option<i64>,
    pub mime_type: Option<String>,
}

pub fn read_file_from_fs(file_path: &Path) -> io::Result<ReadFile> {
    let data = fs::read(file_path)?;

    // During debugging, use no compression to avoid causing slowdowns. For
    // production, we'll go with default compression: it's usually almost as good
    // as best compression but significantly faster.
    let mut encoder = GzEncoder::new(
        Vec::new(),
        if cfg!(debug_assertions) {
            Compression::fast()
        } else {
            Compression::default()
        },
    );
    encoder.write_all(&data).unwrap();
    let data_gzip = encoder.finish().unwrap();

    let mut hasher = sha2::Sha256::new();
    hasher.update(&data);
    let mut hash_bytes = GenericArray::default();
    hasher.finalize_into(&mut hash_bytes);
    let hash = base85::encode(&hash_bytes[..]);

    let last_modified_timestamp = fs::metadata(file_path)?.modified().ok();
    let last_modified = last_modified_timestamp
        .and_then(|value| {
            value
                .duration_since(SystemTime::UNIX_EPOCH)
                .ok()
                .map(|value| value.as_secs() as i64)
        })
        .or_else(|| {
            last_modified_timestamp
                .and_then(|value| SystemTime::UNIX_EPOCH.duration_since(value).ok())
                .map(|value| (-1) * (value.as_secs() as i64))
        })
        .map(|timestamp| chrono::Utc.timestamp(timestamp, 0));

    let mime_type = new_mime_guess::from_path(file_path)
        .first()
        .map(|mime| mime.to_string());

    Ok(ReadFile {
        data,
        data_gzip,
        metadata: ReadMetadata {
            etag: format!("\"{hash}\""),
            hash,
            last_modified: last_modified.map(|v| v.to_rfc2822()),
            last_modified_timestamp: last_modified.map(|v| v.timestamp()),
            mime_type,
        },
    })
}

fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
    p.as_ref()
        .to_str()
        .expect("Path does not have a string representation")
        .to_owned()
}