feat: enhance media management with EXIF data extraction, metadata filtering, and storage path generation
refactor: update configuration handling to use environment variables and improve code organization
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::error::{CoreError, CoreResult};
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum DatabaseType {
|
||||
Postgres,
|
||||
Sqlite,
|
||||
@@ -30,6 +33,48 @@ pub struct ThumbnailConfig {
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct Config {
|
||||
#[serde(rename = "DATABASE_URL")]
|
||||
pub database_url: String,
|
||||
|
||||
#[serde(rename = "DATABASE_DB_TYPE")]
|
||||
pub database_db_type: DatabaseType,
|
||||
|
||||
#[serde(rename = "SERVER_ADDRESS")]
|
||||
pub server_address: String,
|
||||
|
||||
#[serde(rename = "JWT_SECRET")]
|
||||
pub jwt_secret: String,
|
||||
|
||||
#[serde(rename = "MEDIA_LIBRARY_PATH")]
|
||||
pub media_library_path: String,
|
||||
|
||||
#[serde(rename = "BROKER_URL")]
|
||||
pub broker_url: String,
|
||||
|
||||
#[serde(default = "default_max_upload_size")]
|
||||
#[serde(rename = "MAX_UPLOAD_SIZE_MB")]
|
||||
pub max_upload_size_mb: u32,
|
||||
|
||||
#[serde(default = "default_storage_quota")]
|
||||
#[serde(rename = "DEFAULT_STORAGE_QUOTA_GB")]
|
||||
pub default_storage_quota_gb: u64,
|
||||
|
||||
#[serde(default = "default_allowed_sort_columns")]
|
||||
#[serde(rename = "ALLOWED_SORT_COLUMNS")]
|
||||
pub allowed_sort_columns: Vec<String>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub thumbnail_config: Option<ThumbnailConfig>,
|
||||
}
|
||||
|
||||
fn default_max_upload_size() -> u32 { 100 }
|
||||
fn default_storage_quota() -> u64 { 10 }
|
||||
fn default_allowed_sort_columns() -> Vec<String> {
|
||||
vec!["created_at".to_string(), "original_filename".to_string()]
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppConfig {
|
||||
pub database: DatabaseConfig,
|
||||
pub server_address: String,
|
||||
pub jwt_secret: String,
|
||||
@@ -40,3 +85,29 @@ pub struct Config {
|
||||
pub allowed_sort_columns: Option<Vec<String>>,
|
||||
pub thumbnail_config: Option<ThumbnailConfig>,
|
||||
}
|
||||
|
||||
pub fn load_config() -> CoreResult<AppConfig> {
|
||||
// Load the .env file at that specific path
|
||||
let env_path = dotenvy::dotenv()
|
||||
.map_err(|e| CoreError::Config(format!("Failed to load .env file: {}", e)))?;
|
||||
|
||||
println!("Loaded config from {}", env_path.display());
|
||||
|
||||
let config = envy::from_env::<Config>()
|
||||
.map_err(|e| CoreError::Config(format!("Failed to load config from env: {}", e)))?;
|
||||
|
||||
Ok(AppConfig {
|
||||
database: DatabaseConfig {
|
||||
db_type: config.database_db_type,
|
||||
url: config.database_url,
|
||||
},
|
||||
server_address: config.server_address,
|
||||
jwt_secret: config.jwt_secret,
|
||||
media_library_path: config.media_library_path,
|
||||
broker_url: config.broker_url,
|
||||
max_upload_size_mb: Some(config.max_upload_size_mb),
|
||||
default_storage_quota_gb: Some(config.default_storage_quota_gb),
|
||||
allowed_sort_columns: Some(config.allowed_sort_columns),
|
||||
thumbnail_config: config.thumbnail_config,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user