1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Configuration for the storage system.
use crate::facade::DiskConfig;
#[cfg(feature = "s3")]
use crate::facade::DiskDriver;
use std::collections::HashMap;
use std::env;
/// Configuration for the storage system.
#[derive(Debug, Clone)]
pub struct StorageConfig {
/// Default disk name.
pub default: String,
/// Disk configurations.
pub disks: HashMap<String, DiskConfig>,
}
impl Default for StorageConfig {
fn default() -> Self {
let mut disks = HashMap::new();
disks.insert("local".to_string(), DiskConfig::local("./storage"));
Self {
default: "local".to_string(),
disks,
}
}
}
impl StorageConfig {
/// Create a new storage config with a default disk.
pub fn new(default: impl Into<String>) -> Self {
Self {
default: default.into(),
disks: HashMap::new(),
}
}
/// Create configuration from environment variables.
///
/// Reads the following environment variables:
/// - `FILESYSTEM_DISK`: Default disk name (default: "local")
/// - `FILESYSTEM_LOCAL_ROOT`: Root path for local disk (default: "./storage")
/// - `FILESYSTEM_LOCAL_URL`: Public URL for local files
/// - `FILESYSTEM_PUBLIC_ROOT`: Root path for public disk (default: "./storage/public")
/// - `FILESYSTEM_PUBLIC_URL`: Public URL for public files (default: "/storage")
///
/// With `s3` feature:
/// - `AWS_ACCESS_KEY_ID`: S3 access key
/// - `AWS_SECRET_ACCESS_KEY`: S3 secret key
/// - `AWS_DEFAULT_REGION`: S3 region (default: "us-east-1")
/// - `AWS_BUCKET`: S3 bucket name
/// - `AWS_PUBLIC_URL`: Public base URL for generated file URLs (overrides `AWS_URL` for this purpose)
/// - `AWS_URL`: S3 API endpoint; also used as public URL base if `AWS_PUBLIC_URL` is not set
///
/// # Example
///
/// ```rust,ignore
/// use ferro_storage::{StorageConfig, Storage};
///
/// let config = StorageConfig::from_env();
/// let storage = Storage::with_storage_config(config);
/// ```
pub fn from_env() -> Self {
let default = env::var("FILESYSTEM_DISK").unwrap_or_else(|_| "local".to_string());
let mut disks = HashMap::new();
// Local disk
let local_root =
env::var("FILESYSTEM_LOCAL_ROOT").unwrap_or_else(|_| "./storage".to_string());
let mut local_config = DiskConfig::local(&local_root);
if let Ok(url) = env::var("FILESYSTEM_LOCAL_URL") {
local_config = local_config.with_url(url);
}
disks.insert("local".to_string(), local_config);
// Public disk (for publicly accessible files)
let public_root =
env::var("FILESYSTEM_PUBLIC_ROOT").unwrap_or_else(|_| "./storage/public".to_string());
let public_url =
env::var("FILESYSTEM_PUBLIC_URL").unwrap_or_else(|_| "/storage".to_string());
let public_config = DiskConfig::local(&public_root).with_url(public_url);
disks.insert("public".to_string(), public_config);
// S3 disk (if configured)
#[cfg(feature = "s3")]
if let Ok(bucket) = env::var("AWS_BUCKET") {
let region = env::var("AWS_DEFAULT_REGION").unwrap_or_else(|_| "us-east-1".to_string());
let mut s3_config = DiskConfig {
driver: DiskDriver::S3,
root: None,
url: None,
bucket: Some(bucket.clone()),
region: Some(region),
};
// Resolve public file URL base (used by Storage::url() to build asset URLs).
// Priority: AWS_PUBLIC_URL → computed from AWS_URL+bucket → AWS_URL bare.
// Auto-compute handles providers like DigitalOcean Spaces and Cloudflare R2
// where the public URL is {bucket}.{endpoint_host} but the API endpoint is
// just {endpoint_host}.
let public_url = if let Ok(explicit) = env::var("AWS_PUBLIC_URL") {
Some(explicit)
} else if let Ok(api_url) = env::var("AWS_URL") {
let host = api_url
.trim_start_matches("https://")
.trim_start_matches("http://");
let scheme = if api_url.starts_with("https://") {
"https"
} else {
"http"
};
Some(format!("{scheme}://{bucket}.{host}"))
} else {
None
};
s3_config.url = public_url;
disks.insert("s3".to_string(), s3_config);
}
Self { default, disks }
}
/// Add a disk configuration.
pub fn disk(mut self, name: impl Into<String>, config: DiskConfig) -> Self {
self.disks.insert(name.into(), config);
self
}
/// Set the default disk.
pub fn default_disk(mut self, name: impl Into<String>) -> Self {
self.default = name.into();
self
}
/// Get the default disk name.
pub fn get_default(&self) -> &str {
&self.default
}
/// Get a disk configuration by name.
pub fn get_disk(&self, name: &str) -> Option<&DiskConfig> {
self.disks.get(name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_storage_config_defaults() {
let config = StorageConfig::default();
assert_eq!(config.default, "local");
assert!(config.disks.contains_key("local"));
}
#[test]
fn test_storage_config_builder() {
let config = StorageConfig::new("s3")
.disk("local", DiskConfig::local("./storage"))
.disk("public", DiskConfig::local("./public").with_url("/files"));
assert_eq!(config.default, "s3");
assert!(config.disks.contains_key("local"));
assert!(config.disks.contains_key("public"));
}
#[test]
fn test_storage_config_from_env() {
// Test with default env (no env vars set)
let config = StorageConfig::from_env();
assert_eq!(config.default, "local");
assert!(config.disks.contains_key("local"));
assert!(config.disks.contains_key("public"));
}
}