From dab09c4e4d1e4b21216b630bb44e57f597dfe85b Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Tue, 5 Nov 2024 16:33:19 +0100 Subject: Feat: auto file (pdf + epub) creation and synchronization for story --- src/db/story.rs | 28 ++++++++++++++++++++++++++++ src/handlers/admin.rs | 11 +++++++++++ src/main.rs | 6 ++++++ 3 files changed, 45 insertions(+) (limited to 'src') diff --git a/src/db/story.rs b/src/db/story.rs index 686c33f..0680e94 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -1,3 +1,7 @@ +use std::ffi::OsStr; +use std::fs::{read_dir, remove_file}; +use std::io::{BufWriter, Write}; + use axum::async_trait; use maud::html; use sqlx::SqlitePool; @@ -25,6 +29,30 @@ 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(); + if path.extension().is_some_and(|ext| ext == OsStr::new("pdf") || ext == OsStr::new("epub")) { + remove_file(path)?; + } + } + 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 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())?; + } + Ok(()) +} + #[async_trait] impl ProductDatabaseHandler for Story { async fn get_all(pool: &SqlitePool) -> Result, AppError> diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs index af7aa42..6cfe133 100644 --- a/src/handlers/admin.rs +++ b/src/handlers/admin.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::io::{BufWriter, Write}; use anyhow::Context; use axum::body::Bytes; @@ -94,12 +95,22 @@ 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))?; + BufWriter::new(file).write_all(pdf_value)?; pdf_value.to_vec() } else { return Ok(StatusCode::BAD_REQUEST.into_response()); }; 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))?; + BufWriter::new(file).write_all(epub_value)?; epub_value.to_vec() } else { return Ok(StatusCode::BAD_REQUEST.into_response()); diff --git a/src/main.rs b/src/main.rs index ffdce27..dfe1447 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ use std::env; use dotenvy::dotenv; use sqlx::sqlite::SqlitePoolOptions; +use self::db::story; + pub mod db; mod error; mod handlers; @@ -23,6 +25,10 @@ async fn main() { .await .expect("Failed to run migrations"); + story::synchronize_story_files(&pool) + .await + .expect("Failed to synchronize files"); + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, handlers::router(pool).await.into_make_service()) .await -- cgit v1.2.3