Refactor file and thumbnail serving to common code
This commit is contained in:
parent
d0c5e0a59f
commit
f451df3a79
|
@ -43,7 +43,7 @@ impl App {
|
|||
FileInfo::open(&id, &self.files_root)
|
||||
}
|
||||
|
||||
pub fn get_file(&self, id: String) -> Result<(FileInfo, std::fs::File), FileError> {
|
||||
pub fn get_file(&self, id: &str) -> Result<(FileInfo, std::fs::File), FileError> {
|
||||
let f = File::open(&id, &self.files_root)?;
|
||||
let info = f.info();
|
||||
let stream = f.stream()?;
|
||||
|
|
|
@ -221,6 +221,28 @@ fn script(_: &mut Request) -> IronResult<Response> {
|
|||
}
|
||||
*/
|
||||
|
||||
fn serve_file(
|
||||
info: FileInfo,
|
||||
mut file: std::fs::File,
|
||||
old_etags: Option<String>,
|
||||
) -> http::Result<http::Response<Vec<u8>>> {
|
||||
let mut content = Vec::new();
|
||||
match old_etags {
|
||||
Some(old_etags) if old_etags != info.hash => warp::http::Response::builder()
|
||||
.header("content-type", info.file_type)
|
||||
.status(StatusCode::NOT_MODIFIED)
|
||||
.body(content),
|
||||
_ => {
|
||||
let _ = file.read_to_end(&mut content);
|
||||
warp::http::Response::builder()
|
||||
.header("content-type", info.file_type)
|
||||
.header("etag", info.hash)
|
||||
.status(StatusCode::OK)
|
||||
.body(content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
/*
|
||||
|
@ -297,33 +319,31 @@ pub async fn main() {
|
|||
.and(warp::header::optional::<String>("if-none-match"))
|
||||
.map({
|
||||
let app = app.clone();
|
||||
move |id: String, old_etags: Option<String>| {
|
||||
let mut content = Vec::new();
|
||||
match app.read().unwrap().get_thumbnail(&id) {
|
||||
Ok((info, mut stream)) => match old_etags {
|
||||
Some(old_etags) if old_etags != info.hash => {
|
||||
warp::http::Response::builder()
|
||||
.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),
|
||||
}
|
||||
move |id: String, old_etags: Option<String>| match app
|
||||
.read()
|
||||
.unwrap()
|
||||
.get_thumbnail(&id)
|
||||
{
|
||||
Ok((info, file)) => serve_file(info, file, old_etags),
|
||||
Err(_err) => warp::http::Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(vec![]),
|
||||
}
|
||||
});
|
||||
|
||||
let server = warp::serve(root.or(thumbnail));
|
||||
let file = warp::path!(String)
|
||||
.and(warp::header::optional::<String>("if-none-match"))
|
||||
.map({
|
||||
let app = app.clone();
|
||||
move |id: String, old_etags: Option<String>| match app.read().unwrap().get_file(&id) {
|
||||
Ok((info, file)) => serve_file(info, file, old_etags),
|
||||
Err(_err) => warp::http::Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(vec![]),
|
||||
}
|
||||
});
|
||||
|
||||
let server = warp::serve(root.or(file).or(thumbnail));
|
||||
server
|
||||
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
|
||||
.await;
|
||||
|
|
Loading…
Reference in New Issue