resource get method
This commit is contained in:
parent
33af28da8a
commit
ebbea87532
|
@ -74,7 +74,7 @@ impl Resource {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: &mut Self) -> Result<Vec<u8>, Box<dyn Error>> {
|
pub fn get(self: &Self) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||||
if let Some(cached) = self.cached_data.clone() {
|
if let Some(cached) = self.cached_data.clone() {
|
||||||
Ok(cached)
|
Ok(cached)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -14,6 +14,7 @@ mod rejection;
|
||||||
mod api;
|
mod api;
|
||||||
mod uploaded;
|
mod uploaded;
|
||||||
mod curlapi;
|
mod curlapi;
|
||||||
|
mod resource;
|
||||||
|
|
||||||
use state::SharedState;
|
use state::SharedState;
|
||||||
|
|
||||||
|
@ -23,7 +24,8 @@ pub fn routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error = R
|
||||||
.or(pages::get_routes(state.clone()))
|
.or(pages::get_routes(state.clone()))
|
||||||
.or(forms::get_routes(state.clone()))
|
.or(forms::get_routes(state.clone()))
|
||||||
.or(api::get_routes(state.clone()))
|
.or(api::get_routes(state.clone()))
|
||||||
.or(uploaded::get_uploaded(state))
|
.or(uploaded::get_uploaded(state.clone()))
|
||||||
|
.or(resource::get_routes(state))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -88,6 +88,18 @@ pub struct ErrorPage {
|
||||||
pub link_text: Option<String>
|
pub link_text: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ErrorPage {
|
||||||
|
pub fn not_found(state: SharedState) -> Self {
|
||||||
|
ErrorPage {
|
||||||
|
env: state.env,
|
||||||
|
conf: state.config,
|
||||||
|
error_text: "404 Not Found".into(),
|
||||||
|
link: Some("/".into()),
|
||||||
|
link_text: Some("Go back".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template( path = "curlapi_help.html" )]
|
#[template( path = "curlapi_help.html" )]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
use askama::Template;
|
||||||
|
use warp::{Filter, reply::Reply, reject::Rejection};
|
||||||
|
use warp::{reply::{with_status, with_header, html}, http::StatusCode};
|
||||||
|
|
||||||
|
use super::pages::ErrorPage;
|
||||||
|
use super::rejection::HttpReject;
|
||||||
|
use super::state::SharedState;
|
||||||
|
|
||||||
|
fn not_found_res(state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
|
||||||
|
let nf = ErrorPage::not_found(state.clone());
|
||||||
|
Ok(
|
||||||
|
Box::new(
|
||||||
|
with_status(
|
||||||
|
html(nf.render().map_err(|x| HttpReject::AskamaError(x))?),
|
||||||
|
StatusCode::NOT_FOUND
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_resource((state, id): (SharedState, String)) -> Result<Box<dyn Reply>, Rejection> {
|
||||||
|
if let Some(ref store) = state.config.resources {
|
||||||
|
if let Some(ref res) = store.get(id) {
|
||||||
|
Ok(
|
||||||
|
Box::new(
|
||||||
|
with_header(
|
||||||
|
res.get().map_err(|x| HttpReject::StringError(x.to_string()))?,
|
||||||
|
"Content-Type",
|
||||||
|
res.mime.clone()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
not_found_res(state)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
not_found_res(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_filter(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path!("resource" / "get" / String)
|
||||||
|
.map(move |id| (state.clone(), id))
|
||||||
|
.and_then(get_resource)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
get_filter(state)
|
||||||
|
}
|
Loading…
Reference in New Issue