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
use std::{path::PathBuf, fs};
use crossbeam_channel::{Receiver, Sender};
use crate::{optimizer::{EntryType, check_file_by_name, ProgressState, StrError, ERR_SIGNFILE}, fop::FileOp, blacklist};
use super::{EntryReader, EntrySaver};
pub struct FSEntryReader {
src_dir: PathBuf
}
impl FSEntryReader {
pub fn new(src_dir: PathBuf) -> Self {
Self { src_dir }
}
}
impl EntryReader for FSEntryReader {
fn read_entries(
self,
tx: Sender<crate::optimizer::EntryType>,
use_blacklist: bool
) -> std::io::Result<()> {
let mut vdir = Vec::new();
vdir.push(self.src_dir.clone());
while let Some(px) = vdir.pop() {
let rd = fs::read_dir(px)?.collect::<Result<Vec<_>, _>>()?;
tx.send(EntryType::Count(rd.len() as u64)).unwrap();
for de in rd {
let meta = de.metadata()?;
if meta.is_dir() {
let dp = de.path();
let dname = dp.strip_prefix(&self.src_dir).expect("Subdir not in source dir")
.to_string_lossy().to_string();
vdir.push(dp);
tx.send(EntryType::Directory(dname)).unwrap();
} else if meta.is_file() {
let fp = de.path();
let fname = fp.strip_prefix(&self.src_dir).expect("File not in source dir")
.to_string_lossy().to_string();
let fop = check_file_by_name(&fname, use_blacklist);
let ff = fs::read(fp)?;
tx.send(EntryType::File(fname, ff, fop)).unwrap();
}
}
}
Ok(())
}
}
pub struct FSEntrySaver {
dest_dir: PathBuf
}
impl FSEntrySaver {
pub fn new(dest_dir: PathBuf) -> Self {
Self { dest_dir }
}
}
impl EntrySaver for FSEntrySaver {
fn save_entries(
self,
rx: Receiver<EntryType>,
ev: &mut dyn crate::errors::ErrorCollector,
ps: &Sender<crate::optimizer::ProgressState>
) -> std::io::Result<i64> {
let mut dsum = 0;
let mut cv = Vec::new();
for et in rx {
match et {
EntryType::Count(u) => {
ps.send(ProgressState::Start(u)).unwrap();
}
EntryType::Directory(dir) => {
let mut dp = self.dest_dir.clone();
dp.push(dir);
fs::create_dir(dp)?;
}
EntryType::File(fname, buf, fop) => {
use FileOp::*;
ps.send(ProgressState::Push(fname.clone())).unwrap();
let mut fp = self.dest_dir.clone();
fp.push(fname.clone());
match fop {
Recompress(_) => {
fs::write(fp, buf)?;
}
Minify(m) => {
let fsz = buf.len() as i64;
let buf = match m.minify(&buf, &mut cv) {
Ok(()) => &cv,
Err(e) => {
ev.collect(&fname, e);
&buf
}
};
dsum -= (buf.len() as i64) - fsz;
fs::write(fp, buf)?;
cv.clear();
}
Ignore => {
ev.collect(&fname, Box::new(blacklist::BlacklistedFile));
}
Warn(x) => {
ev.collect(&fname, Box::new(StrError(x)));
fs::write(fp, buf)?;
}
Signfile => {
ev.collect(&fname, Box::new(StrError(ERR_SIGNFILE.to_string())));
}
}
}
}
}
ps.send(ProgressState::Finish).unwrap();
Ok(dsum)
}
}