-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_util.rs
294 lines (219 loc) · 7.27 KB
/
file_util.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
extern crate image;
extern crate rand;
extern crate immeta;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::fs;
use std::thread;
use glob::glob;
use error::{Result, ResultExt, ErrorKind};
use chrono::NaiveDateTime;
use exiftool;
use exiftool::{ExifData};
use byte_source::{ByteSource, vec_from_byte_source};
use std::sync::mpsc;
use std::time::{SystemTime, UNIX_EPOCH};
/**
Length or width, depending on which is the longest of a generated thumbnail
*/
const THUMBNAIL_SIZE: u32 = 200;
/**
Starts a thread that generates a thumbnail from the specified source. It is
stored in `destination_path`
Returns a channel which can be used to listen for errors that occured during
generation
*/
pub fn generate_thumbnail(
source: ByteSource,
destination_path: &Path,
) -> mpsc::Receiver<Result<()>> {
let destination_path = destination_path.to_owned();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let handler = || -> Result<()> {
let file_content = vec_from_byte_source(source)?;
let img = image::load_from_memory(&file_content)?;
let thumb_data = generate_thumbnail_from_generic_image(&img, THUMBNAIL_SIZE);
let fout = &mut File::create(&destination_path)?;
thumb_data.save(fout, image::PNG)?;
Ok(())
};
let generation_result = handler()
.chain_err(|| ErrorKind::ThumbnailGenerationFailed);
// We don't care if the result could not be sent because it probably means
// that the receiver does not care
let _result = tx.send(generation_result);
});
rx
}
/**
Takes a `image::GenericImage` and generates a thumbnail image from that
*/
fn generate_thumbnail_from_generic_image(
src: &image::DynamicImage,
max_size: u32,
) -> image::DynamicImage {
src.resize(max_size, max_size, image::FilterType::Nearest)
}
/**
Returns a really big random number as a string
*/
pub fn get_semi_unique_identifier() -> i32 {
rand::random::<i32>()
}
pub fn get_file_timestamp(filename: &Path) -> Result<NaiveDateTime> {
Ok(match get_file_timestamp_from_metadata(filename)? {
Some(timestamp) => timestamp,
None => get_file_timestamp_from_filesystem(filename)?
})
}
/**
Reads the timestamp of the specified file from the file metadata. Returns Ok(None) if
the metadata does not contain a creation time and Err if reading failed for some reason.
*/
pub fn get_file_timestamp_from_metadata(filename: &Path) -> Result<Option<NaiveDateTime>> {
let exif_data = ExifData::from_file(&filename.to_string_lossy())?;
match exif_data.get_creation_date() {
Ok(data) => Ok(Some(data)),
Err(exiftool::Error(exiftool::ErrorKind::NoSuchTag(_), _)) => Ok(None),
Err(e) => Err(e)?
}
}
/**
Converts a `SystemTime` into a u64
*/
fn system_time_as_unix_timestamp(time: SystemTime) -> Result<u64> {
let duration = time.duration_since(UNIX_EPOCH)?;
Ok(duration.as_secs())
}
/**
Reads the modified time of the specified file from the timestamp in the file system.
*/
fn get_file_timestamp_from_filesystem(filename: &Path) -> Result<NaiveDateTime> {
let modification_time = fs::metadata(filename)?.modified()?;
Ok(NaiveDateTime::from_timestamp(system_time_as_unix_timestamp(modification_time)? as i64, 0))
}
/**
Checks a list of tags for unallowed characters and converts it into a storeable format,
which at the moment is just removal of capital letters
*/
pub fn sanitize_tag_names(tag_list: &[String]) -> Vec<String> {
tag_list.iter().filter(|x| !x.is_empty()).map(|x| x.to_lowercase()).collect()
}
/**
Returns a list of all the files in a directory
*/
pub fn get_files_in_dir(dir: &PathBuf) -> Vec<PathBuf> {
let mut result = Vec::<PathBuf>::new();
let full_path = String::from(dir.to_string_lossy()).clone() + "/*";
for entry in glob(&full_path).expect("Failed to read glob") {
match entry {
Ok(path) => result.push(path),
Err(e) => println!("{}", e),
}
}
result
}
/**
Returns a list of all subdirectories of a dir
*/
pub fn subdirs_in_directory(dir: &Path) -> Result<Vec<PathBuf>> {
let files = fs::read_dir(dir)?;
Ok(files.map(|file| {
let file = file?;
match file.metadata() {
Ok(ref metadata) if metadata.is_dir() => Ok(Some(file.path())),
Ok(_) => Ok(None),
Err(e) => Err(e)?
}
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.filter_map(|a| a)
.map(|path| path.strip_prefix(dir).unwrap().to_path_buf())
.collect()
)
}
#[cfg(test)]
mod thumbnail_tests {
extern crate image;
use image::GenericImage;
#[test]
fn thumbnail_test() {
let img = image::DynamicImage::new_rgba8(500, 500);
let thumbnail = super::generate_thumbnail_from_generic_image(&img, 300);
assert!(thumbnail.dimensions() == (300, 300));
}
#[test]
fn portrait_test() {
let img = image::DynamicImage::new_rgba8(500, 250);
let thumbnail = super::generate_thumbnail_from_generic_image(&img, 300);
assert!(thumbnail.dimensions() == (300, 150));
}
#[test]
fn landscape_test() {
let img = image::DynamicImage::new_rgba8(250, 500);
let thumbnail = super::generate_thumbnail_from_generic_image(&img, 300);
assert!(thumbnail.dimensions() == (150, 300));
}
}
#[cfg(never)]
mod thumbnail_bench {
extern crate test;
use self::test::Bencher;
extern crate image;
#[bench]
fn thumbnail_generation_bench(b: &mut Bencher) {
let image = image::open("test/media/DSC_0001.JPG").unwrap();
b.iter(|| {
super::generate_thumbnail_from_generic_image(&image, 300);
})
}
}
#[cfg(test)]
mod util_tests {
use super::*;
use chrono::NaiveDate;
#[test]
fn sanitize_tests() {
{
let vec = vec![
String::from("abCde"),
String::from("ABC"),
String::from("abc"),
String::from(""),
];
let expected = vec![
String::from("abcde"),
String::from("abc"),
String::from("abc"),
];
assert_eq!(sanitize_tag_names(&vec), expected);
}
}
#[test]
fn subdir_test() {
let mut subdirs = subdirs_in_directory(&PathBuf::from("test")).expect("Failed to read files in dir");
let mut expected = mapvec!(PathBuf::from: "media", "files");
expected.sort();
subdirs.sort();
assert_eq!(subdirs.len(), 2);
assert_eq!(
subdirs,
expected
);
}
#[test]
fn metadata_timestamp_test() {
let path = PathBuf::from("test/media/DSC_0001.JPG");
let timestamp = get_file_timestamp(&path).unwrap();
assert_eq!(timestamp, NaiveDate::from_ymd(2016, 12, 16).and_hms(21, 34, 26));
}
#[test]
// TODO Test disabled for now. Find a way to reliably test this
fn filesystem_timestamp_test() {
let path = PathBuf::from("test/media/10x10.png");
let _timestamp = get_file_timestamp(&path).unwrap();
// assert_eq!(timestamp, NaiveDate::from_ymd(2018, 5, 31).and_hms(20, 39, 56));
}
}