Import and update the file service application and orizentic #72
|
@ -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()?;
|
||||||
|
|
|
@ -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)
|
Err(_err) => warp::http::Response::builder()
|
||||||
.status(StatusCode::NOT_MODIFIED)
|
.status(StatusCode::NOT_FOUND)
|
||||||
.body(content)
|
.body(vec![]),
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue