-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
309 lines (265 loc) · 9.88 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use async_std::fs;
use async_std::prelude::StreamExt;
use eframe::{egui, NativeOptions};
use egui::{ProgressBar, Ui};
use filetime_creation::{set_file_ctime, set_file_mtime, FileTime};
use rfd::FileDialog;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use std::vec::Vec;
// todo
// error window
// show skipped files count
// show matched files count
// geo data
// subdirs
// optionally delete jsons
fn main() -> Result<(), eframe::Error> {
env_logger::init();
let options = NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([360.0, 240.0]),
..Default::default()
};
eframe::run_native(
"Google Photos metadata matcher",
options,
Box::new(|_cc| Box::<Matcher>::default()),
)
}
struct Matcher {
folder_path: String,
selected_folder: Option<PathBuf>,
should_copy: bool,
should_go_over_subdirs: bool,
progress: Arc<Mutex<f32>>,
working_message: String,
}
impl Default for Matcher {
fn default() -> Self {
Self {
folder_path: String::new(),
selected_folder: None,
should_copy: false,
should_go_over_subdirs: false,
progress: Arc::new(Mutex::new(0.0)),
working_message: "".to_string(),
}
}
}
impl eframe::App for Matcher {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui: &mut Ui| {
ui.horizontal(|ui: &mut Ui| {
// Select folder by entering path
ui.label("Folder Path:");
ui.add(egui::TextEdit::singleline(&mut self.folder_path).desired_width(200.0));
if ui.button("Select").clicked() {
if let Ok(path) = std::fs::canonicalize(&self.folder_path) {
self.selected_folder = Some(path);
} else {
self.selected_folder = None;
}
}
});
// Select folder button by window dialog
if ui.button("Select Folder").clicked() {
if let Some(folder) = FileDialog::new()
.set_directory(&std::env::current_dir().unwrap())
.pick_folder()
{
self.selected_folder = Some(folder);
}
}
// Show selected folder in ui
if let Some(folder) = self.selected_folder.as_ref().and_then(|p| p.to_str()) {
ui.label(format!("Selected folder: {}", folder));
} else {
ui.label("No folder selected");
}
// Those are not implemented
// ui.checkbox(&mut self.should_copy, "Copy photos to new folder");
// ui.checkbox(&mut self.should_go_over_subdirs, "Go over subdirectories");
// ui.checkbox(, "Delete json files");
// Match metadata button
if ui.button("Match metadata").clicked() {
if self.selected_folder == None {
return;
}
self.working_message = "Working...".to_string();
let matcher = Matcher {
folder_path: self.folder_path.clone(),
selected_folder: self.selected_folder.clone(),
should_copy: self.should_copy,
should_go_over_subdirs: self.should_go_over_subdirs,
progress: self.progress.clone(),
working_message: "".to_string(),
};
let ctx_clone = ctx.clone();
async_std::task::spawn(async move {
match matcher.selected_folder {
Some(folder) => {
match_metadata(
folder,
matcher.should_go_over_subdirs,
matcher.should_copy,
matcher.progress,
ctx_clone,
)
.await;
}
None => {
println!("No folder selected");
}
}
});
}
// Progress bar
let prog = self.progress.lock().unwrap();
if *prog > 0.0 && *prog < 1.0 {
ui.add(ProgressBar::new(*prog).show_percentage());
} else if *prog >= 1.0 {
ui.label("Metadata processing complete");
self.working_message = "".to_string();
}
ui.label(&self.working_message);
});
}
}
async fn match_metadata(
path: PathBuf,
search_subdirs: bool,
copy: bool,
progress: Arc<Mutex<f32>>,
ctx: egui::Context,
) {
println!("copy photos: {}", copy);
println!("subdirs: {}", search_subdirs);
println!("path: {:?}", path);
let (progress_sender, progress_receiver) = mpsc::channel();
// let ctx_clone = ctx.clone();
async_std::task::spawn(async move {
if search_subdirs {
unimplemented!();
// disappears immediately due to request_repaint() on progress_receiver.recv()
// display_error(
// &ctx_clone,
// "Searching subdirectories is currently unimplemented.",
// )
// .await;
// return;
} else {
// search for jsons
let json_paths = get_jsons(&path).await;
let metadata = extract_metadata(json_paths).await;
// open the files by the title inside of the json file and match the timestamps to the images
match metadata {
Ok(m) => {
let total_elements = m.len();
let mut current_element = 0;
for element in m {
open_and_match(element, &path);
// progress
current_element += 1;
let progress = (current_element as f32) / (total_elements as f32);
match progress_sender.send(progress) {
Ok(_) => println!("Sent progress: {}", progress),
Err(err) => println!("Error sending progress: {}", err),
}
}
}
Err(e) => {
println!("Error: {}", e);
// display_error(&ctx_clone, e.as_str()).await;
return;
}
}
progress_sender.send(1.0).unwrap();
}
});
while let Ok(p) = progress_receiver.recv() {
ctx.request_repaint();
*progress.lock().unwrap() = p;
}
}
async fn get_jsons(path: &PathBuf) -> Vec<async_std::path::PathBuf> {
let mut json_paths = Vec::new();
if let Ok(mut entries) = fs::read_dir(&path).await {
while let Some(entry) = entries.next().await {
if let Ok(entry) = entry {
let file_path = entry.path();
if let Some(extension) = file_path.extension() {
if extension == "json" {
json_paths.push(file_path);
}
}
}
}
} else {
println!("Failed to read directory: {:?}", path);
}
json_paths
}
struct GPhotosMetadata {
title: String,
phototaken_timestamp: i64,
// todo geo data
}
async fn extract_metadata(
json_paths: Vec<async_std::path::PathBuf>,
) -> Result<Vec<GPhotosMetadata>, String> {
let mut all_files_metadata = Vec::new();
for json_path in json_paths {
let file_content = async_std::fs::read_to_string(&json_path)
.await
.map_err(|err| format!("Failed to read JSON file {:?}: {}", json_path, err))?;
let json_value = serde_json::from_str::<Value>(&file_content)
.map_err(|err| format!("Failed to parse JSON file {:?}: {}", json_path, err))?;
let title = if let Some(t) = json_value.get("title") {
t.as_str().unwrap().to_string()
} else {
return Err(format!(
"JSON file {:?} does not contain 'title' property",
json_path
));
};
let cr_time = if let Some(creation_time) = json_value.get("photoTakenTime") {
let timestamp = creation_time["timestamp"].as_str().unwrap();
timestamp.parse::<i64>().unwrap()
} else {
return Err(format!(
"JSON file {:?} does not contain 'photoTakenTime' property",
json_path
));
};
let metadata = GPhotosMetadata {
title: title,
phototaken_timestamp: cr_time,
};
all_files_metadata.push(metadata);
}
return Ok(all_files_metadata);
}
// async fn display_error(ctx: &egui::Context, message: &str) {
// egui::Window::new("Error").show(ctx, |ui| {
// ui.add(egui::Label::new(message));
// });
// }
fn open_and_match(el: GPhotosMetadata, path: &PathBuf) {
println!("{:?}", el.title);
println!("{:?}", el.phototaken_timestamp);
// Photo taken time
let phototaken_time = SystemTime::UNIX_EPOCH + Duration::new(el.phototaken_timestamp as u64, 0);
FileTime::from_system_time(phototaken_time);
let phototaken_filetime = FileTime::from_system_time(phototaken_time);
// todo geo data
let file_path = path.join(&el.title);
if !file_path.exists() {
println!("File {:?} does not exist, skipping...", file_path);
return;
}
set_file_ctime(&file_path, phototaken_filetime).expect("Failed to set creation file time");
set_file_mtime(&file_path, phototaken_filetime).expect("Failed to set modification file time");
}