feat: assemble completed uploads
Implement POST /api/uploads/{id}/complete. The storage layer now reloads upload
metadata, verifies that every expected chunk exists with the exact expected
length, concatenates chunks in order into a temporary final file, flushes it,
and renames it into data/complete only after assembly succeeds.
The endpoint preserves staging data after completion, rejects incomplete uploads
with a conflict response, and refuses to overwrite an existing completed file.
This keeps failed or duplicate completion attempts explicit rather than silently
clobbering local files.
Extend the model, router, documentation, and test checklist for completion
responses and add integration coverage for successful assembly, incomplete
uploads, staging preservation, and duplicate completion conflicts.
Test Plan:
- just check
Refs: PLAN.md milestone 8
This commit is contained in:
+16
-1
@@ -10,7 +10,7 @@ use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
app::AppState,
|
||||
model::{CreateUploadRequest, CreateUploadResponse},
|
||||
model::{CompleteUploadResponse, CreateUploadRequest, CreateUploadResponse},
|
||||
storage::StorageError,
|
||||
};
|
||||
|
||||
@@ -56,6 +56,19 @@ pub async fn put_chunk(
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
/// Assembles uploaded chunks into the final completed file.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an API error when the upload is unknown, incomplete, invalid, or
|
||||
/// cannot be assembled on disk.
|
||||
pub async fn complete_upload(
|
||||
State(state): State<AppState>,
|
||||
Path(upload_id): Path<String>,
|
||||
) -> Result<Json<CompleteUploadResponse>, ApiError> {
|
||||
Ok(Json(state.storage.complete_upload(&upload_id).await?))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ApiError {
|
||||
status: StatusCode,
|
||||
@@ -78,6 +91,8 @@ impl From<StorageError> for ApiError {
|
||||
fn from(error: StorageError) -> Self {
|
||||
let status = if error.is_not_found() {
|
||||
StatusCode::NOT_FOUND
|
||||
} else if error.is_conflict() {
|
||||
StatusCode::CONFLICT
|
||||
} else if error.is_invalid_input() {
|
||||
StatusCode::BAD_REQUEST
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user