Initial draft

This commit is contained in:
Dennis Nemec
2025-09-26 16:41:42 +02:00
commit 3f57ddefb7
10 changed files with 502 additions and 0 deletions

55
src/app.rs Normal file
View File

@ -0,0 +1,55 @@
use std::fs;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use crate::config::Config;
pub fn move_file_to_dir(watch_path_string: &String, filename: &String, move_path_string: &String) -> Result<(), Box<dyn std::error::Error>> {
let move_path = Path::new(move_path_string);
let watch_path = Path::new(watch_path_string);
let file_path = watch_path.join(Path::new(filename));
let mut moved_file_path = move_path.join(filename);
if !fs::exists(move_path)? {
info!("Creating directory {}", move_path_string);
fs::create_dir(move_path)?;
}
if fs::exists(&moved_file_path)? {
moved_file_path.set_file_name(format!("{}.2", filename));
}
fs::copy(&file_path, &moved_file_path)?;
fs::remove_file(&file_path)?;
info!("Moved file to {}", &moved_file_path.display());
Ok(())
}
pub fn restart_server() {
}
pub fn watch_files(config: Config) -> Result<(), Box<dyn std::error::Error>> {
loop {
let mut has_found = false;
for file in fs::read_dir(Path::new(&config.watch_path))? {
if file.is_ok() {
let filename = file.unwrap().file_name().into_string().unwrap();
if filename.starts_with(&config.watch_file_prefix) {
info!("Found file: {}. Moving file.", filename);
move_file_to_dir(&config.watch_path, &filename, &config.dir)?;
has_found = true;
}
}
}
if has_found {
info!("Restarting server");
restart_server();
}
sleep(Duration::from_secs(config.period as u64));
}
}