/
misc.rs
52 lines (45 loc) · 1.56 KB
/
misc.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
use std::time::{SystemTime, UNIX_EPOCH};
use linkify::{LinkFinder, LinkKind};
use std::fs;
use crate::{dbio, Pasta};
pub fn remove_expired(pastas: &mut Vec<Pasta>) {
// get current time - this will be needed to check which pastas have expired
let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(n) => n.as_secs(),
Err(_) => {
log::error!("SystemTime before UNIX EPOCH!");
0
}
} as i64;
pastas.retain(|p| {
// expiration is `never` or not reached
if p.expiration == 0 || p.expiration > timenow {
// keep
true
} else {
// remove the file itself
if let Some(file) = &p.file {
if fs::remove_file(format!(
"./pasta_data/public/{}/{}",
p.id_as_animals(),
file.name()
))
.is_err()
{
log::error!("Failed to delete file {}!", file.name())
}
// and remove the containing directory
if fs::remove_dir(format!("./pasta_data/public/{}/", p.id_as_animals())).is_err() {
log::error!("Failed to delete directory {}!", file.name())
}
}
false
}
});
dbio::save_to_file(pastas);
}
pub fn is_valid_url(url: &str) -> bool {
let finder = LinkFinder::new();
let spans: Vec<_> = finder.spans(url).collect();
spans[0].as_str() == url && Some(&LinkKind::Url) == spans[0].kind()
}