Skip to content
Snippets Groups Projects
misc.rs 950 B
Newer Older
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,
        }
    }
}