-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.rs
147 lines (129 loc) · 3.79 KB
/
main.rs
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
use anyhow::{anyhow, Context, Result};
use duct::cmd;
use glob::glob;
use rayon::prelude::*;
use std::{
fs,
path::{Path, PathBuf},
};
use which::which;
// Width of images on blog.
const MAX_IMAGE_WIDTH: u32 = 650; // pixels
const INPUT_PATH: &str = "content/**/raw/*";
fn check_deps() -> Result<()> {
for dep in vec!["magick", "cavif", "cwebp"] {
which(dep).with_context(|| format!("Missing binary required for execution: {}", dep))?;
}
Ok(())
}
fn main() -> Result<()> {
check_deps()?;
let entries: Vec<PathBuf> = glob(INPUT_PATH)?.filter_map(Result::ok).collect();
println!("Inspecting {} images", entries.len());
entries.into_par_iter().map(handle).collect::<Vec<_>>();
Ok(())
}
fn copy_original(path: &Path, out_file: &Path) -> Result<()> {
println!("Copying original to {:?}", out_file);
let ext = path
.extension()
.ok_or_else(|| anyhow!("Cannot get extension for {}", path.display()))?;
if !out_file.exists() {
if ext == "svg" || ext == "gif" {
// Simply copy over SVG to target directory for now.
// In the future we could use svgo to optimize here.
fs::copy(path, out_file)?;
} else {
println!(
"magick convert {:?} -strip -resize {}> {:?}",
&path, MAX_IMAGE_WIDTH, &out_file
);
cmd!(
"magick",
"convert",
&path,
"-strip",
"-resize",
MAX_IMAGE_WIDTH.to_string() + ">",
out_file,
)
.run()?;
}
}
if !out_file.with_extension("jpg").exists() {
cmd!(
"magick",
"convert",
&path,
"-strip",
"-interlace",
"JPEG",
"-sharpen",
"0x1.0",
"-quality",
"90%",
"-sampling-factor",
"4:2:0",
"-colorspace",
"RGB",
"-resize",
MAX_IMAGE_WIDTH.to_string() + ">",
out_file.with_extension("jpg")
)
.run()?;
// let output = cmd!("cjpeg", "-quality", "85", "-optimize", out_file)
// .stdout_capture()
// .read()?;
// fs::write(out_file.with_extension("jpg"), output)?;
}
Ok(())
}
fn handle(path: PathBuf) -> Result<()> {
println!("Handling {:?}", path);
let filename = path
.file_name()
.ok_or_else(|| anyhow!("Unexpected file: {}", path.display()))?;
let in_dir = path
.parent()
.ok_or_else(|| anyhow!("Unexpected dir: {}", path.display()))?;
// This is not so beautiful...
let out_dir = Path::new("static").join(
in_dir
.strip_prefix("content/")?
.parent()
.ok_or_else(|| anyhow!("Cannot get parent for {}", in_dir.display()))?,
);
let out_file = out_dir.join(filename);
if filename == ".DS_Store" {
return Ok(());
}
let orig_extension = path
.extension()
.ok_or_else(|| anyhow!("Cannot get extension for {}", path.display()))?;
if orig_extension == "afdesign" {
return Ok(());
}
fs::create_dir_all(&out_dir)?;
copy_original(&path, &out_file)?;
if orig_extension == "svg" || orig_extension == "gif" {
// We're done here.
return Ok(());
}
let webp_file = out_file.with_extension("webp");
if !webp_file.exists() {
cmd!("cwebp", &out_file, "-o", webp_file).run()?;
}
let avif_file = out_file.with_extension("avif");
if !avif_file.exists() {
cmd!(
"cavif",
"--quality=90",
"--overwrite",
"-o",
avif_file,
&out_file
)
.run()?;
}
Ok(())
}