From 078498724770fa490de63273e2d8404e99e26f6b Mon Sep 17 00:00:00 2001
From: Stephen <webmaster@scd31.com>
Date: Tue, 7 Feb 2023 21:48:15 -0400
Subject: [PATCH] video working. need to add the correct content type

---
 src/assets/style.css |  6 ++++++
 src/blog.rs          | 11 +++++++++--
 src/main.rs          |  1 +
 src/misc.rs          | 42 ++++++++++++++++++++++++++++++++++++++++++
 src/parser.rs        |  7 +++++++
 5 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 src/misc.rs

diff --git a/src/assets/style.css b/src/assets/style.css
index 56a4acc..05862b1 100644
--- a/src/assets/style.css
+++ b/src/assets/style.css
@@ -40,7 +40,13 @@ a:visited {
 .header-container {
     display: flex;
 }
+
 .header-date {
     white-space: nowrap;
     margin: 1em 1em 1em auto;
 }
+
+img, video {
+    max-width: 100%;
+    max-width: 100%;
+}
diff --git a/src/blog.rs b/src/blog.rs
index 81d12d7..abf7d19 100644
--- a/src/blog.rs
+++ b/src/blog.rs
@@ -2,12 +2,13 @@ use anyhow::Error;
 use std::collections::HashMap;
 use warp::hyper::StatusCode;
 
-use crate::{image::MyImage, load::load_from_path, post::Post, resp::Response};
+use crate::{image::MyImage, load::load_from_path, misc::Misc, post::Post, resp::Response};
 
 pub struct Blog {
     //pages: Vec<Page>,
     posts: Vec<Post>,
     imgs: Vec<MyImage>,
+    misc: Vec<Misc>,
 }
 
 impl Blog {
@@ -20,7 +21,9 @@ impl Blog {
 
         let imgs = load_from_path("assets/img", "")?;
 
-        Ok(Self { posts, imgs })
+        let misc = load_from_path("assets/misc", "misc")?;
+
+        Ok(Self { posts, imgs, misc })
     }
 
     fn home(&self) -> Result<String, Error> {
@@ -77,6 +80,10 @@ impl TryFrom<Blog> for RenderedBlog {
             pages.insert(original_url, original);
         }
 
+        for m in b.misc {
+            pages.insert(m.url.clone(), m.response());
+        }
+
         let not_found = Response::html(dress_page(
             "Page not found",
             include_str!("assets/404.html"),
diff --git a/src/main.rs b/src/main.rs
index daedcbd..6d2f2f1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,7 @@ mod blog;
 mod date;
 mod image;
 mod load;
+mod misc;
 mod parser;
 mod post;
 mod resp;
diff --git a/src/misc.rs b/src/misc.rs
new file mode 100644
index 0000000..b62d64c
--- /dev/null
+++ b/src/misc.rs
@@ -0,0 +1,42 @@
+use std::{fs::File, io::Read};
+
+use anyhow::Context;
+
+use crate::{load::Loadable, resp::Response, util::slugify};
+
+pub struct Misc {
+    pub url: String,
+    content_type: &'static str,
+    data: Vec<u8>,
+}
+
+impl Loadable for Misc {
+    fn load(path: &std::path::Path, slug_prefix: &str) -> anyhow::Result<Self> {
+        let file_name = path
+            .file_name()
+            .context("Could not get file name")?
+            .to_str()
+            .context("Could not convert filename into string")?
+            .to_owned();
+
+        let url = slugify(&format!("/{slug_prefix}/{file_name}"));
+
+        let mut data = vec![];
+        File::open(path)?.read_to_end(&mut data)?;
+
+        Ok(Self {
+            data,
+            content_type: "todo",
+            url,
+        })
+    }
+}
+
+impl Misc {
+    pub fn response(self) -> Response {
+        Response {
+            content_type: self.content_type,
+            data: self.data,
+        }
+    }
+}
diff --git a/src/parser.rs b/src/parser.rs
index 6821d8f..2d58ce7 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -1,6 +1,7 @@
 use anyhow::bail;
 use anyhow::Context;
 use anyhow::Error;
+use orgize::elements::Keyword;
 use orgize::elements::Link;
 use orgize::elements::Timestamp;
 use orgize::{
@@ -89,6 +90,12 @@ impl HtmlHandler<Error> for CustomHtmlHandler {
             Element::Link(Link { path, desc }) if desc.is_none() => {
                 write!(w, r#"<a href="/img/{path}"><img src="/thumb/{path}">"#)?;
             }
+            Element::Keyword(Keyword { key, value, .. }) if key == "VIDEO" => {
+                write!(
+                    w,
+                    r#"<video controls="controls"><source src="{value}"></video>"#
+                )?;
+            }
             // fallback to default handler
             _ => self.fallback.start(w, element)?,
         }
-- 
GitLab