Add file watcher to re-generate thumbnails

Previously files added while the server is running would not have
thumbnails until after the server was restarted. Now we watch the
BASE_PATH for added/renamed files in order to re-gen the thumbnail.
This commit is contained in:
Cameron Cordes
2020-09-13 21:31:32 -04:00
parent 426c695b47
commit 2343351a4d
3 changed files with 118 additions and 0 deletions

View File

@@ -10,11 +10,13 @@ use chrono::{Duration, Utc};
use data::{AddFavoriteRequest, LoginRequest, ThumbnailRequest};
use futures::stream::StreamExt;
use jsonwebtoken::{encode, EncodingKey, Header};
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use rayon::prelude::*;
use serde::Serialize;
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use crate::data::{secret_key, Claims, CreateAccountRequest, Token};
use crate::database::{add_favorite, create_user, get_favorites, get_user, user_exists};
@@ -305,6 +307,31 @@ async fn create_thumbnails() {
async fn main() -> std::io::Result<()> {
create_thumbnails().await;
tokio::spawn(async {
let (wtx, wrx) = channel();
let mut watcher = watcher(wtx, std::time::Duration::from_secs(10)).unwrap();
watcher
.watch(dotenv::var("BASE_PATH").unwrap(), RecursiveMode::Recursive)
.unwrap();
loop {
let ev = wrx.recv_timeout(std::time::Duration::from_secs(5));
match ev {
Ok(event) => {
match event {
DebouncedEvent::Create(_) => create_thumbnails().await,
DebouncedEvent::Rename(_, _) => create_thumbnails().await,
_ => continue,
};
}
Err(e) => {
println!("Event: {:?}", e);
// break;
}
}
}
});
HttpServer::new(|| {
App::new()
.service(register)