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).
This commit is contained in:
MedzikUser 2022-08-30 13:33:15 +02:00
parent dc8ccff079
commit eef5cd8e02
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
9 changed files with 19 additions and 38 deletions

View File

@ -25,8 +25,7 @@ pub async fn handle(
// create user token // create user token
let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?; let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?;
// Reponse user token Response {
Response::LoggedIn {
access_token: token, access_token: token,
} }
}, },

View File

@ -41,7 +41,7 @@ pub async fn handle(
Ok(_result) => { Ok(_result) => {
let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?; let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?;
Response::LoggedIn { Response {
access_token: token, access_token: token,
} }
}, },

View File

@ -47,7 +47,6 @@ pub async fn handle(
// delete directory // delete directory
else if path.is_dir() { else if path.is_dir() {
fs::remove_dir(&path) fs::remove_dir(&path)
// return error
.map_err(|err| ServerError::FsError(FsError::DeleteDirectory(err.to_string())))?; .map_err(|err| ServerError::FsError(FsError::DeleteDirectory(err.to_string())))?;
} }

View File

@ -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::{extract::rejection::JsonRejection, Extension, Json};
use axum_auth::AuthBearer; use axum_auth::AuthBearer;
@ -91,26 +91,13 @@ pub async fn handle(
.get_appropriate_unit(true) .get_appropriate_unit(true)
.to_string(); .to_string();
// check how long it has been since the file was last modified // get modification time in unix time format
let elapsed = metadata.modified().unwrap().elapsed().unwrap(); let modified = metadata
.modified()
let seconds = elapsed.as_secs(); .unwrap()
let minutes = seconds / 60; .duration_since(SystemTime::UNIX_EPOCH)
let hours = minutes / 60; .unwrap()
let days = hours / 24; .as_secs();
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)
}
files.push(FileInfo { files.push(FileInfo {
name, name,

View File

@ -63,10 +63,11 @@ pub async fn handle(
let file = std::fs::File::create(&file_path) let file = std::fs::File::create(&file_path)
.map_err(|err| ServerError::FsError(FsError::CreateFile(err.to_string())))?; .map_err(|err| ServerError::FsError(FsError::CreateFile(err.to_string())))?;
// write file // write file (chunk by chunk)
field field
.try_fold((file, 0u64), |(mut file, written_len), bytes| async move { .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)) Ok((file, written_len + bytes.len() as u64))
}) })

View File

@ -7,7 +7,7 @@ pub fn validate_json<T>(payload: Result<Json<T>, JsonRejection>) -> Result<Json<
// if success return payload // if success return payload
Ok(payload) => Ok(payload), Ok(payload) => Ok(payload),
// mission json in Content-Type Header // mission json in Content-Type Header
Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::MissingJsonContentType), Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::InvalidContentType),
// failed to deserialize json // failed to deserialize json
Err(JsonRejection::JsonDataError(_)) => Err(ServerError::JsonDataError), Err(JsonRejection::JsonDataError(_)) => Err(ServerError::JsonDataError),
// syntax error in json // syntax error in json

View File

@ -5,17 +5,12 @@ use serde::{Deserialize, Serialize};
/// HTTP `/auth/login` Request /// HTTP `/auth/login` Request
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Request { pub struct Request {
/// Username
pub username: String, pub username: String,
/// Unencrypted user password
pub password: String, pub password: String,
} }
/// HTTP `/auth/login` Response /// HTTP `/auth/login` Response
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Response { pub struct Response {
LoggedIn { pub access_token: String,
/// User access token
access_token: String,
},
} }

View File

@ -13,8 +13,8 @@ pub enum Error {
FsError(#[from] FsError), FsError(#[from] FsError),
#[error("too may requests, please slow down")] #[error("too may requests, please slow down")]
TooManyRequests, TooManyRequests,
#[error("missing json in Content-Type header")] #[error("invalid Content-Type")]
MissingJsonContentType, InvalidContentType,
#[error("failed to deserialize json")] #[error("failed to deserialize json")]
JsonDataError, JsonDataError,
#[error("syntax error in json")] #[error("syntax error in json")]

View File

@ -26,7 +26,7 @@ pub struct FileInfo {
/// File size /// File size
pub size: String, pub size: String,
/// Latest modification of this file /// Latest modification of this file
pub modified: String, pub modified: u64,
} }
/// Info about a directory /// Info about a directory