1#![warn(missing_docs)]
10pub mod providers;
11
12use serde::{Deserialize, Serialize};
13pub use wae_types::{WaeError, WaeResult};
14
15use url::Url;
16
17pub type StorageResult<T> = WaeResult<T>;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub enum StorageProviderType {
23 Cos,
25 Gcs,
27 Oss,
29 S3,
31 Local,
33 AzureBlob,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct StorageConfig {
40 pub provider: StorageProviderType,
42 pub secret_id: String,
44 pub secret_key: String,
46 pub bucket: String,
48 pub region: String,
50 pub endpoint: Option<String>,
52 pub cdn_url: Option<String>,
54}
55
56pub trait StorageProvider: Send + Sync {
60 fn get_presigned_put_url(&self, key: &str, config: &StorageConfig) -> StorageResult<Url>;
62
63 fn sign_url(&self, path: &str, config: &StorageConfig) -> StorageResult<Url>;
65}
66
67pub use providers::{
68 azure_blob::AzureBlobProvider, cos::CosProvider, gcs::GcsProvider, local::LocalStorageProvider, oss::OssProvider,
69 s3::S3Provider,
70};
71
72pub struct StorageService;
76
77impl StorageService {
78 pub fn get_provider(provider_type: &StorageProviderType) -> Box<dyn StorageProvider> {
80 match provider_type {
81 StorageProviderType::Cos => Box::new(CosProvider),
82 StorageProviderType::Gcs => Box::new(GcsProvider),
83 StorageProviderType::Oss => Box::new(OssProvider),
84 StorageProviderType::S3 => Box::new(S3Provider),
85 StorageProviderType::Local => Box::new(LocalStorageProvider),
86 StorageProviderType::AzureBlob => Box::new(AzureBlobProvider),
87 }
88 }
89
90 pub fn sign_url(path: &str, config: &StorageConfig) -> StorageResult<Url> {
92 let provider = Self::get_provider(&config.provider);
93 provider.sign_url(path, config)
94 }
95
96 pub fn get_presigned_put_url(key: &str, config: &StorageConfig) -> StorageResult<Url> {
98 let provider = Self::get_provider(&config.provider);
99 provider.get_presigned_put_url(key, config)
100 }
101}