Refactor file and thumbnail serving to common code

This commit is contained in:
Savanni D'Gerinel 2023-09-21 22:15:58 -04:00
parent a06c9fae25
commit 4a7b23544e
2 changed files with 45 additions and 25 deletions

View File

@ -43,7 +43,7 @@ impl App {
FileInfo::open(&id, &self.files_root) 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 f = File::open(&id, &self.files_root)?;
let info = f.info(); let info = f.info();
let stream = f.stream()?; let stream = f.stream()?;

View File

@ -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] #[tokio::main]
pub async fn main() { pub async fn main() {
/* /*
@ -297,33 +319,31 @@ pub async fn main() {
.and(warp::header::optional::<String>("if-none-match")) .and(warp::header::optional::<String>("if-none-match"))
.map({ .map({
let app = app.clone(); let app = app.clone();
move |id: String, old_etags: Option<String>| { move |id: String, old_etags: Option<String>| match app
let mut content = Vec::new(); .read()
match app.read().unwrap().get_thumbnail(&id) { .unwrap()
Ok((info, mut stream)) => match old_etags { .get_thumbnail(&id)
Some(old_etags) if old_etags != info.hash => { {
warp::http::Response::builder() Ok((info, file)) => serve_file(info, file, old_etags),
.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() Err(_err) => warp::http::Response::builder()
.status(StatusCode::NOT_FOUND) .status(StatusCode::NOT_FOUND)
.body(content), .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 server
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002)) .run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
.await; .await;