From f3f5c40b6bbe54f256b8a002fda6dc80d7b989ba Mon Sep 17 00:00:00 2001 From: George Wu Date: Sat, 21 Feb 2026 16:51:50 -0800 Subject: [PATCH] fix: auto-create home folder when listing root folders returns empty When a user has no home folder (e.g., legacy users or failed folder creation during registration), the frontend would get an empty list from GET /api/folders, leaving userHomeFolderId undefined. This caused uploads to fail or go to the wrong location. Now list_folders_for_owner() automatically creates a home folder when: - Listing root folders (parent_id is None) - The result is empty This self-healing approach fixes the issue at the source, ensuring the frontend always gets a valid userHomeFolderId. --- src/application/services/folder_service.rs | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/application/services/folder_service.rs b/src/application/services/folder_service.rs index 3dc4b74e..04541ca5 100644 --- a/src/application/services/folder_service.rs +++ b/src/application/services/folder_service.rs @@ -231,6 +231,7 @@ impl FolderUseCase for FolderService { } /// Lists folders scoped to a specific owner. + /// Self-healing: if listing root folders and none exist, creates a home folder. async fn list_folders_for_owner( &self, parent_id: Option<&str>, @@ -250,6 +251,34 @@ impl FolderUseCase for FolderService { ) })?; + // Self-healing: if listing root folders and none exist, create a home folder + // This ensures the frontend always gets a valid userHomeFolderId + if parent_id.is_none() && folders.is_empty() { + tracing::info!( + "No root folders found for user {}, creating home folder automatically", + owner_id + ); + let folder_name = format!("My Folder - {}", &owner_id[..8.min(owner_id.len())]); + match self + .folder_storage + .create_home_folder(owner_id, folder_name.clone()) + .await + { + Ok(home_folder) => { + tracing::info!( + "Created home folder '{}' for user {}", + folder_name, + owner_id + ); + return Ok(vec![FolderDto::from(home_folder)]); + } + Err(e) => { + tracing::warn!("Failed to create home folder for user {}: {}", owner_id, e); + // Return empty list rather than failing - user might not have storage quota, etc. + } + } + } + Ok(folders.into_iter().map(FolderDto::from).collect()) }