Added some basic file archiving functionality

This commit is contained in:
Gregory Ballantine 2022-08-31 18:05:00 -04:00
parent 0fa83eff42
commit b979850582
2 changed files with 56 additions and 6 deletions

View File

@ -2,7 +2,10 @@ use std::fs;
use std::path::Path;
pub struct Repository {
base_path: String,
base_dir: String,
ingest_dir: String,
archive_dir: String,
output_dir: String,
}
impl Repository {
@ -10,15 +13,54 @@ impl Repository {
// create the base directory path
create_directory(base_path);
let sub_dirs: Vec<&str> = vec!["ingest", "archive", "output"];
for s in sub_dirs {
create_directory(Path::new(base_path).join(s).to_str().unwrap());
}
// 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());
return Repository {
base_path: String::from(base_path),
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()),
};
}
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: String) {
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);
}
}
}
}
fn create_directory(path: &str) {

View File

@ -16,5 +16,13 @@ impl Transcoder {
pub fn start(self) {
println!("Starting transcoder...");
// search for files in ingest
let ingest_files = self.repository.search_ingest();
for i in ingest_files {
// copy the file to the archive
self.repository.archive_file(i);
}
}
}