From 0fa5fd4d0465ef98ed31176ed1741e63b8927cc2 Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Tue, 24 Sep 2024 09:02:02 +0200 Subject: [PATCH] feat: Implement rescan endpoint --- src/handlers.rs | 13 +++++++++++++ src/main.rs | 15 ++++++++++++--- src/utils.rs | 4 ++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index 237db6d..ddeb099 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -17,3 +17,16 @@ pub async fn list_files( }; HtmlTemplate(template) } + +pub async fn rescan_files( + base_path: String, + State(state): State>>>, +) -> impl IntoResponse { + let mut files = state.write().unwrap(); // Adquire lock + + files.clear(); + + files.extend(utils::find_files(&base_path).expect("Error trying to refresh list")); + + "Files rescanned successfuly" +} diff --git a/src/main.rs b/src/main.rs index ee12ffc..f12b1fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ mod utils; use std::sync::{Arc, RwLock}; use crate::config::read_config; -use crate::handlers::list_files; use axum::routing::get; use axum::Router; use tokio::net::TcpListener; @@ -15,11 +14,21 @@ use tokio::net::TcpListener; async fn main() { let config = read_config().expect("Failed to read config"); + let base_directory = &config.settings.base_directory; let files = Arc::new(RwLock::new( - utils::find_files(&config.settings.base_directory).expect("Couldn't open files directory"), + utils::find_files(base_directory).expect("Couldn't open files directory"), )); - let app = Router::new().route("/", get(list_files)).with_state(files); + let app = Router::new() + .route("/", get(handlers::list_files)) + .route( + "/rescan", + get({ + let base_directory = base_directory.clone(); + move |state| handlers::rescan_files(base_directory, state) + }), + ) + .with_state(files); let listener = TcpListener::bind("127.0.0.1:3004").await.unwrap(); diff --git a/src/utils.rs b/src/utils.rs index 31c58aa..cde2377 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -93,6 +93,8 @@ fn find_tag_files(dir: &str) -> Result, Box> pub fn find_files(dir: &str) -> Result, Box> { let mut files = HashMap::new(); + print!("Generating list of files..."); + for tag_file in find_tag_files(dir)? { for (key, value) in tag_file.entries.into_iter() { // Create a copy of the list (append needs it to be mutable) and @@ -117,5 +119,7 @@ pub fn find_files(dir: &str) -> Result, Box