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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
10 additions and
14 deletions
-
daemon/src/routes.rs
|
|
@ -1,7 +1,6 @@ |
|
|
|
use rocket::http::{ContentType, Status}; |
|
|
|
use rust_embed::EmbeddedFile; |
|
|
|
use std::borrow::Cow; |
|
|
|
use std::ffi::OsStr; |
|
|
|
use std::path::PathBuf; |
|
|
|
|
|
|
|
pub trait EmbeddedFileExt { |
|
|
@ -10,18 +9,15 @@ pub trait EmbeddedFileExt { |
|
|
|
|
|
|
|
impl EmbeddedFileExt for Option<EmbeddedFile> { |
|
|
|
fn into_response(self, file: PathBuf) -> Result<(ContentType, Cow<'static, [u8]>), Status> { |
|
|
|
match self { |
|
|
|
None => Err(Status::NotFound), |
|
|
|
Some(embedded_file) => { |
|
|
|
let ext = file |
|
|
|
.as_path() |
|
|
|
.extension() |
|
|
|
.and_then(OsStr::to_str) |
|
|
|
.ok_or_else(|| Status::new(400))?; |
|
|
|
let content_type = |
|
|
|
ContentType::from_extension(ext).ok_or_else(|| Status::new(400))?; |
|
|
|
Ok::<(ContentType, Cow<[u8]>), Status>((content_type, embedded_file.data)) |
|
|
|
} |
|
|
|
} |
|
|
|
let embedded_file = self.ok_or(Status::NotFound)?; |
|
|
|
let ext = file |
|
|
|
.as_path() |
|
|
|
.extension() |
|
|
|
.ok_or(Status::BadRequest)? |
|
|
|
.to_str() |
|
|
|
.ok_or(Status::InternalServerError)?; |
|
|
|
let content_type = ContentType::from_extension(ext).ok_or(Status::InternalServerError)?; |
|
|
|
|
|
|
|
Ok((content_type, embedded_file.data)) |
|
|
|
} |
|
|
|
} |
|
|
|