Browse Source

Merge pull request #142 from comit-network/simplify-embedded-file-ext

fix-bad-api-calls
Thomas Eizinger 3 years ago
committed by GitHub
parent
commit
8915c43661
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      daemon/src/routes.rs

24
daemon/src/routes.rs

@ -1,7 +1,6 @@
use rocket::http::{ContentType, Status}; use rocket::http::{ContentType, Status};
use rust_embed::EmbeddedFile; use rust_embed::EmbeddedFile;
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::PathBuf; use std::path::PathBuf;
pub trait EmbeddedFileExt { pub trait EmbeddedFileExt {
@ -10,18 +9,15 @@ pub trait EmbeddedFileExt {
impl EmbeddedFileExt for Option<EmbeddedFile> { impl EmbeddedFileExt for Option<EmbeddedFile> {
fn into_response(self, file: PathBuf) -> Result<(ContentType, Cow<'static, [u8]>), Status> { fn into_response(self, file: PathBuf) -> Result<(ContentType, Cow<'static, [u8]>), Status> {
match self { let embedded_file = self.ok_or(Status::NotFound)?;
None => Err(Status::NotFound), let ext = file
Some(embedded_file) => { .as_path()
let ext = file .extension()
.as_path() .ok_or(Status::BadRequest)?
.extension() .to_str()
.and_then(OsStr::to_str) .ok_or(Status::InternalServerError)?;
.ok_or_else(|| Status::new(400))?; let content_type = ContentType::from_extension(ext).ok_or(Status::InternalServerError)?;
let content_type =
ContentType::from_extension(ext).ok_or_else(|| Status::new(400))?; Ok((content_type, embedded_file.data))
Ok::<(ContentType, Cow<[u8]>), Status>((content_type, embedded_file.data))
}
}
} }
} }

Loading…
Cancel
Save