From eef5cd8e0218c866b6ebcba38bcef2133f6d550a Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Tue, 30 Aug 2022 13:33:15 +0200 Subject: [PATCH] chore: some changes Changed Response in `/auth/login` and `/auth/register`, now access token is in `access_token` instead of `LoggedIn.access_token`. Fixed modified time in `/fs/list` (now returns unix timestamp). --- server/src/auth/login.rs | 3 +-- server/src/auth/register.rs | 2 +- server/src/fs/delete.rs | 1 - server/src/fs/list.rs | 29 +++++++------------------- server/src/fs/upload.rs | 5 +++-- server/src/middleware/validate_json.rs | 2 +- types/src/auth/login.rs | 9 ++------ types/src/errors/server.rs | 4 ++-- types/src/fs/list.rs | 2 +- 9 files changed, 19 insertions(+), 38 deletions(-) diff --git a/server/src/auth/login.rs b/server/src/auth/login.rs index f745f2e..c57cd77 100644 --- a/server/src/auth/login.rs +++ b/server/src/auth/login.rs @@ -25,8 +25,7 @@ pub async fn handle( // create user token let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?; - // Reponse user token - Response::LoggedIn { + Response { access_token: token, } }, diff --git a/server/src/auth/register.rs b/server/src/auth/register.rs index c75b6c9..ebec0ff 100644 --- a/server/src/auth/register.rs +++ b/server/src/auth/register.rs @@ -41,7 +41,7 @@ pub async fn handle( Ok(_result) => { let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?; - Response::LoggedIn { + Response { access_token: token, } }, diff --git a/server/src/fs/delete.rs b/server/src/fs/delete.rs index e8f1815..d1d8365 100644 --- a/server/src/fs/delete.rs +++ b/server/src/fs/delete.rs @@ -47,7 +47,6 @@ pub async fn handle( // delete directory else if path.is_dir() { fs::remove_dir(&path) - // return error .map_err(|err| ServerError::FsError(FsError::DeleteDirectory(err.to_string())))?; } diff --git a/server/src/fs/list.rs b/server/src/fs/list.rs index b9a91ee..38c2e76 100644 --- a/server/src/fs/list.rs +++ b/server/src/fs/list.rs @@ -1,4 +1,4 @@ -use std::{fs, io, path::PathBuf}; +use std::{fs, io, path::PathBuf, time::SystemTime}; use axum::{extract::rejection::JsonRejection, Extension, Json}; use axum_auth::AuthBearer; @@ -91,26 +91,13 @@ pub async fn handle( .get_appropriate_unit(true) .to_string(); - // check how long it has been since the file was last modified - let elapsed = metadata.modified().unwrap().elapsed().unwrap(); - - let seconds = elapsed.as_secs(); - let minutes = seconds / 60; - let hours = minutes / 60; - let days = hours / 24; - - let modified; - - // format elapsed time - if days > 1 { - modified = format!("{} day(s)", days) - } else if hours > 1 { - modified = format!("{} hour(s)", hours) - } else if minutes > 1 { - modified = format!("{} minute(s)", minutes) - } else { - modified = format!("{} second(s)", seconds) - } + // get modification time in unix time format + let modified = metadata + .modified() + .unwrap() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs(); files.push(FileInfo { name, diff --git a/server/src/fs/upload.rs b/server/src/fs/upload.rs index 4a189a8..16842e5 100644 --- a/server/src/fs/upload.rs +++ b/server/src/fs/upload.rs @@ -63,10 +63,11 @@ pub async fn handle( let file = std::fs::File::create(&file_path) .map_err(|err| ServerError::FsError(FsError::CreateFile(err.to_string())))?; - // write file + // write file (chunk by chunk) field .try_fold((file, 0u64), |(mut file, written_len), bytes| async move { - file.write_all(bytes.as_ref()).expect("write file error"); + file.write_all(bytes.as_ref()) + .expect("failed to write chunk to file"); Ok((file, written_len + bytes.len() as u64)) }) diff --git a/server/src/middleware/validate_json.rs b/server/src/middleware/validate_json.rs index 08368ca..05530d4 100644 --- a/server/src/middleware/validate_json.rs +++ b/server/src/middleware/validate_json.rs @@ -7,7 +7,7 @@ pub fn validate_json(payload: Result, JsonRejection>) -> Result Ok(payload), // mission json in Content-Type Header - Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::MissingJsonContentType), + Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::InvalidContentType), // failed to deserialize json Err(JsonRejection::JsonDataError(_)) => Err(ServerError::JsonDataError), // syntax error in json diff --git a/types/src/auth/login.rs b/types/src/auth/login.rs index d1b96e6..bf30f7b 100644 --- a/types/src/auth/login.rs +++ b/types/src/auth/login.rs @@ -5,17 +5,12 @@ use serde::{Deserialize, Serialize}; /// HTTP `/auth/login` Request #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Request { - /// Username pub username: String, - /// Unencrypted user password pub password: String, } /// HTTP `/auth/login` Response #[derive(Debug, Serialize, Deserialize, Clone)] -pub enum Response { - LoggedIn { - /// User access token - access_token: String, - }, +pub struct Response { + pub access_token: String, } diff --git a/types/src/errors/server.rs b/types/src/errors/server.rs index f2f982f..f4afb74 100644 --- a/types/src/errors/server.rs +++ b/types/src/errors/server.rs @@ -13,8 +13,8 @@ pub enum Error { FsError(#[from] FsError), #[error("too may requests, please slow down")] TooManyRequests, - #[error("missing json in Content-Type header")] - MissingJsonContentType, + #[error("invalid Content-Type")] + InvalidContentType, #[error("failed to deserialize json")] JsonDataError, #[error("syntax error in json")] diff --git a/types/src/fs/list.rs b/types/src/fs/list.rs index ef5cf41..f4b392e 100644 --- a/types/src/fs/list.rs +++ b/types/src/fs/list.rs @@ -26,7 +26,7 @@ pub struct FileInfo { /// File size pub size: String, /// Latest modification of this file - pub modified: String, + pub modified: u64, } /// Info about a directory