-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathfile.rs
More file actions
58 lines (48 loc) · 1.5 KB
/
file.rs
File metadata and controls
58 lines (48 loc) · 1.5 KB
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
use std::{collections::HashMap, error::Error, io::Read};
use atomic_lib::resources::PropVals;
use mime_guess::Mime;
pub struct File {
filename: String,
mime: Mime,
bytes: Vec<u8>,
}
impl File {
pub fn open(filename: &str) -> Result<File, Box<dyn Error>> {
let file = std::fs::File::open(filename)?;
let bytes = std::io::BufReader::new(file)
.bytes()
.collect::<Result<Vec<u8>, _>>()?;
let mime = mime_guess::from_path(filename).first_or_octet_stream();
Ok(File {
filename: filename.to_string(),
mime,
bytes,
})
}
pub fn from_filename_bytes(filename: &str, bytes: Vec<u8>) -> Result<File, Box<dyn Error>> {
let mime = mime_guess::from_path(filename).first_or_octet_stream();
Ok(File {
filename: filename.to_string(),
mime,
bytes,
})
}
/// Creates property-value combinations based on the file's contents.
/// Defaults to an empty HashMap if the file type is not supported.
pub fn to_propvals(self) -> PropVals {
match self.mime.to_string().as_str() {
"application/pdf" => crate::pdf::atomize(self),
"image/jpeg" => crate::image::atomize(self),
_ => HashMap::new(),
}
}
pub fn bytes(&mut self) -> Vec<u8> {
self.bytes.clone()
}
pub fn mime(&self) -> &Mime {
&self.mime
}
pub fn filename(&self) -> &str {
&self.filename
}
}