-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy paththumbnailer.rs
157 lines (140 loc) · 4.97 KB
/
thumbnailer.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
148
149
150
151
152
153
154
155
156
157
// Copyright 2023 System76 <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
use mime_guess::Mime;
use once_cell::sync::Lazy;
use std::{collections::HashMap, fs, path::Path, process, sync::Mutex, time::Instant};
#[derive(Clone, Debug)]
pub struct Thumbnailer {
pub exec: String,
}
impl Thumbnailer {
pub fn command(
&self,
input: &Path,
output: &Path,
thumbnail_size: u32,
) -> Option<process::Command> {
let args_vec: Vec<String> = shlex::split(&self.exec)?;
let mut args = args_vec.iter();
let mut command = process::Command::new(args.next()?);
for arg in args {
if arg.starts_with('%') {
match arg.as_str() {
"%i" | "%u" => {
command.arg(input);
}
"%o" => {
command.arg(output);
}
"%s" => {
command.arg(format!("{}", thumbnail_size));
}
_ => {
log::warn!(
"unsupported thumbnailer Exec code {:?} in {:?}",
arg,
self.exec
);
return None;
}
}
} else {
command.arg(arg);
}
}
Some(command)
}
}
pub struct ThumbnailerCache {
cache: HashMap<Mime, Vec<Thumbnailer>>,
}
impl ThumbnailerCache {
pub fn new() -> Self {
let mut thumbnailer_cache = Self {
cache: HashMap::new(),
};
thumbnailer_cache.reload();
thumbnailer_cache
}
#[cfg(not(feature = "desktop"))]
pub fn reload(&mut self) {}
#[cfg(feature = "desktop")]
pub fn reload(&mut self) {
let start = Instant::now();
self.cache.clear();
let mut search_dirs = Vec::new();
match xdg::BaseDirectories::new() {
Ok(xdg_dirs) => {
search_dirs.push(xdg_dirs.get_data_home().join("thumbnailers"));
for data_dir in xdg_dirs.get_data_dirs() {
search_dirs.push(data_dir.join("thumbnailers"));
}
}
Err(err) => {
log::warn!("failed to get xdg base directories: {}", err);
}
}
let mut thumbnailer_paths = Vec::new();
for dir in search_dirs {
log::trace!("looking for thumbnailers in {:?}", dir);
match fs::read_dir(&dir) {
Ok(entries) => {
for entry_res in entries {
match entry_res {
Ok(entry) => thumbnailer_paths.push(entry.path()),
Err(err) => {
log::warn!("failed to read entry in directory {:?}: {}", dir, err);
}
}
}
}
Err(err) => {
log::warn!("failed to read directory {:?}: {}", dir, err);
}
}
}
//TODO: handle directory specific behavior
for path in thumbnailer_paths {
let entry = match freedesktop_entry_parser::parse_entry(&path) {
Ok(ok) => ok,
Err(err) => {
log::warn!("failed to parse {:?}: {}", path, err);
continue;
}
};
//TODO: use TryExec?
let section = entry.section("Thumbnailer Entry");
let Some(exec) = section.attr("Exec") else {
log::warn!("missing Exec attribute for thumbnailer {:?}", path);
continue;
};
let Some(mime_types) = section.attr("MimeType") else {
log::warn!("missing MimeType attribute for thumbnailer {:?}", path);
continue;
};
for mime_type in mime_types.split_terminator(';') {
if let Ok(mime) = mime_type.parse::<Mime>() {
log::trace!("thumbnailer {}={:?}", mime, path);
let apps = self
.cache
.entry(mime.clone())
.or_insert_with(|| Vec::with_capacity(1));
apps.push(Thumbnailer {
exec: exec.to_string(),
});
}
}
}
let elapsed = start.elapsed();
log::info!("loaded thumbnailer cache in {:?}", elapsed);
}
pub fn get(&self, key: &Mime) -> Vec<Thumbnailer> {
self.cache.get(key).map_or_else(Vec::new, |x| x.clone())
}
}
static THUMBNAILER_CACHE: Lazy<Mutex<ThumbnailerCache>> =
Lazy::new(|| Mutex::new(ThumbnailerCache::new()));
pub fn thumbnailer(mime: &Mime) -> Vec<Thumbnailer> {
let thumbnailer_cache = THUMBNAILER_CACHE.lock().unwrap();
thumbnailer_cache.get(mime)
}