get_structure endpoint
This commit is contained in:
parent
32d207a991
commit
1eba969197
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -1042,9 +1042,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.2.6"
|
version = "2.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
|
checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
@ -1142,6 +1142,7 @@ checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"scopeguard",
|
"scopeguard",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1534,6 +1535,7 @@ dependencies = [
|
|||||||
"futures",
|
"futures",
|
||||||
"jsonwebtoken",
|
"jsonwebtoken",
|
||||||
"oauth2",
|
"oauth2",
|
||||||
|
"parking_lot",
|
||||||
"reqwest 0.12.5",
|
"reqwest 0.12.5",
|
||||||
"serde",
|
"serde",
|
||||||
"sha2",
|
"sha2",
|
||||||
@ -2478,6 +2480,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
|
@ -26,6 +26,7 @@ dotenvy = "0.15"
|
|||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
jsonwebtoken = "9"
|
jsonwebtoken = "9"
|
||||||
oauth2 = "4"
|
oauth2 = "4"
|
||||||
|
parking_lot = { version = "0.12.3", features = ["serde"] }
|
||||||
reqwest = { version = "0.12", features = [
|
reqwest = { version = "0.12", features = [
|
||||||
"http2",
|
"http2",
|
||||||
"rustls-tls",
|
"rustls-tls",
|
||||||
@ -41,7 +42,7 @@ sqlx = { version = "0.8", features = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"uuid",
|
"uuid",
|
||||||
] }
|
] }
|
||||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
tokio = { version = "1", features = ["parking_lot", "rt-multi-thread"] }
|
||||||
tokio-util = { version = "0.7" }
|
tokio-util = { version = "0.7" }
|
||||||
tower = { version = "0.4" }
|
tower = { version = "0.4" }
|
||||||
tower-http = { version = "0.5", features = [
|
tower-http = { version = "0.5", features = [
|
||||||
|
11
sql/get_folders.sql
Normal file
11
sql/get_folders.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
SELECT
|
||||||
|
f.folder_id,
|
||||||
|
owner_id,
|
||||||
|
folder_name,
|
||||||
|
created_at
|
||||||
|
FROM
|
||||||
|
folders f
|
||||||
|
JOIN permissions p ON f.folder_id = p.folder_id
|
||||||
|
WHERE
|
||||||
|
parent_folder_id = $1
|
||||||
|
AND p.user_id = $2
|
@ -5,21 +5,16 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::AppState;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
pub user_id: i32,
|
pub user_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[axum::async_trait]
|
#[axum::async_trait]
|
||||||
impl FromRequestParts<AppState> for Claims {
|
impl<T> FromRequestParts<T> for Claims {
|
||||||
type Rejection = StatusCode;
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
async fn from_request_parts(
|
async fn from_request_parts(parts: &mut Parts, _state: &T) -> Result<Self, Self::Rejection> {
|
||||||
parts: &mut Parts,
|
|
||||||
_state: &AppState,
|
|
||||||
) -> Result<Self, Self::Rejection> {
|
|
||||||
match parts.extract().await {
|
match parts.extract().await {
|
||||||
Ok(Query(claims)) => Ok(claims),
|
Ok(Query(claims)) => Ok(claims),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -33,12 +33,12 @@ pub async fn update(file_id: Uuid, size: i64, hash: Vec<u8>, pool: &Pool) -> sql
|
|||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
||||||
pub struct FileWithoutParentId {
|
pub struct FileWithoutParentId {
|
||||||
file_id: Uuid,
|
pub file_id: Uuid,
|
||||||
file_name: String,
|
pub file_name: String,
|
||||||
file_size: i64,
|
pub file_size: i64,
|
||||||
sha512: String,
|
pub sha512: String,
|
||||||
created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_files(folder_id: Uuid, pool: &Pool) -> sqlx::Result<Vec<FileWithoutParentId>> {
|
pub async fn get_files(folder_id: Uuid, pool: &Pool) -> sqlx::Result<Vec<FileWithoutParentId>> {
|
||||||
|
@ -33,7 +33,7 @@ pub async fn get_root(user_id: i32, pool: &Pool) -> sqlx::Result<Uuid> {
|
|||||||
.map(|row| row.folder_id)
|
.map(|row| row.folder_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Result<Option<Uuid>> {
|
pub async fn process_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Result<Option<Uuid>> {
|
||||||
match id {
|
match id {
|
||||||
Some(id) => get_permissions(id, user_id, pool)
|
Some(id) => get_permissions(id, user_id, pool)
|
||||||
.await
|
.await
|
||||||
@ -45,21 +45,41 @@ pub async fn get_by_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Res
|
|||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
#[allow(clippy::struct_field_names, clippy::module_name_repetitions)]
|
||||||
pub struct FolderWithoutParentId {
|
pub struct FolderWithoutParentId {
|
||||||
folder_id: Uuid,
|
pub folder_id: Uuid,
|
||||||
owner_id: i32,
|
pub owner_id: i32,
|
||||||
folder_name: String,
|
pub folder_name: String,
|
||||||
created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_folders(
|
pub async fn get_by_id(
|
||||||
parent_folder_id: Uuid,
|
folder_id: Uuid,
|
||||||
pool: &Pool,
|
pool: &Pool,
|
||||||
) -> sqlx::Result<Vec<FolderWithoutParentId>> {
|
) -> sqlx::Result<Option<FolderWithoutParentId>> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
FolderWithoutParentId,
|
FolderWithoutParentId,
|
||||||
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE parent_folder_id = $1",
|
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE folder_id = $1",
|
||||||
|
folder_id
|
||||||
|
)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get folders that user can read
|
||||||
|
///
|
||||||
|
/// # Warning
|
||||||
|
///
|
||||||
|
/// This function doesn't check that the user can read the parent folder itself
|
||||||
|
pub async fn get_folders(
|
||||||
|
parent_folder_id: Uuid,
|
||||||
|
user_id: i32,
|
||||||
|
pool: &Pool,
|
||||||
|
) -> sqlx::Result<Vec<FolderWithoutParentId>> {
|
||||||
|
sqlx::query_file_as!(
|
||||||
|
FolderWithoutParentId,
|
||||||
|
"sql/get_folders.sql",
|
||||||
parent_folder_id,
|
parent_folder_id,
|
||||||
)
|
user_id
|
||||||
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
69
src/endpoints/folder/get_structure.rs
Normal file
69
src/endpoints/folder/get_structure.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use tokio::try_join;
|
||||||
|
|
||||||
|
use super::list::Params;
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
pub struct FolderStructure {
|
||||||
|
#[serde(flatten)]
|
||||||
|
folder_base: db::folder::FolderWithoutParentId,
|
||||||
|
folders: Vec<WrappedStructure>,
|
||||||
|
files: Vec<db::file::FileWithoutParentId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
type WrappedStructure = Arc<Mutex<FolderStructure>>;
|
||||||
|
|
||||||
|
impl From<db::folder::FolderWithoutParentId> for WrappedStructure {
|
||||||
|
fn from(value: db::folder::FolderWithoutParentId) -> Self {
|
||||||
|
let fs = 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn structure(
|
||||||
|
Query(params): Query<Params>,
|
||||||
|
State(pool): State<Pool>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<Response>, StatusCode> {
|
||||||
|
let folder_id = db::folder::process_id(params.folder_id, claims.user_id, &pool)
|
||||||
|
.await
|
||||||
|
.handle_internal()?
|
||||||
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
let folder = db::folder::get_by_id(folder_id, &pool)
|
||||||
|
.await
|
||||||
|
.handle_internal()?
|
||||||
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
let response = Response {
|
||||||
|
folder_id,
|
||||||
|
structure: folder.into(),
|
||||||
|
};
|
||||||
|
// TODO: Spawn tasks instead of a single loop
|
||||||
|
let mut stack: Vec<WrappedStructure> = vec![Arc::clone(&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)
|
||||||
|
)
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
Ok(Json(response))
|
||||||
|
}
|
@ -4,7 +4,7 @@ use crate::prelude::*;
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
folder_id: Option<Uuid>,
|
pub(super) folder_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -19,14 +19,14 @@ pub async fn list(
|
|||||||
State(pool): State<Pool>,
|
State(pool): State<Pool>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
) -> Result<Json<Response>, StatusCode> {
|
) -> Result<Json<Response>, StatusCode> {
|
||||||
let folder_id = db::folder::get_by_id(params.folder_id, claims.user_id, &pool)
|
let folder_id = db::folder::process_id(params.folder_id, claims.user_id, &pool)
|
||||||
.await
|
.await
|
||||||
.handle_internal()?
|
.handle_internal()?
|
||||||
.ok_or(StatusCode::NOT_FOUND)?;
|
.ok_or(StatusCode::NOT_FOUND)?;
|
||||||
|
|
||||||
let (files, folders) = try_join!(
|
let (files, folders) = try_join!(
|
||||||
db::file::get_files(folder_id, &pool),
|
db::file::get_files(folder_id, &pool),
|
||||||
db::folder::get_folders(folder_id, &pool)
|
db::folder::get_folders(folder_id, claims.user_id, &pool)
|
||||||
)
|
)
|
||||||
.handle_internal()?;
|
.handle_internal()?;
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pub mod create;
|
pub mod create;
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
|
pub mod get_structure;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
|
@ -13,18 +13,12 @@ use tokio::net::TcpListener;
|
|||||||
|
|
||||||
type Pool = sqlx::postgres::PgPool;
|
type Pool = sqlx::postgres::PgPool;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, FromRef)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
pool: Pool,
|
pool: Pool,
|
||||||
storage: FileStorage,
|
storage: FileStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for Pool {
|
|
||||||
fn from_ref(input: &AppState) -> Self {
|
|
||||||
input.pool.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn create_test_users(pool: &Pool) -> anyhow::Result<()> {
|
async fn create_test_users(pool: &Pool) -> anyhow::Result<()> {
|
||||||
let count = sqlx::query!("SELECT count(user_id) FROM users")
|
let count = sqlx::query!("SELECT count(user_id) FROM users")
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
@ -104,6 +98,7 @@ fn app(state: AppState) -> Router {
|
|||||||
.post(folder::create::create)
|
.post(folder::create::create)
|
||||||
.delete(folder::delete::delete),
|
.delete(folder::delete::delete),
|
||||||
)
|
)
|
||||||
|
.route("/folders/structure", get(folder::get_structure::structure))
|
||||||
.route(
|
.route(
|
||||||
"/permissions",
|
"/permissions",
|
||||||
get(permissions::get::get)
|
get(permissions::get::get)
|
||||||
|
Reference in New Issue
Block a user