adept/src/repository.rs

88 lines
2.3 KiB
Rust
Raw Normal View History

2022-08-31 16:14:41 -04:00
use std::fs;
use std::path::Path;
pub struct Repository {
base_dir: String,
ingest_dir: String,
archive_dir: String,
output_dir: String,
2022-08-31 16:14:41 -04:00
}
impl Repository {
pub fn new(base_path: &str) -> Repository {
// create the base directory path
create_directory(base_path);
// create the needed sub-directories
let ingest_path = Path::new(base_path).join("ingest");
create_directory(ingest_path.to_str().unwrap());
let archive_path = Path::new(base_path).join("archive");
create_directory(archive_path.to_str().unwrap());
let output_path = Path::new(base_path).join("output");
create_directory(output_path.to_str().unwrap());
2022-08-31 16:14:41 -04:00
return Repository {
base_dir: String::from(base_path),
ingest_dir: String::from(ingest_path.to_str().unwrap()),
archive_dir: String::from(archive_path.to_str().unwrap()),
output_dir: String::from(output_path.to_str().unwrap()),
2022-08-31 16:14:41 -04:00
};
}
pub fn search_ingest(&self) -> Vec<String> {
// read file entries from ingest
let files = fs::read_dir(&self.ingest_dir).unwrap();
// create vec object and loop through entries to find what we want
let mut ingest_files: Vec<String> = vec![];
for f in files {
let f = f.unwrap();
let path = f.path();
if path.is_file() {
let file_path = path.file_name().unwrap().to_str();
ingest_files.push(String::from(file_path.unwrap()));
}
}
return ingest_files;
}
pub fn archive_file(&self, file: &str) {
let ingest_file = Path::new(&self.ingest_dir).join(file);
let archive_file = Path::new(&self.archive_dir).join(file);
match fs::copy(&ingest_file, &archive_file) {
Ok(_) => {
println!("Archiving video file {}.", ingest_file.to_str().unwrap());
},
Err(e) => {
eprintln!("Error archiving file {}: {}", ingest_file.to_str().unwrap(), e);
std::process::exit(1);
}
}
}
pub fn cleanup_file(&self, file: &str) {
let ingest_file = Path::new(&self.ingest_dir).join(file);
fs::remove_file(&ingest_file)
.expect("File deletion failed.");
}
2022-08-31 16:14:41 -04:00
}
fn create_directory(path: &str) {
let d = Path::new(path);
if d.is_dir() {
println!("Directory {} already exists.", path);
} else {
match fs::create_dir(path) {
Ok(_) => {
println!("Creating directory {}.", path);
},
Err(e) => {
eprintln!("Error creating {}: {}", path, e);
std::process::exit(1);
}
}
}
}