diff --git a/Cargo.lock b/Cargo.lock index 542e849..72432d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -816,6 +816,18 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "filetime" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed85775dcc68644b5c950ac06a2b23768d3bc9390464151aaf27136998dcf9e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.9", +] + [[package]] name = "flate2" version = "1.0.17" @@ -834,6 +846,25 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fsevent" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" +dependencies = [ + "bitflags", + "fsevent-sys", +] + +[[package]] +name = "fsevent-sys" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" +dependencies = [ + "libc", +] + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -1126,10 +1157,12 @@ dependencies = [ "hmac", "image", "jsonwebtoken", + "notify", "rayon", "serde", "serde_json", "sha2", + "tokio", "walkdir", ] @@ -1143,6 +1176,26 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inotify" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f" +dependencies = [ + "bitflags", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74a1aa87c59aeff6ef2cc2fa62d41bc43f54952f55652656b18a02fd5e356c0" +dependencies = [ + "libc", +] + [[package]] name = "instant" version = "0.1.6" @@ -1231,6 +1284,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.77" @@ -1372,6 +1431,18 @@ dependencies = [ "winapi 0.2.8", ] +[[package]] +name = "mio-extras" +version = "2.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" +dependencies = [ + "lazycell", + "log", + "mio", + "slab", +] + [[package]] name = "mio-uds" version = "0.6.8" @@ -1416,6 +1487,24 @@ dependencies = [ "version_check 0.1.5", ] +[[package]] +name = "notify" +version = "4.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80ae4a7688d1fab81c5bf19c64fc8db920be8d519ce6336ed4e7efe024724dbd" +dependencies = [ + "bitflags", + "filetime", + "fsevent", + "fsevent-sys", + "inotify", + "libc", + "mio", + "mio-extras", + "walkdir", + "winapi 0.3.9", +] + [[package]] name = "num-bigint" version = "0.2.6" diff --git a/Cargo.toml b/Cargo.toml index 1c1ab29..3298ec2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,5 @@ bcrypt = "0.8.1" image = "0.23.7" walkdir = "2" rayon = "1.3" +notify = "4.0" +tokio = "0.2" diff --git a/src/main.rs b/src/main.rs index 73e933a..8fdf50c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)