use std::sync::Arc;
use bytes::Bytes;
use super::authorization::{self, ACTION};
use crate::cache::image::manager::{Error, ImageCacheService};
use crate::models::user::UserId;
pub struct Service {
image_cache_service: Arc<ImageCacheService>,
authorization_service: Arc<authorization::Service>,
}
impl Service {
#[must_use]
pub fn new(image_cache_service: Arc<ImageCacheService>, authorization_service: Arc<authorization::Service>) -> Self {
Self {
image_cache_service,
authorization_service,
}
}
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
let Some(user_id) = maybe_user_id else {
return Err(Error::Unauthenticated);
};
self.authorization_service
.authorize(ACTION::GetImageByUrl, maybe_user_id)
.await
.map_err(|_| Error::Unauthenticated)?;
self.image_cache_service.get_image_by_url(url, user_id).await
}
}