From c2f93e5aea5a02049e23c6d4f799d22b9f2becff Mon Sep 17 00:00:00 2001 From: Shimun Date: Sat, 15 Feb 2020 16:43:37 +0100 Subject: [PATCH] allow user provided mime types --- src/main.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7c5fa0d..acb4127 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,8 @@ mod chacha_io; mod pgp; mod snippet; -use crate::snippet::*; use crate::pgp::KnownKeys; +use crate::snippet::*; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use chacha_io::{ChaChaReader, ChaChaWriter}; use chrono::*; @@ -30,9 +30,8 @@ use std::io; use std::io::prelude::*; use std::iter::Iterator; use std::path::{Path, PathBuf}; -use std::sync::Mutex; use std::sync::Arc; - +use std::sync::Mutex; #[cfg(not(debug_assertions))] const STORAGE_DIR: &str = "/snips"; @@ -100,7 +99,15 @@ fn handle(req: &mut Request) -> IronResult { ))) } } - (Method::Get, Some(id)) => { + (Method::Get, Some(path)) => { + let (id, mime) = { + let mut parts = path.split("."); + ( + parts.next().unwrap().to_string(), + parts.next().flat_map(|format| format.parse::().ok()), + ) + }; + let mime = mime.unwrap_or("application/text".parse().unwrap()); let att = storage.open(&id).map(|snip| snip.contents()).map(|res| { Response::with( match res.map(|text| (iron::status::Ok, text)).map_err(|err| { @@ -111,6 +118,7 @@ fn handle(req: &mut Request) -> IronResult { Err(e) => (iron::status::InternalServerError, e), }, ) + .set(mime) }); Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry")))) }