feat: add media file retrieval functionality and update dependencies
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{DefaultBodyLimit, Multipart, State},
|
||||
extract::{DefaultBodyLimit, Multipart, Path, Request, State},
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
routing::post,
|
||||
response::{IntoResponse, Json},
|
||||
routing::{get, post},
|
||||
};
|
||||
use futures::TryStreamExt;
|
||||
use libertas_core::{error::CoreError, models::Media, schema::UploadMediaData};
|
||||
use serde::Serialize;
|
||||
use std::io;
|
||||
use std::{io, path::PathBuf};
|
||||
|
||||
use tower::ServiceExt;
|
||||
use tower_http::services::ServeFile;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{error::ApiError, middleware::auth::UserId, state::AppState};
|
||||
|
||||
@@ -36,6 +40,7 @@ impl From<Media> for MediaResponse {
|
||||
pub fn media_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", post(upload_media))
|
||||
.route("/{media_id}/file", get(get_media_file))
|
||||
.layer(DefaultBodyLimit::max(250 * 1024 * 1024))
|
||||
}
|
||||
|
||||
@@ -75,3 +80,27 @@ async fn upload_media(
|
||||
|
||||
Ok((StatusCode::CREATED, Json(media.into())))
|
||||
}
|
||||
|
||||
async fn get_media_file(
|
||||
State(state): State<AppState>,
|
||||
UserId(user_id): UserId,
|
||||
Path(media_id): Path<Uuid>,
|
||||
request: Request,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let storage_path = state
|
||||
.media_service
|
||||
.get_media_filepath(media_id, user_id)
|
||||
.await?;
|
||||
|
||||
let full_path = PathBuf::from(&state.config.media_library_path).join(&storage_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),
|
||||
)))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user