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
let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?;
// Reponse user token
Response::LoggedIn {
Response {
access_token: token,
}
},

View File

@ -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,
}
},

View File

@ -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())))?;
}

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_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,

View File

@ -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))
})

View File

@ -7,7 +7,7 @@ pub fn validate_json<T>(payload: Result<Json<T>, JsonRejection>) -> Result<Json<
// if success return payload
Ok(payload) => 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

View File

@ -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,
}

View File

@ -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")]

View File

@ -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