2022-12-28 22:05:28 -06:00
|
|
|
use std::{
|
|
|
|
path::{Component, PathBuf},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use axum::{
|
|
|
|
body::StreamBody,
|
|
|
|
extract::{Path, State},
|
2023-01-08 18:08:24 -06:00
|
|
|
http::HeaderValue,
|
|
|
|
response::{IntoResponse, Response},
|
2022-12-28 22:05:28 -06:00
|
|
|
};
|
2023-01-08 18:08:24 -06:00
|
|
|
|
2023-01-28 19:17:35 -06:00
|
|
|
use bytes::Bytes;
|
2022-12-28 22:05:28 -06:00
|
|
|
use hyper::StatusCode;
|
2023-01-08 18:08:24 -06:00
|
|
|
use mime_guess::mime;
|
2023-01-08 19:10:30 -06:00
|
|
|
use tokio::fs::File;
|
2022-12-28 22:05:28 -06:00
|
|
|
use tokio_util::io::ReaderStream;
|
|
|
|
|
2023-01-28 19:17:35 -06:00
|
|
|
pub enum ViewResponse {
|
|
|
|
FromDisk(File),
|
|
|
|
FromCache(PathBuf, Bytes),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for ViewResponse {
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
match self {
|
|
|
|
ViewResponse::FromDisk(file) => {
|
|
|
|
let reader = ReaderStream::new(file);
|
|
|
|
let stream = StreamBody::new(reader);
|
|
|
|
|
|
|
|
stream.into_response()
|
|
|
|
}
|
|
|
|
ViewResponse::FromCache(original_path, data) => {
|
|
|
|
// guess the content-type using the original path
|
|
|
|
// (axum handles this w/ streamed file responses but caches are octet-stream by default)
|
|
|
|
let content_type = mime_guess::from_path(original_path)
|
|
|
|
.first()
|
|
|
|
.unwrap_or(mime::APPLICATION_OCTET_STREAM)
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
// extract mutable headers from the response
|
|
|
|
let mut res = data.into_response();
|
|
|
|
let headers = res.headers_mut();
|
|
|
|
|
|
|
|
// clear the headers and add our content-type
|
|
|
|
headers.clear();
|
|
|
|
headers.insert(
|
|
|
|
"content-type",
|
|
|
|
HeaderValue::from_str(content_type.as_str()).unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 23:03:14 -06:00
|
|
|
#[axum::debug_handler]
|
2022-12-28 22:05:28 -06:00
|
|
|
pub async fn view(
|
2023-01-28 19:17:35 -06:00
|
|
|
State(engine): State<Arc<crate::engine::Engine>>,
|
2022-12-28 22:05:28 -06:00
|
|
|
Path(original_path): Path<PathBuf>,
|
2023-01-28 19:17:35 -06:00
|
|
|
) -> Result<ViewResponse, StatusCode> {
|
2022-12-28 22:05:28 -06:00
|
|
|
// (hopefully) prevent path traversal, just check for any non-file components
|
|
|
|
if original_path
|
|
|
|
.components()
|
|
|
|
.into_iter()
|
|
|
|
.any(|x| !matches!(x, Component::Normal(_)))
|
|
|
|
{
|
2023-01-08 18:08:24 -06:00
|
|
|
error!(target: "view", "a request attempted path traversal");
|
2023-01-28 19:17:35 -06:00
|
|
|
return Err(StatusCode::NOT_FOUND);
|
2022-12-28 22:05:28 -06:00
|
|
|
}
|
2023-01-28 19:17:35 -06:00
|
|
|
|
|
|
|
engine.get_upload(&original_path).await
|
2023-01-08 19:10:30 -06:00
|
|
|
}
|