From 845073129204835413b53bb40ca7b6a7806d373a Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Tue, 5 Nov 2024 19:06:54 +0100 Subject: Feat: Add card content for story - Refactoring: Add filepath formatting method - Add extra error context in story file sync - Add specific product as parameter to product specific card content trait method - TODO: Add pdf icon and epub icon to story card content - Update tailwind config to look in all .rs files in src/ --- src/db/story.rs | 46 ++++++++++++++++++++++++---------------- src/handlers/admin.rs | 14 +++++------- src/handlers/frontend_builder.rs | 7 ++++-- static/styles.css | 2 +- tailwind.config.js | 2 +- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/db/story.rs b/src/db/story.rs index 0680e94..4228e80 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -2,6 +2,7 @@ use std::ffi::OsStr; use std::fs::{read_dir, remove_file}; use std::io::{BufWriter, Write}; +use anyhow::Context; use axum::async_trait; use maud::html; use sqlx::SqlitePool; @@ -30,25 +31,21 @@ impl ProductMarker for Story { } pub async fn synchronize_story_files(pool: &SqlitePool) -> Result<(), anyhow::Error> { - for entry in read_dir("static")? { - let path = entry?.path(); + for entry in read_dir("static").with_context(|| "Failed to read directory")? { + let path = entry.with_context(|| "Failed to get entry")?.path(); if path.extension().is_some_and(|ext| ext == OsStr::new("pdf") || ext == OsStr::new("epub")) { - remove_file(path)?; + remove_file(path).with_context(|| "Failed to remove file")?; } } let results = sqlx::query!("SELECT p.name, s.pdf, s.epub FROM story s INNER JOIN product p ON s.pid = p.id").fetch_all(pool).await?; for result in results.iter() { - let mut pdf_filename = format!("{}.pdf", result.name).to_lowercase(); - pdf_filename.retain(|c| !c.is_whitespace()); + let pdf_filename = format_filepath(&result.name, "pdf"); + let file = std::fs::File::create_new(&pdf_filename).with_context(|| format!("Failed to create {}", &pdf_filename))?; + BufWriter::new(file).write_all(&result.pdf.to_vec()).with_context(|| format!("Failed to write {}", &pdf_filename))?; - let file = std::fs::File::create_new(format!("static/{}", pdf_filename))?; - BufWriter::new(file).write_all(&result.pdf.to_vec())?; - - let mut epub_filename = format!("{}.epub", result.name).to_lowercase(); - epub_filename.retain(|c| !c.is_whitespace()); - - let file = std::fs::File::create_new(format!("static/{}", epub_filename))?; - BufWriter::new(file).write_all(&result.epub.to_vec())?; + let epub_filename = format_filepath(&result.name, "epub"); + let file = std::fs::File::create_new(&epub_filename).with_context(|| format!("Failed to create {}", &epub_filename))?; + BufWriter::new(file).write_all(&result.epub.to_vec()).with_context(|| format!("Failed to write {}", &epub_filename))?; } Ok(()) } @@ -101,13 +98,26 @@ impl ProductDatabaseHandler for Story { impl PageBuilder for Story { fn page_title() -> String { "Stories".to_string() - } + } - // TODO: Add pdf and epub download link + language info - fn product_specific_card_content(&self) -> maud::Markup { + // TODO: Add pdf and epub icons + fn product_specific_card_content + (&self, specific_product: &super::product::SpecificProduct) -> maud::Markup { html!( - span { + div class="flex justify-start mt-5 text-sm" { + span class="mr-10" { (&self.language) } + div class="flex justify-between" { + a href=(format_filepath(&specific_product.product.name, "pdf")) { "PDF" } + a href=(format_filepath(&specific_product.product.name, "epub")) { "EPUB" } + } } - ) + ) } } + +pub fn format_filepath(name: &str, extension: &str) -> String { + let mut filename = format!("static/{}.{}", name, extension).to_lowercase(); + filename.retain(|c| !c.is_whitespace()); + filename +} + diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs index 6cfe133..a48be21 100644 --- a/src/handlers/admin.rs +++ b/src/handlers/admin.rs @@ -11,7 +11,7 @@ use axum::{Extension, Json}; use rand::Rng; use subtle::ConstantTimeEq; -use crate::db::story::Story; +use crate::db::story::{format_filepath, Story}; use crate::db::ProductDatabaseHandler; use crate::error::AppError; @@ -95,10 +95,8 @@ pub async fn upload_story( }; let pdf = if let Some(pdf_value) = form.get("pdf") { - let mut filename = format!("{}.pdf", name).to_lowercase(); - filename.retain(|c| !c.is_whitespace()); - - let file = std::fs::File::create_new(format!("static/{}", filename))?; + let filename = format_filepath(&name, "pdf"); + let file = std::fs::File::create_new(filename)?; BufWriter::new(file).write_all(pdf_value)?; pdf_value.to_vec() } else { @@ -106,10 +104,8 @@ pub async fn upload_story( }; let epub = if let Some(epub_value) = form.get("epub") { - let mut filename = format!("{}.epub", name).to_lowercase(); - filename.retain(|c| !c.is_whitespace()); - - let file = std::fs::File::create_new(format!("static/{}", filename))?; + let filename = format_filepath(&name, "epub"); + let file = std::fs::File::create_new(filename)?; BufWriter::new(file).write_all(epub_value)?; epub_value.to_vec() } else { diff --git a/src/handlers/frontend_builder.rs b/src/handlers/frontend_builder.rs index 4a9ea67..515277e 100644 --- a/src/handlers/frontend_builder.rs +++ b/src/handlers/frontend_builder.rs @@ -75,7 +75,7 @@ fn card(specific_product: SpecificProduct) -> (specific_product.product.description) } div { - (specific_product.detail.product_specific_card_content()) + (specific_product.detail.product_specific_card_content(&specific_product)) } div class="flex flex-row space-x-5 mt-9 text-xs" { span {"Upload: " (upload_date)} @@ -114,5 +114,8 @@ pub async fn build_page String; - fn product_specific_card_content(&self) -> Markup; + fn product_specific_card_content( + &self, + specific_product: &SpecificProduct, + ) -> Markup; } diff --git a/static/styles.css b/static/styles.css index fa5f269..e5b60ae 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1 +1 @@ -/*! tailwindcss v3.4.4 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}:host,html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.static{position:static}.m-12{margin:3rem}.mt-9{margin-top:2.25rem}.flex{display:flex}.grid{display:grid}.grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.flex-row{flex-direction:row}.flex-wrap{flex-wrap:wrap}.content-center{align-content:center}.justify-around{justify-content:space-around}.gap-8{gap:2rem}.space-x-5>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1.25rem*var(--tw-space-x-reverse));margin-left:calc(1.25rem*(1 - var(--tw-space-x-reverse)))}.bg-\[\#eae3d2\]{--tw-bg-opacity:1;background-color:rgb(234 227 210/var(--tw-bg-opacity))}.p-3{padding:.75rem}.p-6{padding:1.5rem}.p-9{padding:2.25rem}.text-center{text-align:center}.font-mono{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}.font-serif{font-family:ui-serif,Georgia,Cambria,Times New Roman,Times,serif}.text-base{font-size:1rem;line-height:1.5rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.text-xs{font-size:.75rem;line-height:1rem}.tracking-wide{letter-spacing:.025em}.tracking-wider{letter-spacing:.05em}.tracking-widest{letter-spacing:.1em}.text-\[\#333\]{--tw-text-opacity:1;color:rgb(51 51 51/var(--tw-text-opacity))}.text-\[\#601B69\]{--tw-text-opacity:1;color:rgb(96 27 105/var(--tw-text-opacity))}.underline{text-decoration-line:underline}.decoration-\[\#601B69\]{text-decoration-color:#601b69}.decoration-\[\#eae3d2\]{text-decoration-color:#eae3d2}.shadow{--tw-shadow:0 1px 3px 0 #0000001a,0 1px 2px -1px #0000001a;--tw-shadow-colored:0 1px 3px 0 var(--tw-shadow-color),0 1px 2px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.hover\:underline:hover{text-decoration-line:underline}@media (prefers-color-scheme:dark){.dark\:bg-\[\#333\]{--tw-bg-opacity:1;background-color:rgb(51 51 51/var(--tw-bg-opacity))}.dark\:text-\[\#E1B1E7\]{--tw-text-opacity:1;color:rgb(225 177 231/var(--tw-text-opacity))}.dark\:text-\[\#eae3d2\]{--tw-text-opacity:1;color:rgb(234 227 210/var(--tw-text-opacity))}.dark\:decoration-\[\#E1B1E7\],.dark\:decoration-\[\#e1b1e7\]{text-decoration-color:#e1b1e7}} \ No newline at end of file +/*! tailwindcss v3.4.4 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}:host,html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.static{position:static}.m-12{margin:3rem}.m-5{margin:1.25rem}.mt-9{margin-top:2.25rem}.mr-5{margin-right:1.25rem}.mr-9{margin-right:2.25rem}.mr-28{margin-right:7rem}.mt-5{margin-top:1.25rem}.mr-32{margin-right:8rem}.mr-10{margin-right:2.5rem}.flex{display:flex}.grid{display:grid}.grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.flex-row{flex-direction:row}.flex-wrap{flex-wrap:wrap}.content-center{align-content:center}.justify-start{justify-content:flex-start}.justify-between{justify-content:space-between}.justify-around{justify-content:space-around}.gap-8{gap:2rem}.space-x-5>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1.25rem*var(--tw-space-x-reverse));margin-left:calc(1.25rem*(1 - var(--tw-space-x-reverse)))}.space-x-4>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1rem*var(--tw-space-x-reverse));margin-left:calc(1rem*(1 - var(--tw-space-x-reverse)))}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem*var(--tw-space-y-reverse))}.space-y-6>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1.5rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1.5rem*var(--tw-space-y-reverse))}.space-x-6>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1.5rem*var(--tw-space-x-reverse));margin-left:calc(1.5rem*(1 - var(--tw-space-x-reverse)))}.border{border-width:1px}.bg-\[\#eae3d2\]{--tw-bg-opacity:1;background-color:rgb(234 227 210/var(--tw-bg-opacity))}.p-3{padding:.75rem}.p-6{padding:1.5rem}.p-9{padding:2.25rem}.text-center{text-align:center}.font-mono{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}.font-serif{font-family:ui-serif,Georgia,Cambria,Times New Roman,Times,serif}.text-base{font-size:1rem;line-height:1.5rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.text-xs{font-size:.75rem;line-height:1rem}.tracking-wide{letter-spacing:.025em}.tracking-wider{letter-spacing:.05em}.tracking-widest{letter-spacing:.1em}.text-\[\#333\]{--tw-text-opacity:1;color:rgb(51 51 51/var(--tw-text-opacity))}.text-\[\#601B69\]{--tw-text-opacity:1;color:rgb(96 27 105/var(--tw-text-opacity))}.underline{text-decoration-line:underline}.decoration-\[\#601B69\]{text-decoration-color:#601b69}.decoration-\[\#eae3d2\]{text-decoration-color:#eae3d2}.shadow{--tw-shadow:0 1px 3px 0 #0000001a,0 1px 2px -1px #0000001a;--tw-shadow-colored:0 1px 3px 0 var(--tw-shadow-color),0 1px 2px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.hover\:underline:hover{text-decoration-line:underline}@media (prefers-color-scheme:dark){.dark\:border-\[E1B1E7\]{border-color:E1B1E7}.dark\:border-\[\#E1B1E7\]{--tw-border-opacity:1;border-color:rgb(225 177 231/var(--tw-border-opacity))}.dark\:bg-\[\#333\]{--tw-bg-opacity:1;background-color:rgb(51 51 51/var(--tw-bg-opacity))}.dark\:text-\[\#E1B1E7\]{--tw-text-opacity:1;color:rgb(225 177 231/var(--tw-text-opacity))}.dark\:text-\[\#eae3d2\]{--tw-text-opacity:1;color:rgb(234 227 210/var(--tw-text-opacity))}.dark\:decoration-\[\#E1B1E7\],.dark\:decoration-\[\#e1b1e7\]{text-decoration-color:#e1b1e7}.dark\:shadow-\[\#E1B1E7\]{--tw-shadow-color:#e1b1e7;--tw-shadow:var(--tw-shadow-colored)}} \ No newline at end of file diff --git a/tailwind.config.js b/tailwind.config.js index b25370d..d8a3a8c 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,6 +1,6 @@ /** @type {import('tailwindcss').Config} */ module.exports = { - content: ["./src/handlers/**.rs"], + content: ["./src/**/**.rs"], theme: { extend: {}, }, -- cgit v1.2.3