From ea718be0661c6984669cb60949b2b015c0dd6817 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Wed, 31 Jul 2024 12:30:18 +0300 Subject: [PATCH] Changes --- ...833ea61970ea7f901dd57c0715ae3dbddd164.json | 36 +++++ ...ec3a632ce125fdb092dbd17630e952d4e0d9e.json | 34 +++++ Cargo.lock | 139 ------------------ Cargo.toml | 6 +- sql/name_exists.sql | 18 +++ src/db/folder.rs | 26 +--- src/endpoints/file/modify.rs | 14 +- src/endpoints/file/upload.rs | 6 +- src/endpoints/folder/create.rs | 7 +- 9 files changed, 113 insertions(+), 173 deletions(-) create mode 100644 .sqlx/query-347a486f9ea5183b1c4c16234a1833ea61970ea7f901dd57c0715ae3dbddd164.json create mode 100644 .sqlx/query-fd53e5f2c7e8aa87f3cf4e430a6ec3a632ce125fdb092dbd17630e952d4e0d9e.json create mode 100644 sql/name_exists.sql diff --git a/.sqlx/query-347a486f9ea5183b1c4c16234a1833ea61970ea7f901dd57c0715ae3dbddd164.json b/.sqlx/query-347a486f9ea5183b1c4c16234a1833ea61970ea7f901dd57c0715ae3dbddd164.json new file mode 100644 index 0000000..704b367 --- /dev/null +++ b/.sqlx/query-347a486f9ea5183b1c4c16234a1833ea61970ea7f901dd57c0715ae3dbddd164.json @@ -0,0 +1,36 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE users SET username = $2, email = $3 WHERE user_id = $1 RETURNING *", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "user_id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "username", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "email", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Int4", + "Varchar", + "Varchar" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "347a486f9ea5183b1c4c16234a1833ea61970ea7f901dd57c0715ae3dbddd164" +} diff --git a/.sqlx/query-fd53e5f2c7e8aa87f3cf4e430a6ec3a632ce125fdb092dbd17630e952d4e0d9e.json b/.sqlx/query-fd53e5f2c7e8aa87f3cf4e430a6ec3a632ce125fdb092dbd17630e952d4e0d9e.json new file mode 100644 index 0000000..90a42bf --- /dev/null +++ b/.sqlx/query-fd53e5f2c7e8aa87f3cf4e430a6ec3a632ce125fdb092dbd17630e952d4e0d9e.json @@ -0,0 +1,34 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT user_id, username, email FROM users WHERE user_id = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "user_id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "username", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "email", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "fd53e5f2c7e8aa87f3cf4e430a6ec3a632ce125fdb092dbd17630e952d4e0d9e" +} diff --git a/Cargo.lock b/Cargo.lock index a175989..baea03d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -572,21 +572,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1002,22 +987,6 @@ dependencies = [ "webpki-roots 0.26.3", ] -[[package]] -name = "hyper-tls" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" -dependencies = [ - "bytes", - "http-body-util", - "hyper 1.4.1", - "hyper-util", - "native-tls", - "tokio", - "tokio-native-tls", - "tower-service", -] - [[package]] name = "hyper-util" version = "0.1.6" @@ -1262,23 +1231,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "native-tls" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" -dependencies = [ - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", -] - [[package]] name = "nom" version = "7.1.3" @@ -1397,50 +1349,6 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -[[package]] -name = "openssl" -version = "0.10.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" -dependencies = [ - "bitflags 2.6.0", - "cfg-if", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.72", -] - -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "overload" version = "0.1.1" @@ -1838,7 +1746,6 @@ checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" dependencies = [ "base64 0.22.1", "bytes", - "encoding_rs", "futures-core", "futures-util", "h2 0.4.5", @@ -1847,13 +1754,11 @@ dependencies = [ "http-body-util", "hyper 1.4.1", "hyper-rustls 0.27.2", - "hyper-tls", "hyper-util", "ipnet", "js-sys", "log", "mime", - "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -1865,9 +1770,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper 1.0.1", - "system-configuration", "tokio", - "tokio-native-tls", "tokio-rustls 0.26.0", "tower-service", "url", @@ -2022,15 +1925,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" -[[package]] -name = "schannel" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "scopeguard" version = "1.2.0" @@ -2047,29 +1941,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "security-framework" -version = "2.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" -dependencies = [ - "bitflags 2.6.0", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", -] - -[[package]] -name = "security-framework-sys" -version = "2.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "serde" version = "1.0.204" @@ -2625,16 +2496,6 @@ dependencies = [ "syn 2.0.72", ] -[[package]] -name = "tokio-native-tls" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" -dependencies = [ - "native-tls", - "tokio", -] - [[package]] name = "tokio-rustls" version = "0.24.1" diff --git a/Cargo.toml b/Cargo.toml index d020e7d..958cd6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,11 @@ dotenvy = "0.15" futures = "0.3" jsonwebtoken = "9" oauth2 = "4" -reqwest = { version = "0.12", features = ["http2", "rustls-tls", "json"] } +reqwest = { version = "0.12", features = [ + "http2", + "rustls-tls", + "json", +], default-features = false } serde = { version = "1", features = ["derive"] } sha2 = "0.10" sqlx = { version = "0.8", features = [ diff --git a/sql/name_exists.sql b/sql/name_exists.sql new file mode 100644 index 0000000..4ebba07 --- /dev/null +++ b/sql/name_exists.sql @@ -0,0 +1,18 @@ +SELECT + EXISTS ( + SELECT + file_name as name + FROM + files + WHERE + folder_id = $1 + AND file_name = $2 + UNION + SELECT + folder_name as name + FROM + folders + WHERE + parent_folder_id = $1 + AND folder_name = $2 + ) \ No newline at end of file diff --git a/src/db/folder.rs b/src/db/folder.rs index 55e0afe..d7f9f3b 100644 --- a/src/db/folder.rs +++ b/src/db/folder.rs @@ -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> { +pub fn get_names(folder_id: Uuid, pool: &Pool) -> impl Stream> + '_ { 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::>() - .await } pub async fn get_root(user_id: i32, pool: &Pool) -> sqlx::Result { @@ -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 { - 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 { + 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( diff --git a/src/endpoints/file/modify.rs b/src/endpoints/file/modify.rs index a40a5bb..e696faf 100644 --- a/src/endpoints/file/modify.rs +++ b/src/endpoints/file/modify.rs @@ -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()?; diff --git a/src/endpoints/file/upload.rs b/src/endpoints/file/upload.rs index 70de9f2..1d47c4b 100644 --- a/src/endpoints/file/upload.rs +++ b/src/endpoints/file/upload.rs @@ -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 = db::folder::get_names(params.parent_folder, &state.pool) + .try_collect() .await .handle_internal()?; let mut result = HashMap::new(); diff --git a/src/endpoints/folder/create.rs b/src/endpoints/folder/create.rs index 36eae70..9f7719f 100644 --- a/src/endpoints/folder/create.rs +++ b/src/endpoints/folder/create.rs @@ -16,10 +16,9 @@ pub async fn create( .handle_internal()? .can_write_guard()?; - let exists = - db::folder::exists_by_name(params.parent_folder_id, ¶ms.folder_name, &state.pool) - .await - .handle_internal()?; + let exists = db::folder::name_exists(params.parent_folder_id, ¶ms.folder_name, &state.pool) + .await + .handle_internal()?; if exists { return Err(StatusCode::CONFLICT); }