From b9798505828c337dacec88fc0801b8c941da181a Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 31 Aug 2022 18:05:00 -0400 Subject: [PATCH] Added some basic file archiving functionality --- src/repository.rs | 54 +++++++++++++++++++++++++++++++++++++++++------ src/transcoder.rs | 8 +++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/repository.rs b/src/repository.rs index 40b755d..d532bb6 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -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 { + // 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 = 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) { diff --git a/src/transcoder.rs b/src/transcoder.rs index 894efb7..d1bb221 100644 --- a/src/transcoder.rs +++ b/src/transcoder.rs @@ -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); + } } }