diff --git a/Cargo.lock b/Cargo.lock index d2b8f96..def7fdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1142,7 +1142,6 @@ checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" dependencies = [ "autocfg", "scopeguard", - "serde", ] [[package]] @@ -1535,7 +1534,6 @@ dependencies = [ "futures", "jsonwebtoken", "oauth2", - "parking_lot", "reqwest 0.12.5", "serde", "sha2", diff --git a/Cargo.toml b/Cargo.toml index 0cd24ee..54eda93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ dotenvy = "0.15" futures = "0.3" jsonwebtoken = "9" oauth2 = "4" -parking_lot = { version = "0.12.3", features = ["serde"] } reqwest = { version = "0.12", features = [ "http2", "rustls-tls", diff --git a/src/db/folder.rs b/src/db/folder.rs index f85dafb..9420521 100644 --- a/src/db/folder.rs +++ b/src/db/folder.rs @@ -69,19 +69,18 @@ pub async fn get_by_id( /// # Warning /// /// This function doesn't check that the user can read the parent folder itself -pub async fn get_folders( +pub fn get_folders( parent_folder_id: Uuid, user_id: i32, pool: &Pool, -) -> sqlx::Result> { +) -> impl Stream> + '_ { sqlx::query_file_as!( FolderWithoutParentId, "sql/get_folders.sql", parent_folder_id, user_id ) - .fetch_all(pool) - .await + .fetch(pool) } pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result { diff --git a/src/endpoints/folder/get_structure.rs b/src/endpoints/folder/get_structure.rs index eaa0a16..390f0d7 100644 --- a/src/endpoints/folder/get_structure.rs +++ b/src/endpoints/folder/get_structure.rs @@ -1,6 +1,4 @@ -use std::sync::Arc; - -use parking_lot::Mutex; +use futures::TryStreamExt; use tokio::try_join; use super::list::Params; @@ -10,27 +8,24 @@ use crate::prelude::*; pub struct FolderStructure { #[serde(flatten)] folder_base: db::folder::FolderWithoutParentId, - folders: Vec, + folders: Vec, files: Vec, } -type WrappedStructure = Arc>; - -impl From for WrappedStructure { +impl From for FolderStructure { fn from(value: db::folder::FolderWithoutParentId) -> Self { - let fs = FolderStructure { + FolderStructure { folder_base: value, folders: Vec::new(), files: Vec::new(), - }; - Arc::new(Mutex::new(fs)) + } } } #[derive(Debug, Serialize)] pub struct Response { folder_id: Uuid, - structure: WrappedStructure, + structure: FolderStructure, } pub async fn structure( @@ -46,24 +41,23 @@ pub async fn structure( .await .handle_internal()? .ok_or(StatusCode::NOT_FOUND)?; - let response = Response { + let mut response = Response { folder_id, structure: folder.into(), }; // TODO: Spawn tasks instead of a single loop - let mut stack: Vec = vec![Arc::clone(&response.structure)]; + let mut stack: Vec<&mut FolderStructure> = vec![&mut response.structure]; while let Some(folder) = stack.pop() { - let folder_id = folder.lock().folder_base.folder_id; let (files, folders) = try_join!( db::file::get_files(folder_id, &pool), db::folder::get_folders(folder_id, claims.user_id, &pool) + .map_ok(Into::into) + .try_collect() ) .handle_internal()?; - let folders: Vec<_> = folders.into_iter().map(Into::into).collect(); - stack.extend(folders.iter().cloned()); - let mut lock = folder.lock(); - lock.folders = folders; - lock.files = files; + folder.folders = folders; + folder.files = files; + stack.extend(folder.folders.iter_mut()); } Ok(Json(response)) } diff --git a/src/endpoints/folder/list.rs b/src/endpoints/folder/list.rs index dad806e..9156a6d 100644 --- a/src/endpoints/folder/list.rs +++ b/src/endpoints/folder/list.rs @@ -1,3 +1,4 @@ +use futures::TryStreamExt; use tokio::try_join; use crate::prelude::*; @@ -26,7 +27,7 @@ pub async fn list( let (files, folders) = try_join!( db::file::get_files(folder_id, &pool), - db::folder::get_folders(folder_id, claims.user_id, &pool) + db::folder::get_folders(folder_id, claims.user_id, &pool).try_collect() ) .handle_internal()?;