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: [torrust#157] extract service: proxy
Decoupling services from actix-web framework.
- Loading branch information
1 parent
d58f3cc
commit c9fb249
Showing
8 changed files
with
147 additions
and
67 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
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
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,3 +1,4 @@ | ||
pub mod about; | ||
pub mod category; | ||
pub mod proxy; | ||
pub mod user; |
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,46 @@ | ||
//! Image cache proxy. | ||
//! | ||
//! The image cache proxy is a service that allows users to proxy images | ||
//! through the server. | ||
//! | ||
//! Sample URL: | ||
//! | ||
//! <http://0.0.0.0:3000/v1/proxy/image/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F2%2F21%2FMandel_zoom_00_mandelbrot_set.jpg%2F1280px-Mandel_zoom_00_mandelbrot_set.jpg> | ||
use std::sync::Arc; | ||
|
||
use bytes::Bytes; | ||
|
||
use super::user::DbUserRepository; | ||
use crate::cache::image::manager::{Error, ImageCacheService}; | ||
use crate::models::user::UserId; | ||
|
||
pub struct Service { | ||
image_cache_service: Arc<ImageCacheService>, | ||
user_repository: Arc<DbUserRepository>, | ||
} | ||
|
||
impl Service { | ||
#[must_use] | ||
pub fn new(image_cache_service: Arc<ImageCacheService>, user_repository: Arc<DbUserRepository>) -> Self { | ||
Self { | ||
image_cache_service, | ||
user_repository, | ||
} | ||
} | ||
|
||
/// It gets image by URL and caches it. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if: | ||
/// | ||
/// * The image URL is unreachable. | ||
/// * The image URL is not an image. | ||
/// * The image is too big. | ||
/// * The user quota is met. | ||
pub async fn get_image_by_url(&self, url: &str, user_id: &UserId) -> Result<Bytes, Error> { | ||
let user = self.user_repository.get_compact_user(user_id).await.ok(); | ||
|
||
self.image_cache_service.get_image_by_url(url, user).await | ||
} | ||
} |
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,2 @@ | ||
//! User interface module. Presentation layer. | ||
pub mod proxy; |
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,56 @@ | ||
use std::sync::Once; | ||
|
||
use bytes::Bytes; | ||
use text_to_png::TextRenderer; | ||
|
||
use crate::cache::image::manager::Error; | ||
|
||
pub static ERROR_IMAGE_LOADER: Once = Once::new(); | ||
|
||
pub static mut ERROR_IMAGE_URL_IS_UNREACHABLE: Bytes = Bytes::new(); | ||
pub static mut ERROR_IMAGE_URL_IS_NOT_AN_IMAGE: Bytes = Bytes::new(); | ||
pub static mut ERROR_IMAGE_TOO_BIG: Bytes = Bytes::new(); | ||
pub static mut ERROR_IMAGE_USER_QUOTA_MET: Bytes = Bytes::new(); | ||
pub static mut ERROR_IMAGE_UNAUTHENTICATED: Bytes = Bytes::new(); | ||
|
||
const ERROR_IMG_FONT_SIZE: u8 = 16; | ||
const ERROR_IMG_COLOR: &str = "Red"; | ||
|
||
const ERROR_IMAGE_URL_IS_UNREACHABLE_TEXT: &str = "Could not find image."; | ||
const ERROR_IMAGE_URL_IS_NOT_AN_IMAGE_TEXT: &str = "Invalid image."; | ||
const ERROR_IMAGE_TOO_BIG_TEXT: &str = "Image is too big."; | ||
const ERROR_IMAGE_USER_QUOTA_MET_TEXT: &str = "Image proxy quota met."; | ||
const ERROR_IMAGE_UNAUTHENTICATED_TEXT: &str = "Sign in to see image."; | ||
|
||
pub fn load_error_images() { | ||
ERROR_IMAGE_LOADER.call_once(|| unsafe { | ||
ERROR_IMAGE_URL_IS_UNREACHABLE = generate_img_from_text(ERROR_IMAGE_URL_IS_UNREACHABLE_TEXT); | ||
ERROR_IMAGE_URL_IS_NOT_AN_IMAGE = generate_img_from_text(ERROR_IMAGE_URL_IS_NOT_AN_IMAGE_TEXT); | ||
ERROR_IMAGE_TOO_BIG = generate_img_from_text(ERROR_IMAGE_TOO_BIG_TEXT); | ||
ERROR_IMAGE_USER_QUOTA_MET = generate_img_from_text(ERROR_IMAGE_USER_QUOTA_MET_TEXT); | ||
ERROR_IMAGE_UNAUTHENTICATED = generate_img_from_text(ERROR_IMAGE_UNAUTHENTICATED_TEXT); | ||
}); | ||
} | ||
|
||
pub fn map_error_to_image(error: &Error) -> Bytes { | ||
unsafe { | ||
match error { | ||
Error::UrlIsUnreachable => ERROR_IMAGE_URL_IS_UNREACHABLE.clone(), | ||
Error::UrlIsNotAnImage => ERROR_IMAGE_URL_IS_NOT_AN_IMAGE.clone(), | ||
Error::ImageTooBig => ERROR_IMAGE_TOO_BIG.clone(), | ||
Error::UserQuotaMet => ERROR_IMAGE_USER_QUOTA_MET.clone(), | ||
Error::Unauthenticated => ERROR_IMAGE_UNAUTHENTICATED.clone(), | ||
} | ||
} | ||
} | ||
|
||
fn generate_img_from_text(text: &str) -> Bytes { | ||
let renderer = TextRenderer::default(); | ||
|
||
Bytes::from( | ||
renderer | ||
.render_text_to_png_data(text, ERROR_IMG_FONT_SIZE, ERROR_IMG_COLOR) | ||
.unwrap() | ||
.data, | ||
) | ||
} |