feat: Add media thumbnail retrieval functionality and update MediaResponse structure

This commit is contained in:
2025-11-15 17:37:39 +01:00
parent 3f96de117b
commit 8a735c7c26
4 changed files with 72 additions and 26 deletions

View File

@@ -17,7 +17,7 @@ use crate::{
error::ApiError,
extractors::query_options::ApiListMediaOptions,
middleware::auth::{OptionalUserId, UserId},
schema::{MediaDetailsResponse, MediaMetadataResponse, MediaResponse},
schema::{MediaDetailsResponse, MediaResponse},
state::AppState,
};
@@ -26,6 +26,7 @@ pub fn media_routes(max_upload_size: usize) -> Router<AppState> {
.route("/", post(upload_media).get(list_user_media))
.route("/{id}", get(get_media_details).delete(delete_media))
.route("/{id}/file", get(get_media_file))
.route("/{id}/thumbnail", get(get_media_thumbnail))
.layer(DefaultBodyLimit::max(max_upload_size))
}
@@ -90,26 +91,37 @@ async fn get_media_file(
})
}
async fn get_media_thumbnail(
State(state): State<AppState>,
OptionalUserId(user_id): OptionalUserId,
Path(media_id): Path<Uuid>,
request: Request,
) -> Result<impl IntoResponse, ApiError> {
let thumbnail_path = state
.media_service
.get_media_thumbnail_path(media_id, user_id)
.await?;
let full_path = PathBuf::from(&thumbnail_path);
ServeFile::new(full_path)
.oneshot(request)
.await
.map_err(|e| {
ApiError::from(CoreError::Io(io::Error::new(
io::ErrorKind::NotFound,
format!("File not found: {}", e),
)))
})
}
async fn get_media_details(
State(state): State<AppState>,
OptionalUserId(user_id): OptionalUserId,
Path(id): Path<Uuid>,
) -> Result<Json<MediaDetailsResponse>, ApiError> {
let bundle = state.media_service.get_media_details(id, user_id).await?;
let response = MediaDetailsResponse {
id: bundle.media.id,
storage_path: bundle.media.storage_path,
original_filename: bundle.media.original_filename,
mime_type: bundle.media.mime_type,
hash: bundle.media.hash,
thumbnail_path: bundle.media.thumbnail_path,
metadata: bundle
.metadata
.into_iter()
.map(MediaMetadataResponse::from)
.collect(),
};
let response = MediaDetailsResponse::from(bundle);
Ok(Json(response))
}