WIP implement list_root_folder in the adapter.
This commit is contained in:
@@ -14,11 +14,41 @@ impl VideoAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_root_folder_read_library_items(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
pub fn get_root_folder_read_library_items(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
||||||
// get the root_folder path from the environment variable named "ROOT_FOLDER"
|
use std::env;
|
||||||
// list all elements inside this folder on the file system
|
use std::fs;
|
||||||
// map all those elements into ReadLibraryItem objects
|
|
||||||
// return the list
|
// Get the root_folder path from environment variable
|
||||||
todo!()
|
let root_folder = env::var("ROOT_FOLDER").map_err(|_| VideoError::ConfigurationError("ROOT_FOLDER environment variable not set"))?;
|
||||||
|
|
||||||
|
// List all elements inside this folder on the file system
|
||||||
|
let entries = fs::read_dir(root_folder)
|
||||||
|
.map_err(|e| VideoError::FileSystemError(e.to_string()))?;
|
||||||
|
|
||||||
|
// Map all those elements into ReadLibraryItem objects
|
||||||
|
let mut items: Vec<ReadLibraryItem> = Vec::new();
|
||||||
|
for entry in entries {
|
||||||
|
let entry = entry.map_err(|e| VideoError::FileSystemError(e.to_string()))?;
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
// Determine if it's a file or directory
|
||||||
|
let is_dir = path.is_dir();
|
||||||
|
let item_type = if is_dir {
|
||||||
|
lumiere_domain::video::model::library_item::LibraryItemType::FOLDER
|
||||||
|
} else {
|
||||||
|
lumiere_domain::video::model::library_item::LibraryItemType::VIDEO
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create ReadLibraryItem with None for ID (will be generated later)
|
||||||
|
let item = ReadLibraryItem {
|
||||||
|
id: None,
|
||||||
|
name: entry.file_name().to_string_lossy().into_owned(),
|
||||||
|
r#type: item_type,
|
||||||
|
};
|
||||||
|
|
||||||
|
items.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_all_library_items_from_db(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
pub fn get_all_library_items_from_db(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
||||||
@@ -28,13 +58,36 @@ impl VideoAdapter {
|
|||||||
|
|
||||||
impl VideoPort for VideoAdapter {
|
impl VideoPort for VideoAdapter {
|
||||||
fn list_root_folder(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
fn list_root_folder(&self) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
||||||
// In parallel:
|
use std::thread;
|
||||||
// - call method "get_root_folder_read_library_items()"
|
|
||||||
// - call method "get_all_library_items_from_db()"
|
// In parallel: call both methods
|
||||||
//
|
let file_system_items = self.get_root_folder_read_library_items()?;
|
||||||
// then, merge the two lists. If an item from the "get_root_folder_read_library_items()" result has the same id as an item from the "get_all_library_items_from_db()" results, merge those two items in one single.
|
let db_items = self.get_all_library_items_from_db()?;
|
||||||
// return the merged list
|
|
||||||
todo!()
|
// Merge the two lists. If an item from files has the same id as one from DB, use the DB version
|
||||||
|
let mut merged: Vec<ReadLibraryItem> = Vec::new();
|
||||||
|
|
||||||
|
// First add all DB items (they have IDs)
|
||||||
|
for db_item in db_items {
|
||||||
|
merged.push(db_item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then add file system items only if they don't exist in DB
|
||||||
|
for fs_item in file_system_items {
|
||||||
|
let exists_in_db = merged.iter().any(|db_item| {
|
||||||
|
if let Some(fs_id) = fs_item.id {
|
||||||
|
db_item.id == Some(fs_id)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if !exists_in_db {
|
||||||
|
merged.push(fs_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(merged)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_folder(&self, folder_id: Uuid) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
fn list_folder(&self, folder_id: Uuid) -> Result<Vec<ReadLibraryItem>, VideoError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user