Skip to content

Commit

Permalink
feat: abstract resource reader usage
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Oct 1, 2022
1 parent 805e842 commit 44dd657
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
86 changes: 72 additions & 14 deletions src/resource_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Holds all specified local paths
pub local_resource_paths: Vec<String>,
/// Holds all specified local paths
pub samba_resource_paths: Vec<String>,
}

impl ResourceReader {
/// Reads the specified resource from the filesystem
/// Returns the resource file data
pub fn read_resource_data(&self, resource: &RemoteResource) -> Vec<u8> {
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<RemoteResource> {
self.paths
let local_resources: Vec<RemoteResource> = 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<RemoteResource> = 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,
}
}

Expand All @@ -57,12 +90,28 @@ pub struct RemoteResource {
pub taken: Option<NaiveDateTime>,
pub location: Option<GeoLocation>,
pub orientation: Option<ImageOrientation>,
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<RemoteResource> {
let maybe_folder_path = std::fs::File::open(folder_path);
pub fn read_all_local_files_recursive(folder_path: &PathBuf) -> Vec<RemoteResource> {
let maybe_folder_path = fs::File::open(folder_path);

if maybe_folder_path.is_err() {
error!("Could not open folder: {}", folder_path.display());
Expand Down Expand Up @@ -91,17 +140,24 @@ pub fn read_folder(folder_path: &PathBuf) -> Vec<RemoteResource> {
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<RemoteResource> {
// 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<RemoteResource> {
fn read_local_file(file_path: &PathBuf) -> Vec<RemoteResource> {
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();
Expand All @@ -124,6 +180,7 @@ fn read_file(file_path: &PathBuf) -> Vec<RemoteResource> {
taken: None,
location: None,
orientation: None,
resource_type: RemoteResourceType::Local,
}]
}

Expand Down Expand Up @@ -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,
Expand All @@ -170,6 +227,7 @@ impl Display for RemoteResource {
self.last_modified,
self.taken,
self.location,
self.resource_type,
)
}
}
16 changes: 8 additions & 8 deletions src/resource_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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!(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 44dd657

Please sign in to comment.