diff --git a/server/src/main.rs b/server/src/main.rs index 362a94d..7e7c707 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -71,7 +71,7 @@ async fn rocket() -> _ { api::library::move_file, api::library::upload_file, api::library::download_file, api::library::list_files, api::library::get_file, api::library::delete_file, ]) .mount("/", routes![ - ui::user::index, ui::user::list_library_files, ui::user::get_library_file + ui::user::index, ui::user::redirect_list_library_files, ui::user::list_library_files, ui::user::get_library_file ]) .attach(Template::custom(|engines| { let hb = &mut engines.handlebars; diff --git a/server/src/routes/ui/user.rs b/server/src/routes/ui/user.rs index 3a64d3e..081d44a 100644 --- a/server/src/routes/ui/user.rs +++ b/server/src/routes/ui/user.rs @@ -1,14 +1,17 @@ use std::io::Cursor; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; -use rocket::{get, Response, State}; +use log::debug; +use rocket::{get, uri, Response, State}; use rocket::fs::NamedFile; use rocket::http::{ContentType, Header}; use rocket::http::hyper::body::Buf; -use rocket::response::{status, Responder}; +use rocket::response::{status, Redirect, Responder}; use rocket::response::stream::ByteStream; -use rocket::serde::json::Json; +use rocket::serde::json::{json, Json}; use rocket_dyn_templates::{context, Template}; +use serde::Serialize; +use serde_json::Value; use tokio::sync::Mutex; use crate::managers::libraries::LibraryManager; use crate::util::{JsonErrorResponse, ResponseError}; @@ -18,24 +21,61 @@ pub async fn index() -> Template { Template::render("index", context! { test: "value" }) } -#[get("/libraries//<_>/")] +#[get("/library/")] +pub async fn redirect_list_library_files(libraries: &State>>, library_id: &str) + -> Result +{ + let libs = libraries.lock().await; + let library = libs.get(library_id).await?; + // let redirect_to = format!("/library/{}/{}/", library_id, library.model().name); + // debug!("{}", redirect_to); + Ok(Redirect::to(uri!(list_library_files(library_id, library.model().name, "")))) + // Ok(Redirect::to(redirect_to)) +} + + +#[get("/library//<_>/")] pub async fn list_library_files(libraries: &State>>, library_id: &str, path: PathBuf) -> Result { let libs = libraries.lock().await; let library = libs.get(library_id).await?; - let files = library.list_files(&PathBuf::from(path)).await + let files = library.list_files(&PathBuf::from(&path)).await .map_err(|e| ResponseError::InternalServerError(JsonErrorResponse { code: "STORAGE_ERROR".to_string(), message: e.to_string(), }))?; + // TODO: + // parent + let parent = path.clone(); + let mut seg_path = PathBuf::new(); + let segments: Vec = path.iter() + .map(|segment| { + seg_path = seg_path.join(segment); + PathSegmentPiece { + path: seg_path.clone(), + segment: segment.to_string_lossy().into_owned(), + } + }) + .collect(); + debug!("parent={:?}", parent); + debug!("segments={:?}", segments); + Ok(Template::render("libraries", context! { library: library.model(), - files: files + files: files, + parent, + path_segments: segments })) } +#[derive(Debug, Serialize)] +struct PathSegmentPiece { + pub path: PathBuf, + pub segment: String +} + #[derive(Responder)] #[response(status = 200)] struct FileAttachment { @@ -61,15 +101,9 @@ pub async fn get_library_file<'a>(libraries: &State>>, })) } Some(contents) => { - // TODO: headers? let file_name = path.file_name().unwrap().to_string_lossy(); let ext = path.extension().unwrap().to_string_lossy(); let file_type = ContentType::from_extension(&ext); - // let res = Response::build() - // .header(file_type.unwrap_or(ContentType::Binary)) - // .header(Header::new("Content-Disposition", format!("attachment; filename=\"{}\"", file_name))) - // .sized_body(contents.len(), Cursor::new(contents)) - // .finalize(); Ok(FileAttachment { content: contents, content_type: file_type.unwrap_or(ContentType::Binary), diff --git a/server/src/util.rs b/server/src/util.rs index e2dfd3a..6f612e5 100644 --- a/server/src/util.rs +++ b/server/src/util.rs @@ -15,7 +15,7 @@ pub(crate) fn setup_logger() { tracing_subscriber::registry() .with( tracing_subscriber::filter::EnvFilter::try_from_default_env() - .unwrap_or_else(|_| format!("warn,rocket=trace,storage_server=trace").into()), + .unwrap_or_else(|_| format!("warn,rocket=warn,storage_server=trace").into()), ) .with(tracing_subscriber::fmt::layer()) .init(); diff --git a/server/templates/index.html.hbs b/server/templates/index.html.hbs index d49ca46..23c9dc8 100644 --- a/server/templates/index.html.hbs +++ b/server/templates/index.html.hbs @@ -24,7 +24,7 @@ - My Library + My Library diff --git a/server/templates/libraries.html.hbs b/server/templates/libraries.html.hbs index d98b3ab..9177d4a 100644 --- a/server/templates/libraries.html.hbs +++ b/server/templates/libraries.html.hbs @@ -2,8 +2,12 @@