feat: Implement rescan endpoint

This commit is contained in:
Dendy 2024-09-24 09:02:02 +02:00
parent fb981d6417
commit 0fa5fd4d04
3 changed files with 29 additions and 3 deletions

View File

@ -17,3 +17,16 @@ pub async fn list_files(
}; };
HtmlTemplate(template) HtmlTemplate(template)
} }
pub async fn rescan_files(
base_path: String,
State(state): State<Arc<RwLock<Vec<utils::FileEntry>>>>,
) -> 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"
}

View File

@ -6,7 +6,6 @@ mod utils;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use crate::config::read_config; use crate::config::read_config;
use crate::handlers::list_files;
use axum::routing::get; use axum::routing::get;
use axum::Router; use axum::Router;
use tokio::net::TcpListener; use tokio::net::TcpListener;
@ -15,11 +14,21 @@ use tokio::net::TcpListener;
async fn main() { async fn main() {
let config = read_config().expect("Failed to read config"); let config = read_config().expect("Failed to read config");
let base_directory = &config.settings.base_directory;
let files = Arc::new(RwLock::new( 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(); let listener = TcpListener::bind("127.0.0.1:3004").await.unwrap();

View File

@ -93,6 +93,8 @@ fn find_tag_files(dir: &str) -> Result<Vec<TagFile>, Box<dyn std::error::Error>>
pub fn find_files(dir: &str) -> Result<Vec<FileEntry>, Box<dyn std::error::Error>> { pub fn find_files(dir: &str) -> Result<Vec<FileEntry>, Box<dyn std::error::Error>> {
let mut files = HashMap::new(); let mut files = HashMap::new();
print!("Generating list of files...");
for tag_file in find_tag_files(dir)? { for tag_file in find_tag_files(dir)? {
for (key, value) in tag_file.entries.into_iter() { for (key, value) in tag_file.entries.into_iter() {
// Create a copy of the list (append needs it to be mutable) and // Create a copy of the list (append needs it to be mutable) and
@ -117,5 +119,7 @@ pub fn find_files(dir: &str) -> Result<Vec<FileEntry>, Box<dyn std::error::Error
} }
} }
println!(" OK");
Ok(files.values().cloned().collect()) Ok(files.values().cloned().collect())
} }