From 73ae729e357042b40023c58cab2e171530cc8aba Mon Sep 17 00:00:00 2001 From: Florian THIERRY Date: Tue, 14 Jul 2026 14:58:01 +0200 Subject: [PATCH] WIP implement list_root_folder in the adapter. --- backend/infrastructure/src/video/adapter.rs | 77 +++++++++++++++++---- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/backend/infrastructure/src/video/adapter.rs b/backend/infrastructure/src/video/adapter.rs index 5eb05d1..8a33027 100644 --- a/backend/infrastructure/src/video/adapter.rs +++ b/backend/infrastructure/src/video/adapter.rs @@ -14,11 +14,41 @@ impl VideoAdapter { } pub fn get_root_folder_read_library_items(&self) -> Result, VideoError> { - // get the root_folder path from the environment variable named "ROOT_FOLDER" - // list all elements inside this folder on the file system - // map all those elements into ReadLibraryItem objects - // return the list - todo!() + use std::env; + use std::fs; + + // Get the root_folder path from environment variable + 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 = 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, VideoError> { @@ -28,13 +58,36 @@ impl VideoAdapter { impl VideoPort for VideoAdapter { fn list_root_folder(&self) -> Result, VideoError> { - // In parallel: - // - call method "get_root_folder_read_library_items()" - // - call method "get_all_library_items_from_db()" - // - // 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. - // return the merged list - todo!() + use std::thread; + + // In parallel: call both methods + let file_system_items = self.get_root_folder_read_library_items()?; + let db_items = self.get_all_library_items_from_db()?; + + // 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 = 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, VideoError> {