Implement audio range requests

This commit enables serving audio files via HTTP Range requests. This is
crucial for allowing HTML5 audio players to seek to specific positions
within an audio file without re-downloading the entire file.

The changes include:
- Modifying `handle_audio` in `server/src/routes/web.rs` to parse
  `Range` headers.
- Implementing `serve_range` to handle partial content responses.
- Adding a `parse_range` helper function.
- Updating tests to verify range request functionality.
- Adding `ACCEPT_RANGES: bytes` header to indicate support for range
  requests.
- Storing recording duration in a sidecar file for faster UI rendering.
- Enhancing the HTML template to support a custom audio player with
  seeking.
This commit is contained in:
2026-04-19 22:26:04 +02:00
parent 2bcdb5436e
commit 17aa5d7200
9 changed files with 665 additions and 48 deletions
+5 -6
View File
@@ -90,10 +90,7 @@ async fn create_without_api_key_returns_401() {
let (state, _) = build_state();
let app = doctate_server::create_router_with_state(state);
let resp = app
.oneshot(create_request("{}", None))
.await
.unwrap();
let resp = app.oneshot(create_request("{}", None)).await.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
@@ -193,7 +190,10 @@ async fn consume_token_is_one_time_use() {
let second = app.oneshot(consume_request(&token)).await.unwrap();
// Second use: token gone, redirect to login.
assert_eq!(second.status(), StatusCode::SEE_OTHER);
assert_eq!(second.headers().get(header::LOCATION).unwrap(), "/web/login");
assert_eq!(
second.headers().get(header::LOCATION).unwrap(),
"/web/login"
);
}
#[tokio::test]
@@ -249,4 +249,3 @@ async fn consume_expired_token_redirects_to_login() {
// And the token must be removed from the store, even though it expired.
assert!(store.read().await.get(&token).is_none());
}
+148
View File
@@ -106,6 +106,154 @@ async fn web_audio_invalid_case_id_rejected() {
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn web_audio_serves_range_as_206() {
let config = test_config();
let data_path = config.data_path.clone();
let case_id = "770e8400-e29b-41d4-a716-446655440001";
let case_dir = data_path.join("dr_test").join(case_id);
std::fs::create_dir_all(&case_dir).unwrap();
let audio_bytes: Vec<u8> = (0u8..=200u8).collect();
let filename = "2026-04-13T10-30-00Z.m4a";
std::fs::write(case_dir.join(filename), &audio_bytes).unwrap();
let app = doctate_server::create_router(config);
let response = app
.oneshot(
Request::builder()
.uri(format!("/web/audio/dr_test/{case_id}/{filename}"))
.header("Range", "bytes=10-19")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
assert_eq!(
response
.headers()
.get("content-range")
.unwrap()
.to_str()
.unwrap(),
format!("bytes 10-19/{}", audio_bytes.len())
);
assert_eq!(
response
.headers()
.get("content-length")
.unwrap()
.to_str()
.unwrap(),
"10"
);
assert_eq!(
response
.headers()
.get("accept-ranges")
.unwrap()
.to_str()
.unwrap(),
"bytes"
);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(&bytes[..], &audio_bytes[10..20]);
let _ = std::fs::remove_dir_all(&data_path);
}
#[tokio::test]
async fn web_audio_invalid_range_returns_416() {
let config = test_config();
let data_path = config.data_path.clone();
let case_id = "770e8400-e29b-41d4-a716-446655440002";
let case_dir = data_path.join("dr_test").join(case_id);
std::fs::create_dir_all(&case_dir).unwrap();
let audio_bytes = b"short";
let filename = "2026-04-13T10-30-00Z.m4a";
std::fs::write(case_dir.join(filename), audio_bytes).unwrap();
let app = doctate_server::create_router(config);
let response = app
.oneshot(
Request::builder()
.uri(format!("/web/audio/dr_test/{case_id}/{filename}"))
.header("Range", "bytes=1000-2000")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
assert_eq!(
response
.headers()
.get("content-range")
.unwrap()
.to_str()
.unwrap(),
format!("bytes */{}", audio_bytes.len())
);
let _ = std::fs::remove_dir_all(&data_path);
}
#[tokio::test]
async fn web_audio_no_range_header_advertises_accept_ranges() {
let config = test_config();
let data_path = config.data_path.clone();
let case_id = "770e8400-e29b-41d4-a716-446655440003";
let case_dir = data_path.join("dr_test").join(case_id);
std::fs::create_dir_all(&case_dir).unwrap();
let audio_bytes = b"full file content";
let filename = "2026-04-13T10-30-00Z.m4a";
std::fs::write(case_dir.join(filename), audio_bytes).unwrap();
let app = doctate_server::create_router(config);
let response = app
.oneshot(
Request::builder()
.uri(format!("/web/audio/dr_test/{case_id}/{filename}"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response
.headers()
.get("accept-ranges")
.unwrap()
.to_str()
.unwrap(),
"bytes"
);
assert_eq!(
response
.headers()
.get("content-length")
.unwrap()
.to_str()
.unwrap(),
audio_bytes.len().to_string()
);
let _ = std::fs::remove_dir_all(&data_path);
}
#[tokio::test]
async fn web_audio_nonexistent_file_returns_404() {
let config = test_config();