allow user provided mime types
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
Shimun 2020-02-15 16:43:37 +01:00
parent acfe506f51
commit c2f93e5aea
Signed by: shimun
GPG Key ID: E81D8382DC2F971B

View File

@ -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<Response> {
)))
}
}
(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::<Mime>().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<Response> {
Err(e) => (iron::status::InternalServerError, e),
},
)
.set(mime)
});
Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry"))))
}