From 44dd6578f763a7c505f31498eaea8c6720b5423c Mon Sep 17 00:00:00 2001 From: Rouven Hi Date: Sat, 1 Oct 2022 11:45:00 +0200 Subject: [PATCH] feat: abstract resource reader usage --- src/resource_reader.rs | 86 +++++++++++++++++++++++++++++++------ src/resource_reader_test.rs | 16 +++---- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/src/resource_reader.rs b/src/resource_reader.rs index 05c4a32..632429b 100644 --- a/src/resource_reader.rs +++ b/src/resource_reader.rs @@ -11,37 +11,70 @@ use rayon::iter::IntoParallelRefIterator; use rayon::iter::ParallelIterator; use serde::{Deserialize, Serialize}; +use crate::{exif_reader, resource_processor}; use crate::geo_location::GeoLocation; use crate::image_processor::ImageOrientation; -use crate::{exif_reader, resource_processor}; /// A resource reader that reads available resources from the filesystem #[derive(Clone)] pub struct ResourceReader { - pub paths: Vec, + /// Holds all specified local paths + pub local_resource_paths: Vec, + /// Holds all specified local paths + pub samba_resource_paths: Vec, } impl ResourceReader { /// Reads the specified resource from the filesystem /// Returns the resource file data pub fn read_resource_data(&self, resource: &RemoteResource) -> Vec { - fs::read(resource.path.clone()).unwrap() + match resource.resource_type { + RemoteResourceType::Local => { + fs::read(resource.path.clone()).unwrap() + } + RemoteResourceType::Samba => { + // TODO: implement me + vec![] + } + } } /// Returns all available resources from the filesystem pub fn list_all_resources(&self) -> Vec { - self.paths + let local_resources: Vec = self.local_resource_paths .par_iter() - .flat_map(|path| read_folder(&PathBuf::from(path))) + .inspect(|x| println!(" ## local ## {x}")) // TODO: remove me + .flat_map(|path| read_all_local_files_recursive(&PathBuf::from(path))) .map(|resource| exif_reader::fill_exif_data(&resource)) - .collect() + .collect(); + + let samba_resources: Vec = self.samba_resource_paths + .par_iter() + .inspect(|x| println!(" ## remote ## {x}"))// TODO: remove me + .flat_map(|path| read_all_samba_files_recursive(&PathBuf::from(path))) + .map(|resource| exif_reader::fill_exif_data(&resource)) + .collect(); + + [local_resources, samba_resources].concat() } } /// Instantiates a new resource reader for the given paths -pub fn new(paths: &str) -> ResourceReader { +pub fn new(resource_folder_paths: &str) -> ResourceReader { + let mut local_resource_paths = vec![]; + let mut samba_resource_paths = vec![]; + + for resource_folder in resource_folder_paths.split(',').map(|s| s.to_string()) { + if resource_folder.starts_with("smb://") { + samba_resource_paths.push(resource_folder); + } else { + local_resource_paths.push(resource_folder); + } + } + ResourceReader { - paths: paths.split(',').map(|s| s.to_string()).collect(), + local_resource_paths, + samba_resource_paths, } } @@ -57,12 +90,28 @@ pub struct RemoteResource { pub taken: Option, pub location: Option, pub orientation: Option, + pub resource_type: RemoteResourceType, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum RemoteResourceType { + Local, + Samba, +} + +impl Display for RemoteResourceType { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + match self { + RemoteResourceType::Local => write!(f, "Local"), + RemoteResourceType::Samba => write!(f, "Samba"), + } + } } /// Reads all files of a folder and returns all found resources /// The folder is recursively searched -pub fn read_folder(folder_path: &PathBuf) -> Vec { - let maybe_folder_path = std::fs::File::open(folder_path); +pub fn read_all_local_files_recursive(folder_path: &PathBuf) -> Vec { + let maybe_folder_path = fs::File::open(folder_path); if maybe_folder_path.is_err() { error!("Could not open folder: {}", folder_path.display()); @@ -91,17 +140,24 @@ pub fn read_folder(folder_path: &PathBuf) -> Vec { let metadata = dir_entry.metadata().expect("Failed to read metadata"); if metadata.is_file() { - read_file(&dir_entry.path()) + read_local_file(&dir_entry.path()) } else { - read_folder(&dir_entry.path()) + read_all_local_files_recursive(&dir_entry.path()) } }) .collect() } +/// Reads all files of a samba folder and returns all found resources +/// The folder is recursively searched +pub fn read_all_samba_files_recursive(folder_path: &PathBuf) -> Vec { + // TODO: implement me + vec![] +} + /// Reads a single file and returns the found resource /// Checks if the file is a supported resource currently all image types -fn read_file(file_path: &PathBuf) -> Vec { +fn read_local_file(file_path: &PathBuf) -> Vec { let file = std::fs::File::open(file_path).unwrap(); let metadata = file.metadata().expect("Failed to read metadata"); let file_name = file_path.as_path().file_name().unwrap().to_str().unwrap(); @@ -124,6 +180,7 @@ fn read_file(file_path: &PathBuf) -> Vec { taken: None, location: None, orientation: None, + resource_type: RemoteResourceType::Local, }] } @@ -161,7 +218,7 @@ impl Display for RemoteResource { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, - "{} {} {} {} {} {} {:?} {:?}", + "{} {} {} {} {} {} {:?} {:?} {}", self.id, self.path, self.content_type, @@ -170,6 +227,7 @@ impl Display for RemoteResource { self.last_modified, self.taken, self.location, + self.resource_type, ) } } diff --git a/src/resource_reader_test.rs b/src/resource_reader_test.rs index cc55d0a..cd9f4bc 100644 --- a/src/resource_reader_test.rs +++ b/src/resource_reader_test.rs @@ -25,7 +25,7 @@ fn read_dir_recursive() { create_test_file(&base_test_dir, "sub2", "test_file.txt"); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN two resources should be found assert_eq!(resources_read.len(), 2); @@ -42,7 +42,7 @@ fn read_jpg_image_resource() { let test_image_1_path = create_test_image(&base_test_dir, "", test_image_name, TEST_JPEG_URL); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN the resource info should be correct assert_eq!(resources_read.len(), 1); @@ -67,7 +67,7 @@ fn read_jpg_with_exif_image_resource() { // WHEN reading resources from a folder let resources_read = - exif_reader::fill_exif_data(&resource_reader::read_folder(&base_test_dir)[0]); + exif_reader::fill_exif_data(&resource_reader::read_all_local_files_recursive(&base_test_dir)[0]); // THEN the resource metadata should be correct assert_eq!( @@ -101,7 +101,7 @@ fn read_png_image_resource() { let test_image_1_path = create_test_image(&base_test_dir, "", test_image_name, TEST_PNG_URL); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN the resource info should be correct assert_eq!(resources_read.len(), 1); @@ -125,7 +125,7 @@ fn read_gif_image_resource() { let test_image_1_path = create_test_image(&base_test_dir, "", test_image_name, TEST_GIF_URL); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN the resource info should be correct assert_eq!(resources_read.len(), 1); @@ -148,7 +148,7 @@ fn read_no_images_dir() { create_test_file(&base_test_dir, "", "test_file.txt"); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN two resources should be found assert_eq!(resources_read.len(), 0); @@ -163,7 +163,7 @@ fn read_empty_dir() { let base_test_dir = create_temp_folder(); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN two resources should be found assert_eq!(resources_read.len(), 0); @@ -178,7 +178,7 @@ fn read_non_existent_folder() { let base_test_dir = PathBuf::from("/some/non/existent/path"); // WHEN reading resources from a folder - let resources_read = resource_reader::read_folder(&base_test_dir); + let resources_read = resource_reader::read_all_local_files_recursive(&base_test_dir); // THEN two resources should be found assert_eq!(resources_read.len(), 0);