Compare commits
No commits in common. "1830e8a2f3b3cb565b352e6d752d03a21c0a7026" and "32d207a991c77c516ef3b535c527712ad6bdb7bb" have entirely different histories.
1830e8a2f3
...
32d207a991
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -1042,9 +1042,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.3.0"
|
version = "2.2.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0"
|
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
@ -2478,7 +2478,6 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
|
@ -41,7 +41,7 @@ sqlx = { version = "0.8", features = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"uuid",
|
"uuid",
|
||||||
] }
|
] }
|
||||||
tokio = { version = "1", features = ["parking_lot", "rt-multi-thread"] }
|
tokio = { version = "1", features = ["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 = [
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
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,16 +5,21 @@ 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<T> FromRequestParts<T> for Claims {
|
impl FromRequestParts<AppState> for Claims {
|
||||||
type Rejection = StatusCode;
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
async fn from_request_parts(parts: &mut Parts, _state: &T) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(
|
||||||
|
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 {
|
||||||
pub file_id: Uuid,
|
file_id: Uuid,
|
||||||
pub file_name: String,
|
file_name: String,
|
||||||
pub file_size: i64,
|
file_size: i64,
|
||||||
pub sha512: String,
|
sha512: String,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
created_at: chrono::NaiveDateTime,
|
||||||
pub updated_at: chrono::NaiveDateTime,
|
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 process_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Result<Option<Uuid>> {
|
pub async fn get_by_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,42 +45,23 @@ pub async fn process_id(id: Option<Uuid>, user_id: i32, pool: &Pool) -> sqlx::Re
|
|||||||
#[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 {
|
||||||
pub folder_id: Uuid,
|
folder_id: Uuid,
|
||||||
pub owner_id: i32,
|
owner_id: i32,
|
||||||
pub folder_name: String,
|
folder_name: String,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
created_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id(
|
pub async fn get_folders(
|
||||||
folder_id: Uuid,
|
parent_folder_id: Uuid,
|
||||||
pool: &Pool,
|
pool: &Pool,
|
||||||
) -> sqlx::Result<Option<FolderWithoutParentId>> {
|
) -> sqlx::Result<Vec<FolderWithoutParentId>> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
FolderWithoutParentId,
|
FolderWithoutParentId,
|
||||||
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE folder_id = $1",
|
"SELECT folder_id, owner_id, folder_name, created_at FROM folders WHERE parent_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 fn get_folders(
|
|
||||||
parent_folder_id: Uuid,
|
|
||||||
user_id: i32,
|
|
||||||
pool: &Pool,
|
|
||||||
) -> impl Stream<Item = sqlx::Result<FolderWithoutParentId>> + '_ {
|
|
||||||
sqlx::query_file_as!(
|
|
||||||
FolderWithoutParentId,
|
|
||||||
"sql/get_folders.sql",
|
|
||||||
parent_folder_id,
|
parent_folder_id,
|
||||||
user_id
|
|
||||||
)
|
)
|
||||||
.fetch(pool)
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result<bool> {
|
pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result<bool> {
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
use futures::TryStreamExt;
|
|
||||||
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<FolderStructure>,
|
|
||||||
files: Vec<db::file::FileWithoutParentId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<db::folder::FolderWithoutParentId> for FolderStructure {
|
|
||||||
fn from(value: db::folder::FolderWithoutParentId) -> Self {
|
|
||||||
FolderStructure {
|
|
||||||
folder_base: value,
|
|
||||||
folders: Vec::new(),
|
|
||||||
files: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
folder_id: Uuid,
|
|
||||||
structure: FolderStructure,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 mut response = Response {
|
|
||||||
folder_id,
|
|
||||||
structure: folder.into(),
|
|
||||||
};
|
|
||||||
// TODO: Spawn tasks instead of a single loop
|
|
||||||
let mut stack: Vec<&mut FolderStructure> = vec![&mut response.structure];
|
|
||||||
while let Some(folder) = stack.pop() {
|
|
||||||
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()?;
|
|
||||||
folder.folders = folders;
|
|
||||||
folder.files = files;
|
|
||||||
stack.extend(folder.folders.iter_mut());
|
|
||||||
}
|
|
||||||
Ok(Json(response))
|
|
||||||
}
|
|
@ -1,11 +1,10 @@
|
|||||||
use futures::TryStreamExt;
|
|
||||||
use tokio::try_join;
|
use tokio::try_join;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub(super) folder_id: Option<Uuid>,
|
folder_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -20,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::process_id(params.folder_id, claims.user_id, &pool)
|
let folder_id = db::folder::get_by_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, claims.user_id, &pool).try_collect()
|
db::folder::get_folders(folder_id, &pool)
|
||||||
)
|
)
|
||||||
.handle_internal()?;
|
.handle_internal()?;
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
pub mod create;
|
pub mod create;
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
pub mod get_structure;
|
|
||||||
pub mod list;
|
pub mod list;
|
||||||
|
@ -13,12 +13,18 @@ use tokio::net::TcpListener;
|
|||||||
|
|
||||||
type Pool = sqlx::postgres::PgPool;
|
type Pool = sqlx::postgres::PgPool;
|
||||||
|
|
||||||
#[derive(Clone, FromRef)]
|
#[derive(Clone)]
|
||||||
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)
|
||||||
@ -98,7 +104,6 @@ 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