server (/fs/list): return filesize

This commit is contained in:
MedzikUser 2022-04-29 21:33:44 +02:00
parent ad5a088a51
commit e8f8a4b007
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
6 changed files with 33 additions and 5 deletions

16
Cargo.lock generated
View File

@ -218,6 +218,15 @@ version = "3.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
[[package]]
name = "byte-unit"
version = "4.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95ebf10dda65f19ff0f42ea15572a359ed60d7fc74fdc984d90310937be0014b"
dependencies = [
"utf8-width",
]
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.4.3"
@ -623,6 +632,7 @@ dependencies = [
"axum", "axum",
"axum-auth", "axum-auth",
"base64", "base64",
"byte-unit",
"homedisk-database", "homedisk-database",
"homedisk-types", "homedisk-types",
"hyper", "hyper",
@ -1828,6 +1838,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.0.0" version = "1.0.0"

View File

@ -16,3 +16,4 @@ homedisk-types = { path = "../types", features = ["axum"] }
axum-auth = "0.2.0" axum-auth = "0.2.0"
jsonwebtoken = "8.1.0" jsonwebtoken = "8.1.0"
base64 = "0.13.0" base64 = "0.13.0"
byte-unit = "4.0.14"

View File

@ -2,11 +2,12 @@ use std::fs;
use axum::{extract::rejection::JsonRejection, Extension, Json}; use axum::{extract::rejection::JsonRejection, Extension, Json};
use axum_auth::AuthBearer; use axum_auth::AuthBearer;
use byte_unit::Byte;
use homedisk_database::{Database, Error}; use homedisk_database::{Database, Error};
use homedisk_types::{ use homedisk_types::{
config::types::Config, config::types::Config,
errors::{AuthError, FsError, ServerError}, errors::{AuthError, FsError, ServerError},
fs::list::{Request, Response}, fs::list::{FileInfo, Request, Response},
}; };
use crate::middleware::{validate_json, validate_jwt}; use crate::middleware::{validate_json, validate_jwt};
@ -43,11 +44,15 @@ pub async fn handle(
.map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?; .map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?;
let name = path.path().display().to_string().replace(&user_path, ""); let name = path.path().display().to_string().replace(&user_path, "");
let filesize = Byte::from_bytes(metadata.len().into()).get_appropriate_unit(true);
if metadata.is_dir() { if metadata.is_dir() {
dirs.push(name) dirs.push(name)
} else { } else {
files.push(name) files.push(FileInfo {
name,
size: filesize.to_string(),
})
} }
} }

View File

@ -8,7 +8,7 @@ use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
use homedisk_database::Database; use homedisk_database::Database;
use homedisk_types::config::types::Config; use homedisk_types::config::types::Config;
use log::{debug, info}; use log::{debug, info};
use tower_http::cors::{CorsLayer, AllowOrigin}; use tower_http::cors::{AllowOrigin, CorsLayer};
async fn health_check() -> &'static str { async fn health_check() -> &'static str {
"I'm alive!" "I'm alive!"

View File

@ -7,7 +7,7 @@ pub use fs::Error as FsError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)] #[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
#[serde(tag = "error", content = "error-message", rename_all = "kebab-case")] #[serde(tag = "error", content = "error_message", rename_all = "kebab-case")]
pub enum ServerError { pub enum ServerError {
#[error("auth error: {0}")] #[error("auth error: {0}")]
AuthError(#[from] AuthError), AuthError(#[from] AuthError),

View File

@ -7,6 +7,12 @@ pub struct Request {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Response { pub struct Response {
pub files: Vec<String>, pub files: Vec<FileInfo>,
pub dirs: Vec<String>, pub dirs: Vec<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FileInfo {
pub name: String,
pub size: String,
}