1594c65d89
Add the chunk upload and progress APIs from PLAN.md. PUT
/api/uploads/{id}/chunks/{index} now accepts raw octet-stream bodies, validates
unknown upload ids, out-of-range chunk indexes, and exact chunk lengths, then
writes through a temporary .part.tmp path before renaming the completed chunk
into place. Re-uploading an already-complete chunk is idempotent when the
existing file length matches the expected length.
GET /api/uploads/{id} now reports server-authoritative progress by scanning the
chunk directory and only counting chunk files whose lengths match metadata. The
router also raises Axum's request body limit to 64 MiB so the planned 16 MiB
chunks can reach the handler, matching the nginx deployment guidance.
Document the chunk storage responsibility and extend the reusable test checklist
with the new progress and validation coverage.
Test Plan:
- just check
Refs: PLAN.md milestones 3 and 4
98 lines
2.4 KiB
Rust
98 lines
2.4 KiB
Rust
use axum::{
|
|
Json,
|
|
body::Bytes,
|
|
extract::Path,
|
|
extract::State,
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde::Serialize;
|
|
|
|
use crate::{
|
|
app::AppState,
|
|
model::{CreateUploadRequest, CreateUploadResponse},
|
|
storage::StorageError,
|
|
};
|
|
|
|
/// Creates an upload record and persists its metadata before returning.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an API error when request validation fails or metadata cannot be
|
|
/// written to storage.
|
|
pub async fn create_upload(
|
|
State(state): State<AppState>,
|
|
Json(request): Json<CreateUploadRequest>,
|
|
) -> Result<Json<CreateUploadResponse>, ApiError> {
|
|
let meta = state.storage.create_upload(request).await?;
|
|
Ok(Json(meta.create_response()))
|
|
}
|
|
|
|
/// Returns server-authoritative upload progress.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an API error when the upload id is invalid, unknown, or cannot be
|
|
/// read from storage.
|
|
pub async fn get_upload(
|
|
State(state): State<AppState>,
|
|
Path(upload_id): Path<String>,
|
|
) -> Result<Json<crate::model::UploadProgressResponse>, ApiError> {
|
|
Ok(Json(state.storage.progress(&upload_id).await?))
|
|
}
|
|
|
|
/// Stores one raw binary chunk body.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an API error when the upload id is invalid or unknown, the chunk
|
|
/// index is out of range, the body length is wrong, or the write fails.
|
|
pub async fn put_chunk(
|
|
State(state): State<AppState>,
|
|
Path((upload_id, index)): Path<(String, u64)>,
|
|
body: Bytes,
|
|
) -> Result<StatusCode, ApiError> {
|
|
state.storage.store_chunk(&upload_id, index, &body).await?;
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ApiError {
|
|
status: StatusCode,
|
|
message: String,
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
(
|
|
self.status,
|
|
Json(ErrorResponse {
|
|
error: self.message,
|
|
}),
|
|
)
|
|
.into_response()
|
|
}
|
|
}
|
|
|
|
impl From<StorageError> for ApiError {
|
|
fn from(error: StorageError) -> Self {
|
|
let status = if error.is_not_found() {
|
|
StatusCode::NOT_FOUND
|
|
} else if error.is_invalid_input() {
|
|
StatusCode::BAD_REQUEST
|
|
} else {
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
};
|
|
|
|
Self {
|
|
status,
|
|
message: error.to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct ErrorResponse {
|
|
error: String,
|
|
}
|