feat: add file serving endpoint GET /assets/:id/file
This commit is contained in:
@@ -9,8 +9,10 @@ use application::{
|
||||
};
|
||||
use axum::{
|
||||
Json,
|
||||
body::Body,
|
||||
extract::{Multipart, Path, Query, State},
|
||||
http::StatusCode,
|
||||
http::{StatusCode, header},
|
||||
response::Response,
|
||||
};
|
||||
use domain::value_objects::{MetadataValue, StructuredData, SystemId};
|
||||
use sha2::{Digest, Sha256};
|
||||
@@ -171,3 +173,39 @@ pub async fn update_metadata(
|
||||
state.update_metadata_handler.execute(cmd).await?;
|
||||
Ok(Json(serde_json::json!({ "status": "updated" })))
|
||||
}
|
||||
|
||||
pub async fn serve_file(
|
||||
State(state): State<AppState>,
|
||||
_claims: JwtClaims,
|
||||
Path((asset_id,)): Path<(uuid::Uuid,)>,
|
||||
) -> Result<Response, AppError> {
|
||||
let asset = state
|
||||
.asset_repo
|
||||
.find_by_id(&SystemId::from_uuid(asset_id))
|
||||
.await?
|
||||
.ok_or_else(|| domain::errors::DomainError::NotFound("Asset not found".into()))?;
|
||||
|
||||
let data = state
|
||||
.file_storage
|
||||
.read_file(&asset.source_reference.relative_path)
|
||||
.await?;
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, &asset.mime_type)
|
||||
.header(header::CONTENT_LENGTH, data.len())
|
||||
.header(
|
||||
header::CONTENT_DISPOSITION,
|
||||
format!(
|
||||
"inline; filename=\"{}\"",
|
||||
asset
|
||||
.source_reference
|
||||
.relative_path
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or("file")
|
||||
),
|
||||
)
|
||||
.body(Body::from(data))
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user