Skip to content
Snippets Groups Projects
misc.rs 1.4 KiB
Newer Older
use std::{fs::File, io::Read};

Stephen D's avatar
Stephen D committed
use anyhow::{bail, 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();

Stephen D's avatar
Stephen D committed
        let extension = path
            .extension()
            .with_context(|| format!("Could not get extension for {file_name}"))?
            .to_str()
            .context("Extension could not be converted to a string")?;

        let url = slugify(&format!("/{slug_prefix}/{file_name}"));

        let mut data = vec![];
        File::open(path)?.read_to_end(&mut data)?;

        Ok(Self {
            data,
Stephen D's avatar
Stephen D committed
            content_type: content_type(extension)?,
            url,
        })
    }
}

impl Misc {
    pub fn response(self) -> Response {
        Response {
            content_type: self.content_type,
            data: self.data,
        }
    }
}
Stephen D's avatar
Stephen D committed

fn content_type(ext: &str) -> anyhow::Result<&'static str> {
    let c = match ext {
        "webm" => "video/webm",
        "mp4" => "video/mp4",
        _ => bail!("Unsure how to handle extension {ext}"),
    };

    Ok(c)
}