entertainarr-adapter-http 0.1.1

HTTP adapter for entertainarr
Documentation
use axum::extract::{Path, State};
use entertainarr_domain::media::entity::VideoStreamResponse;
use entertainarr_domain::media::prelude::MediaService;

use crate::server::handler::error::ApiErrorResponse;

pub enum BodyResponse {
    Redirect(String),
}

impl From<VideoStreamResponse> for BodyResponse {
    fn from(value: VideoStreamResponse) -> Self {
        match value {
            VideoStreamResponse::Redirect(url) => BodyResponse::Redirect(url),
        }
    }
}

impl axum::response::IntoResponse for BodyResponse {
    fn into_response(self) -> axum::response::Response {
        match self {
            Self::Redirect(url) => {
                axum::response::Redirect::permanent(url.as_str()).into_response()
            }
        }
    }
}

pub async fn handle<S>(
    State(state): State<S>,
    Path(media_id): Path<u64>,
) -> Result<BodyResponse, ApiErrorResponse>
where
    S: crate::server::prelude::ServerState,
{
    let response = state
        .media_service()
        .video_stream(media_id)
        .await
        .map_err(|err| {
            tracing::error!(error = ?err, "unable to fetch media");
            ApiErrorResponse::internal()
        })?;
    let response = response.ok_or_else(|| ApiErrorResponse::not_found("media file"))?;

    Ok(response.into())
}