From 7078d2c80917d9e212ee118358893e3c2f132ed0 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 31 Mar 2024 21:55:04 +0200 Subject: [PATCH] add basic pdf support --- src/misc/markdown.rs | 10 +--------- src/misc/utils.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/routes/blog.rs | 6 ++---- src/routes/contact.rs | 4 ++-- src/routes/cours.rs | 4 ++-- src/routes/index.rs | 4 ++-- src/routes/portfolio.rs | 4 ++-- 7 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index 65f91ee..b607d77 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -228,7 +228,7 @@ fn fix_local_img(path: &str, html: String) -> String { } /// Transform markdown string to File structure -fn read(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File { +pub fn read_md(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File { let arena = Arena::new(); let options = get_options(); @@ -260,14 +260,6 @@ fn read(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File { } } -/// Read markdown file -pub fn read_file(filename: &str, expected_file: TypeFileMetadata) -> Option { - match fs::read_to_string(filename) { - Ok(text) => Some(read(filename, &text, expected_file)), - _ => None, - } -} - /// Deserialize metadata based on a type fn deserialize_metadata(text: &str) -> T { serde_yml::from_str(text.trim().trim_matches(&['-'] as &[_])).unwrap_or_default() diff --git a/src/misc/utils.rs b/src/misc/utils.rs index 4a3feff..a6f4755 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,13 +1,18 @@ +use std::{fs, path::Path}; + use actix_web::{ http::header::{self, ContentType, TryIntoHeaderValue}, http::StatusCode, HttpRequest, HttpResponse, Responder, }; +use base64::{engine::general_purpose, Engine}; use cached::proc_macro::cached; use reqwest::Client; use crate::config::FileConfig; +use super::markdown::{read_md, File, FileMetadata, Metadata, TypeFileMetadata}; + #[cached] pub fn get_reqwest_client() -> Client { Client::builder() @@ -46,3 +51,40 @@ impl Responder for Html { res } } + +/// Read a file +pub fn read_file(filename: &str, expected_file: TypeFileMetadata) -> Option { + match Path::new(filename).extension() { + Some(ext) => match ext.to_str().unwrap() { + "pdf" => match fs::read(filename) { + Ok(bytes) => Some(read_pdf(bytes)), + Err(_) => None, + }, + _ => match fs::read_to_string(filename) { + Ok(text) => Some(read_md(filename, &text, expected_file)), + Err(_) => None, + }, + }, + None => None, + } +} + +fn read_pdf(data: Vec) -> File { + let pdf = general_purpose::STANDARD.encode(data); + + File { + metadata: Metadata { + info: FileMetadata::default(), + mermaid: false, + syntax_highlight: false, + math: false, + }, + content: format!( + r#""#, + pdf + ), + } +} diff --git a/src/routes/blog.rs b/src/routes/blog.rs index aec3019..36b38a0 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -18,10 +18,8 @@ use crate::{ config::Config, misc::{ date::Date, - markdown::{ - get_metadata, get_options, read_file, File, FileMetadataBlog, TypeFileMetadata, - }, - utils::{get_url, make_kw, Html}, + markdown::{get_metadata, get_options, File, FileMetadataBlog, TypeFileMetadata}, + utils::{get_url, make_kw, read_file, Html}, }, template::{Infos, NavBar}, }; diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 5debdb9..142886b 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -7,8 +7,8 @@ use std::fs::read_to_string; use crate::{ config::Config, misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::{make_kw, Html}, + markdown::{File, TypeFileMetadata}, + utils::{make_kw, read_file, Html}, }, template::{Infos, NavBar}, }; diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 1474baa..9451825 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize}; use crate::{ config::Config, misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::{make_kw, Html}, + markdown::{File, TypeFileMetadata}, + utils::{make_kw, read_file, Html}, }, template::{Infos, NavBar}, }; diff --git a/src/routes/index.rs b/src/routes/index.rs index cb23ad1..4322953 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -5,8 +5,8 @@ use ramhorns::Content; use crate::{ config::Config, misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::{make_kw, Html}, + markdown::{File, TypeFileMetadata}, + utils::{make_kw, read_file, Html}, }, template::{Infos, NavBar}, }; diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index b528002..170b9fd 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -6,8 +6,8 @@ use ramhorns::Content; use crate::{ config::Config, misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::{make_kw, Html}, + markdown::{File, TypeFileMetadata}, + utils::{make_kw, read_file, Html}, }, template::{Infos, NavBar}, };