Skip to content

Commit 76f69df

Browse files
committed
fix: http range protocol support and streaming support.
1 parent 1423a35 commit 76f69df

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/server/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::convert::Infallible;
2020
use std::io::{Read, Write};
2121
use std::sync::Arc;
2222
use sysinfo::{Disks, System};
23+
use tower::ServiceExt;
2324
use tower_http::cors::CorsLayer;
2425
// Use a local definition of ServeDir if needed, but we'll use our embedded assets
2526
// use tower_http::services::ServeDir;
@@ -394,6 +395,7 @@ fn list_files_recursive(path: &str) -> anyhow::Result<serde_json::Value> {
394395
async fn serve_download(
395396
axum::extract::Path(path): axum::extract::Path<String>,
396397
State((state, _)): State<(Arc<AppState>, tokio::sync::broadcast::Sender<()>)>,
398+
req: Request,
397399
) -> impl IntoResponse {
398400
let config = state.engine.get_config().await;
399401
let full_path =
@@ -434,16 +436,15 @@ async fn serve_download(
434436
.into_response();
435437
}
436438

437-
match tokio::fs::read(&full_path).await {
438-
Ok(content) => {
439-
let mime = mime_guess::from_path(&full_path).first_or_octet_stream();
440-
Response::builder()
441-
.header(header::CONTENT_TYPE, mime.as_ref())
442-
.body(axum::body::Body::from(content))
443-
.unwrap()
444-
.into_response()
439+
match tower_http::services::ServeFile::new(&full_path)
440+
.oneshot(req)
441+
.await
442+
{
443+
Ok(res) => res.into_response(),
444+
Err(err) => {
445+
tracing::error!("Error serving file: {}", err);
446+
(StatusCode::INTERNAL_SERVER_ERROR, "Error serving file").into_response()
445447
}
446-
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Error reading file").into_response(),
447448
}
448449
}
449450

0 commit comments

Comments
 (0)