From 7ff6e3cab4b6a39f492c08fb62b6d805f5500f11 Mon Sep 17 00:00:00 2001 From: Jackz Date: Mon, 14 Apr 2025 22:40:52 -0500 Subject: [PATCH] Finish moving to hbs --- server/src/main.rs | 7 +++- server/src/objs/library.rs | 4 +++ server/src/routes/ui/user.rs | 26 +++++++++++++-- server/static/css/main.css | 29 +++++++++++++++++ server/templates/layouts/main.html.hbs | 11 ++++--- server/templates/libraries.html.hbs | 38 ++++++++++++++++++++++ server/templates/partials/sidebar.html.hbs | 14 ++++---- 7 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 server/static/css/main.css create mode 100644 server/templates/libraries.html.hbs diff --git a/server/src/main.rs b/server/src/main.rs index 62f6f0d..f936cce 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,6 +2,8 @@ use std::sync::Arc; use log::debug; use rocket::{catch, launch, routes, Request, State}; use rocket::data::ByteUnit; +use rocket::fs::{relative, FileServer}; +use rocket_dyn_templates::handlebars::Handlebars; use rocket_dyn_templates::Template; use sqlx::{migrate, Pool, Postgres}; use sqlx::postgres::PgPoolOptions; @@ -32,6 +34,8 @@ async fn rocket() -> _ { setup_logger(); dotenvy::dotenv().ok(); + let handlebars = Handlebars::new(); + let pool = PgPoolOptions::new() .max_connections(5) .connect(std::env::var("DATABASE_URL").unwrap().as_str()) @@ -56,8 +60,9 @@ async fn rocket() -> _ { .manage(pool) .manage(repo_manager) .manage(libraries_manager) + .mount("/static", FileServer::from(relative!("static"))) .mount("/", routes![ - ui::user::index + ui::user::index, ui::user::list_library_files ]) .mount("/api", routes![ api::library::move_file, api::library::upload_file, api::library::download_file, api::library::list_files, api::library::get_file, api::library::delete_file, diff --git a/server/src/objs/library.rs b/server/src/objs/library.rs index ca83e83..ad12666 100644 --- a/server/src/objs/library.rs +++ b/server/src/objs/library.rs @@ -20,6 +20,10 @@ impl Library { } } + pub fn model(&self) -> &LibraryModel { + &self.model + } + pub async fn write_file(&self, rel_path: &PathBuf, contents: &[u8]) -> Result<(), anyhow::Error> { let mut repo = self.repo.read().await; repo.backend.write_file(&self.model.id.to_string(), rel_path, contents) diff --git a/server/src/routes/ui/user.rs b/server/src/routes/ui/user.rs index cdb5065..3791a31 100644 --- a/server/src/routes/ui/user.rs +++ b/server/src/routes/ui/user.rs @@ -1,7 +1,29 @@ -use rocket::get; +use std::path::PathBuf; +use std::sync::Arc; +use rocket::{get, State}; +use rocket::serde::json::Json; use rocket_dyn_templates::{context, Template}; +use tokio::sync::Mutex; +use crate::managers::libraries::LibraryManager; +use crate::util::{JsonErrorResponse, ResponseError}; #[get("/")] -pub fn index() -> Template { +pub async fn index() -> Template { Template::render("index", context! { test: "value" }) +} + +#[get("/libraries//<_>/")] +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 + .map_err(|e| ResponseError::InternalServerError(JsonErrorResponse { + code: "STORAGE_ERROR".to_string(), + message: e.to_string(), + }))?; + + Ok(Template::render("libraries", context! { + library: library.model(), + files: files + })) } \ No newline at end of file diff --git a/server/static/css/main.css b/server/static/css/main.css new file mode 100644 index 0000000..26f8503 --- /dev/null +++ b/server/static/css/main.css @@ -0,0 +1,29 @@ +.sidebar { + +} +.sidebar-header { + color: hsl(0, 0%, 48%); + padding-left: 8px; +} +.sidebar-list { + /* margin-top: 2px; */ + padding-top: 10px; + padding-left: 5px; + margin-bottom: 25px; +} +.sidebar-list li button { + padding-left: 8px; + padding-top: 10px; + padding-bottom: 10px; + padding-right: 5px; + border-radius: 2.5px; + /* margin-top: 4px; */ + width: 100%; + text-align: left; +} +.sidebar i { + padding-right: 4px; +} +.sidebar-list li button:hover, .sidebar-list li button.is-active { + background-color: lightgray; +} \ No newline at end of file diff --git a/server/templates/layouts/main.html.hbs b/server/templates/layouts/main.html.hbs index ce932b1..88dc1c6 100644 --- a/server/templates/layouts/main.html.hbs +++ b/server/templates/layouts/main.html.hbs @@ -2,15 +2,16 @@ - + storage-app - + + + + + {{> partials/nav }} diff --git a/server/templates/libraries.html.hbs b/server/templates/libraries.html.hbs new file mode 100644 index 0000000..02ea67f --- /dev/null +++ b/server/templates/libraries.html.hbs @@ -0,0 +1,38 @@ +{{#> layouts/main }} +
+

{{ library.name }} > Files

+
+
+
+ Display +
+
+ Sort +
+
+
+
+ + + + + + + + + + + {{#each files }} + + + + + + + {{/each}} + +
Name Size Last Updated Owner
+ {{ file_name }} + Me
+
+{{/layouts/main}} diff --git a/server/templates/partials/sidebar.html.hbs b/server/templates/partials/sidebar.html.hbs index cb3d9a7..8b36f59 100644 --- a/server/templates/partials/sidebar.html.hbs +++ b/server/templates/partials/sidebar.html.hbs @@ -1,9 +1,9 @@ - - -