forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): [torrust#180] Axum API, proxy context
- Loading branch information
1 parent
032b57f
commit 0af2cb7
Showing
8 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! API handlers for the the [`proxy`](crate::web::api::v1::contexts::proxy) API | ||
//! context. | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{Path, State}; | ||
use axum::response::Response; | ||
|
||
use super::responses::png_image; | ||
use crate::cache::image::manager::Error; | ||
use crate::common::AppData; | ||
use crate::ui::proxy::map_error_to_image; | ||
use crate::web::api::v1::extractors::bearer_token::Extract; | ||
|
||
/// Get the remote image. It uses the cached image if available. | ||
#[allow(clippy::unused_async)] | ||
pub async fn get_proxy_image_handler( | ||
State(app_data): State<Arc<AppData>>, | ||
Extract(maybe_bearer_token): Extract, | ||
Path(url): Path<String>, | ||
) -> Response { | ||
if maybe_bearer_token.is_none() { | ||
return png_image(map_error_to_image(&Error::Unauthenticated)); | ||
} | ||
|
||
let Ok(user_id) = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await else { return png_image(map_error_to_image(&Error::Unauthenticated)) }; | ||
|
||
// code-review: Handling status codes in the frontend other tan OK is quite a pain. | ||
// Return OK for now. | ||
|
||
// todo: it also work for other image types but we are always returning the | ||
// same content type: `image/png`. If we only support PNG images we should | ||
// change the documentation and return an error for other image types. | ||
|
||
// Get image URL from URL path parameter. | ||
let image_url = urlencoding::decode(&url).unwrap_or_default().into_owned(); | ||
|
||
match app_data.proxy_service.get_image_by_url(&image_url, &user_id).await { | ||
Ok(image_bytes) => { | ||
// Returns the cached image. | ||
png_image(image_bytes) | ||
} | ||
Err(e) => { | ||
// Returns an error image. | ||
png_image(map_error_to_image(&e)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use axum::response::{IntoResponse, Response}; | ||
use bytes::Bytes; | ||
use hyper::{header, StatusCode}; | ||
|
||
#[must_use] | ||
pub fn png_image(bytes: Bytes) -> Response { | ||
(StatusCode::OK, [(header::CONTENT_TYPE, "image/png")], bytes).into_response() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//! API routes for the [`proxy`](crate::web::api::v1::contexts::proxy) API context. | ||
//! | ||
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::proxy). | ||
use std::sync::Arc; | ||
|
||
use axum::routing::get; | ||
use axum::Router; | ||
|
||
use super::handlers::get_proxy_image_handler; | ||
use crate::common::AppData; | ||
|
||
/// Routes for the [`about`](crate::web::api::v1::contexts::about) API context. | ||
pub fn router(app_data: Arc<AppData>) -> Router { | ||
Router::new().route("/image/:url", get(get_proxy_image_handler).with_state(app_data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod about; | ||
pub mod category; | ||
pub mod proxy; | ||
pub mod root; | ||
pub mod settings; | ||
pub mod tag; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! API contract for `proxy` context. | ||
|
||
mod with_axum_implementation { | ||
|
||
// todo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod contract; |