Attempt to add etag caching
This commit is contained in:
parent
f05e0a15f1
commit
a06c9fae25
|
@ -247,12 +247,6 @@ pub async fn main() {
|
||||||
"get-file",
|
"get-file",
|
||||||
);
|
);
|
||||||
|
|
||||||
router.get(
|
|
||||||
"/:id/tn",
|
|
||||||
files::GetThumbnailHandler { app: app.clone() },
|
|
||||||
"get-thumbnail",
|
|
||||||
);
|
|
||||||
|
|
||||||
router.post("/", files::PostHandler { app: app.clone() }, "upload-file");
|
router.post("/", files::PostHandler { app: app.clone() }, "upload-file");
|
||||||
|
|
||||||
router.delete(
|
router.delete(
|
||||||
|
@ -299,24 +293,35 @@ pub async fn main() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let thumbnail = warp::path!(String / "tn").map({
|
let thumbnail = warp::path!(String / "tn")
|
||||||
let app = app.clone();
|
.and(warp::header::optional::<String>("if-none-match"))
|
||||||
move |id: String| {
|
.map({
|
||||||
let mut content = Vec::new();
|
let app = app.clone();
|
||||||
match app.read().unwrap().get_thumbnail(&id) {
|
move |id: String, old_etags: Option<String>| {
|
||||||
Ok((info, mut stream)) => {
|
let mut content = Vec::new();
|
||||||
let _ = stream.read_to_end(&mut content);
|
match app.read().unwrap().get_thumbnail(&id) {
|
||||||
warp::http::Response::builder()
|
Ok((info, mut stream)) => match old_etags {
|
||||||
.header("content-type", info.file_type)
|
Some(old_etags) if old_etags != info.hash => {
|
||||||
.status(StatusCode::OK)
|
warp::http::Response::builder()
|
||||||
.body(content)
|
.header("content-type", info.file_type)
|
||||||
|
.status(StatusCode::NOT_MODIFIED)
|
||||||
|
.body(content)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let _ = stream.read_to_end(&mut content);
|
||||||
|
warp::http::Response::builder()
|
||||||
|
.header("content-type", info.file_type)
|
||||||
|
.header("etag", info.hash)
|
||||||
|
.status(StatusCode::OK)
|
||||||
|
.body(content)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_err) => warp::http::Response::builder()
|
||||||
|
.status(StatusCode::NOT_FOUND)
|
||||||
|
.body(content),
|
||||||
}
|
}
|
||||||
Err(_err) => warp::http::Response::builder()
|
|
||||||
.status(StatusCode::NOT_FOUND)
|
|
||||||
.body(content),
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let server = warp::serve(root.or(thumbnail));
|
let server = warp::serve(root.or(thumbnail));
|
||||||
server
|
server
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn index(files: Vec<Result<File, FileError>>) -> build_html::HtmlPage {
|
||||||
let tn = Container::new(ContainerType::Div)
|
let tn = Container::new(ContainerType::Div)
|
||||||
.with_attributes(vec![("class", "thumbnail")])
|
.with_attributes(vec![("class", "thumbnail")])
|
||||||
.with_link(
|
.with_link(
|
||||||
format!("/file/{}", file.info().id),
|
format!("/{}", file.info().id),
|
||||||
Image::new(&format!("{}/tn", file.info().id)).to_html_string(),
|
Image::new(&format!("{}/tn", file.info().id)).to_html_string(),
|
||||||
);
|
);
|
||||||
container.add_html(tn);
|
container.add_html(tn);
|
||||||
|
|
Loading…
Reference in New Issue