http (fs): add file lists

This commit is contained in:
MedzikUser 2022-04-24 22:07:41 +02:00
parent 6e6a25e110
commit 8e2b4c5635
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
6 changed files with 90 additions and 2 deletions

68
server/src/fs/list.rs Normal file
View File

@ -0,0 +1,68 @@
use std::fs;
use axum::{extract::rejection::JsonRejection, Extension, Json};
use axum_auth::AuthBearer;
use homedisk_database::{Database, Error};
use homedisk_types::{
config::types::Config,
errors::{AuthError, FsError, ServerError},
fs::list::{Request, Response},
};
use crate::middleware::{validate_json, validate_jwt};
pub async fn handle(
db: Extension<Database>,
config: Extension<Config>,
AuthBearer(token): AuthBearer,
request: Result<Json<Request>, JsonRejection>,
) -> Result<Json<Response>, ServerError> {
let Json(request) = validate_json::<Request>(request)?;
let token = validate_jwt(config.jwt.secret.as_bytes(), &token)?;
let response = match db.find_user_by_id(token.claims.sub).await {
Ok(res) => {
let user_path = format!(
"{path}/{username}/{request_path}",
path = config.storage.path,
username = res.username,
request_path = request.path,
);
let paths = fs::read_dir(&user_path)
.map_err(|err| ServerError::FsError(FsError::ReadDir(err.to_string())))?;
let mut files = vec![];
let mut dirs = vec![];
for path in paths {
let path = path
.map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?;
let metadata = path
.metadata()
.map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?;
let name = path.path().display().to_string().replace(&user_path, "");
if metadata.is_dir() {
dirs.push(name)
} else {
files.push(name)
}
}
Response { files, dirs }
}
Err(err) => match err {
Error::UserNotFound => return Err(ServerError::AuthError(AuthError::UserNotFound)),
_ => {
return Err(ServerError::AuthError(AuthError::UnknowError(
err.to_string(),
)))
}
},
};
Ok(Json(response))
}

View File

@ -1,7 +1,10 @@
pub mod list;
pub mod upload;
pub fn app() -> axum::Router {
use axum::routing::post;
axum::Router::new().route("/upload", post(upload::handle))
axum::Router::new()
.route("/list", post(list::handle))
.route("/upload", post(upload::handle))
}

View File

@ -5,12 +5,15 @@ pub enum Error {
#[error("file already exists")]
FileAlreadyExists,
#[error("file already exists")]
#[error("write file error - {0}")]
WriteFile(String),
#[error("base64 - {0}")]
Base64(String),
#[error("read dir error - {0}")]
ReadDir(String),
#[error("unknow error")]
UnknowError(String),
}

View File

@ -51,6 +51,7 @@ impl axum::response::IntoResponse for ServerError {
FsError::FileAlreadyExists => StatusCode::BAD_REQUEST,
FsError::WriteFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::Base64(_) => StatusCode::BAD_REQUEST,
FsError::ReadDir(_) => StatusCode::BAD_REQUEST,
FsError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
},
Self::TooManyRequests => StatusCode::TOO_MANY_REQUESTS,

12
types/src/fs/list.rs Normal file
View File

@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Request {
pub path: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Response {
pub files: Vec<String>,
pub dirs: Vec<String>,
}

View File

@ -1 +1,2 @@
pub mod list;
pub mod upload;