This commit is contained in:
2024-07-31 12:30:18 +03:00
parent d4c1cdb582
commit ea718be066
9 changed files with 113 additions and 173 deletions

View File

@ -1,5 +1,3 @@
use std::collections::HashSet;
use futures::{Stream, TryStreamExt};
use uuid::Uuid;
@ -19,12 +17,10 @@ pub async fn get_permissions(
Ok(permission.into())
}
pub async fn get_names(folder_id: Uuid, pool: &Pool) -> sqlx::Result<HashSet<String>> {
pub fn get_names(folder_id: Uuid, pool: &Pool) -> impl Stream<Item = sqlx::Result<String>> + '_ {
sqlx::query!("SELECT folder_name as name FROM folders WHERE parent_folder_id = $1 UNION SELECT file_name as name FROM files WHERE folder_id = $1", folder_id)
.fetch(pool)
.map_ok(|record| record.name.unwrap())
.try_collect::<HashSet<String>>()
.await
}
pub async fn get_root(user_id: i32, pool: &Pool) -> sqlx::Result<Uuid> {
@ -68,21 +64,11 @@ pub async fn get_folders(
.await
}
pub async fn exists_by_name(
parent_folder_id: Uuid,
folder_name: &str,
pool: &Pool,
) -> sqlx::Result<bool> {
sqlx::query!(
"SELECT EXISTS(SELECT folder_id FROM folders WHERE parent_folder_id = $1 AND folder_name = $2)",
parent_folder_id,
folder_name
)
.fetch_one(pool)
.await
.and_then(|row| {
row.exists.ok_or(sqlx::Error::RowNotFound)
})
pub async fn name_exists(parent_folder_id: Uuid, name: &str, pool: &Pool) -> sqlx::Result<bool> {
sqlx::query_file!("sql/name_exists.sql", parent_folder_id, name)
.fetch_one(pool)
.await
.map(|row| row.exists.unwrap_or(false))
}
pub async fn insert(

View File

@ -18,7 +18,7 @@ pub async fn modify(
.handle_internal()?
.can_write_guard()?;
// Very weird work around
// Very weird work around to get the first file in multipart
let mut field = loop {
match multipart.next_field().await {
Ok(Some(field)) if field.file_name().is_some() => break field,
@ -34,13 +34,13 @@ pub async fn modify(
.handle_internal()?
.ok_or(StatusCode::NOT_FOUND)?;
let (hash, size) = match crate::FileStorage::write_to_file(&mut file, &mut field).await {
Ok(values) => values,
Err(err) => {
let (hash, size) = crate::FileStorage::write_to_file(&mut file, &mut field)
.await
.map_err(|err| {
tracing::warn!(%err);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
StatusCode::INTERNAL_SERVER_ERROR
})?;
db::file::update(params.file_id, size, hash, &state.pool)
.await
.handle_internal()?;

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use axum::extract::multipart::{self, Multipart};
use futures::TryStreamExt;
use tokio::io::AsyncWrite;
use crate::prelude::*;
@ -42,7 +43,8 @@ pub async fn upload(
.handle_internal()?
.can_write_guard()?;
let existing_names = db::folder::get_names(params.parent_folder, &state.pool)
let existing_names: HashSet<String> = db::folder::get_names(params.parent_folder, &state.pool)
.try_collect()
.await
.handle_internal()?;
let mut result = HashMap::new();

View File

@ -16,10 +16,9 @@ pub async fn create(
.handle_internal()?
.can_write_guard()?;
let exists =
db::folder::exists_by_name(params.parent_folder_id, &params.folder_name, &state.pool)
.await
.handle_internal()?;
let exists = db::folder::name_exists(params.parent_folder_id, &params.folder_name, &state.pool)
.await
.handle_internal()?;
if exists {
return Err(StatusCode::CONFLICT);
}