feat: add oss direct upload adapter
This commit is contained in:
@@ -3,6 +3,7 @@ use std::{error::Error, fmt};
|
||||
use platform_auth::{
|
||||
JwtConfig, JwtError, RefreshCookieConfig, RefreshCookieError, RefreshCookieSameSite,
|
||||
};
|
||||
use platform_oss::{OssClient, OssConfig, OssError};
|
||||
|
||||
use crate::config::AppConfig;
|
||||
|
||||
@@ -14,12 +15,14 @@ pub struct AppState {
|
||||
pub config: AppConfig,
|
||||
auth_jwt_config: JwtConfig,
|
||||
refresh_cookie_config: RefreshCookieConfig,
|
||||
oss_client: Option<OssClient>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AppStateInitError {
|
||||
Jwt(JwtError),
|
||||
RefreshCookie(RefreshCookieError),
|
||||
Oss(OssError),
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -40,11 +43,13 @@ impl AppState {
|
||||
refresh_cookie_same_site,
|
||||
config.refresh_session_ttl_days,
|
||||
)?;
|
||||
let oss_client = build_oss_client(&config)?;
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
auth_jwt_config,
|
||||
refresh_cookie_config,
|
||||
oss_client,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -55,6 +60,10 @@ impl AppState {
|
||||
pub fn refresh_cookie_config(&self) -> &RefreshCookieConfig {
|
||||
&self.refresh_cookie_config
|
||||
}
|
||||
|
||||
pub fn oss_client(&self) -> Option<&OssClient> {
|
||||
self.oss_client.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AppStateInitError {
|
||||
@@ -62,6 +71,7 @@ impl fmt::Display for AppStateInitError {
|
||||
match self {
|
||||
Self::Jwt(error) => write!(f, "{error}"),
|
||||
Self::RefreshCookie(error) => write!(f, "{error}"),
|
||||
Self::Oss(error) => write!(f, "{error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,3 +89,34 @@ impl From<RefreshCookieError> for AppStateInitError {
|
||||
Self::RefreshCookie(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OssError> for AppStateInitError {
|
||||
fn from(value: OssError) -> Self {
|
||||
Self::Oss(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_oss_client(config: &AppConfig) -> Result<Option<OssClient>, AppStateInitError> {
|
||||
let has_any_oss_field = config.oss_bucket.is_some()
|
||||
|| config.oss_endpoint.is_some()
|
||||
|| config.oss_access_key_id.is_some()
|
||||
|| config.oss_access_key_secret.is_some()
|
||||
|| config.oss_public_base_url.is_some();
|
||||
|
||||
if !has_any_oss_field {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let oss_config = OssConfig::new(
|
||||
config.oss_bucket.clone().unwrap_or_default(),
|
||||
config.oss_endpoint.clone().unwrap_or_default(),
|
||||
config.oss_access_key_id.clone().unwrap_or_default(),
|
||||
config.oss_access_key_secret.clone().unwrap_or_default(),
|
||||
config.oss_public_base_url.clone(),
|
||||
config.oss_post_expire_seconds,
|
||||
config.oss_post_max_size_bytes,
|
||||
config.oss_success_action_status,
|
||||
)?;
|
||||
|
||||
Ok(Some(OssClient::new(oss_config)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user