u/server/src/routes/i.rs
2023-03-04 19:29:56 -05:00

29 lines
738 B
Rust

use crate::state::ReqState;
use axum::{
body::StreamBody,
extract::{Path, State},
response::IntoResponse,
};
use hyper::StatusCode;
use tokio_util::io::ReaderStream;
pub async fn get(State(state): ReqState, Path(id): Path<String>) -> impl IntoResponse {
if id.len() < 8 {
return Err(StatusCode::NOT_FOUND);
}
let id = id[..8].to_string();
let path = state.files_dir.join(id.clone());
if path.exists() {
let file = tokio::fs::File::open(path)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let stream = ReaderStream::new(file);
let body = StreamBody::new(stream);
Ok(body)
} else {
Err(StatusCode::NOT_FOUND)
}
}