backbone-storage 3.0.2

Backbone Framework Storage Module - Local file system support
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Storage service traits

use async_trait::async_trait;
use crate::{
    StorageResult, StorageFile, StorageConfig, StorageStats,
    UploadOptions, DownloadOptions, PresignedUrlOptions, ByteRange,
    types::EncryptionConfig,
};
use std::collections::HashMap;
use bytes::Bytes;

/// Generic storage service trait
#[async_trait]
pub trait StorageService: Send + Sync {
    /// Upload a file from bytes
    async fn upload_bytes(
        &self,
        path: &str,
        data: Bytes,
        options: Option<UploadOptions>,
    ) -> StorageResult<StorageFile>;

    /// Upload a file from a reader
    async fn upload_reader(
        &self,
        path: &str,
        reader: Box<dyn tokio::io::AsyncRead + Send + Unpin>,
        size: Option<u64>,
        options: Option<UploadOptions>,
    ) -> StorageResult<StorageFile>;

    /// Upload a file from local path
    async fn upload_file(
        &self,
        path: &str,
        local_path: &str,
        options: Option<UploadOptions>,
    ) -> StorageResult<StorageFile>;

    /// Download a file to bytes
    async fn download_bytes(
        &self,
        path: &str,
        options: Option<DownloadOptions>,
    ) -> StorageResult<Bytes>;

    /// Download a file to a writer
    async fn download_writer(
        &self,
        path: &str,
        writer: Box<dyn tokio::io::AsyncWrite + Send + Unpin>,
        options: Option<DownloadOptions>,
    ) -> StorageResult<()>;

    /// Download a file to local path
    async fn download_file(
        &self,
        path: &str,
        local_path: &str,
        options: Option<DownloadOptions>,
    ) -> StorageResult<()>;

    /// Stream a file (useful for large files)
    async fn stream_file(
        &self,
        path: &str,
        range: Option<ByteRange>,
    ) -> StorageResult<Box<dyn futures::stream::Stream<Item = StorageResult<Bytes>> + Send + Unpin>>;

    /// Get file metadata
    async fn get_file(&self, path: &str) -> StorageResult<StorageFile>;

    /// Check if file exists
    async fn file_exists(&self, path: &str) -> StorageResult<bool>;

    /// Delete a file
    async fn delete_file(&self, path: &str) -> StorageResult<bool>;

    /// Copy a file
    async fn copy_file(&self, from_path: &str, to_path: &str) -> StorageResult<StorageFile>;

    /// Move a file
    async fn move_file(&self, from_path: &str, to_path: &str) -> StorageResult<StorageFile>;

    /// List files in a directory/prefix
    async fn list_files(
        &self,
        prefix: &str,
        limit: Option<u32>,
        continuation_token: Option<String>,
    ) -> StorageResult<FileListResult>;

    /// Generate presigned URL for file operations
    async fn generate_presigned_url(
        &self,
        path: &str,
        options: PresignedUrlOptions,
    ) -> StorageResult<String>;

    /// Get file URL (public access or presigned)
    async fn get_file_url(
        &self,
        path: &str,
        expiry_seconds: Option<u64>,
    ) -> StorageResult<String>;

    /// Update file metadata
    async fn update_metadata(
        &self,
        path: &str,
        metadata: HashMap<String, String>,
    ) -> StorageResult<StorageFile>;

    /// Get storage statistics
    async fn get_stats(&self) -> StorageResult<StorageStats>;

    /// Test storage connection
    async fn test_connection(&self) -> StorageResult<bool>;

    /// Get storage backend type
    fn backend_type(&self) -> crate::StorageBackend;

    /// Get bucket/container name
    fn bucket(&self) -> &str;

    /// Get configuration
    fn config(&self) -> &StorageConfig;
}

/// Storage management trait for administrative operations
#[async_trait]
pub trait StorageManager: Send + Sync {
    /// Create a new bucket/container
    async fn create_bucket(&self, bucket_name: &str, config: Option<BucketConfig>) -> StorageResult<bool>;

    /// Delete a bucket/container
    async fn delete_bucket(&self, bucket_name: &str, force: bool) -> StorageResult<bool>;

    /// List all buckets/containers
    async fn list_buckets(&self) -> StorageResult<Vec<String>>;

    /// Get bucket/container information
    async fn get_bucket_info(&self, bucket_name: &str) -> StorageResult<BucketInfo>;

    /// Set bucket lifecycle rules
    async fn set_lifecycle_rules(
        &self,
        bucket_name: &str,
        rules: Vec<LifecycleRule>,
    ) -> StorageResult<bool>;

    /// Enable bucket versioning
    async fn enable_versioning(&self, bucket_name: &str) -> StorageResult<bool>;

    /// Disable bucket versioning
    async fn disable_versioning(&self, bucket_name: &str) -> StorageResult<bool>;

    /// Set bucket access control
    async fn set_access_control(
        &self,
        bucket_name: &str,
        policy: AccessPolicy,
    ) -> StorageResult<bool>;

    /// Get bucket access control
    async fn get_access_control(&self, bucket_name: &str) -> StorageResult<AccessPolicy>;

    /// Restore from backup
    async fn restore_backup(
        &self,
        backup_path: &str,
        target_bucket: &str,
        target_prefix: Option<String>,
    ) -> StorageResult<RestoreResult>;

    /// Create backup
    async fn create_backup(
        &self,
        bucket_name: &str,
        backup_path: &str,
        prefix_filter: Option<String>,
    ) -> StorageResult<BackupResult>;
}

/// Storage monitoring trait
#[async_trait]
pub trait StorageMonitor: Send + Sync {
    /// Start monitoring storage operations
    async fn start_monitoring(&self, bucket_name: &str) -> StorageResult<()>;

    /// Stop monitoring storage operations
    async fn stop_monitoring(&self, bucket_name: &str) -> StorageResult<()>;

    /// Get real-time metrics
    async fn get_metrics(&self, bucket_name: &str) -> StorageResult<HashMap<String, f64>>;

    /// Set alert thresholds
    async fn set_alert_thresholds(
        &self,
        bucket_name: &str,
        thresholds: AlertThresholds,
    ) -> StorageResult<()>;

    /// Get current alerts
    async fn get_alerts(&self, bucket_name: &str) -> StorageResult<Vec<StorageAlert>>;

    /// Get usage trends
    async fn get_usage_trends(
        &self,
        bucket_name: &str,
        period: TrendPeriod,
    ) -> StorageResult<UsageTrend>;

    /// Generate storage report
    async fn generate_report(
        &self,
        bucket_name: &str,
        report_type: ReportType,
    ) -> StorageResult<StorageReport>;
}

/// Result type for file listing operations
#[derive(Debug, Clone)]
pub struct FileListResult {
    /// List of files
    pub files: Vec<StorageFile>,

    /// Common prefixes (directories)
    pub prefixes: Vec<String>,

    /// Whether there are more results
    pub truncated: bool,

    /// Token for next page of results
    pub next_continuation_token: Option<String>,

    /// Total number of files (if available)
    pub total_count: Option<u64>,
}

/// Bucket configuration
#[derive(Debug, Clone)]
pub struct BucketConfig {
    /// Geographic region
    pub region: Option<String>,

    /// Access policy
    pub access_policy: Option<AccessPolicy>,

    /// Storage class
    pub storage_class: Option<String>,

    /// Enable versioning
    pub versioning: Option<bool>,

    /// Lifecycle rules
    pub lifecycle_rules: Option<Vec<LifecycleRule>>,

    /// Tags
    pub tags: Option<HashMap<String, String>>,

    /// Encryption settings
    pub encryption: Option<EncryptionConfig>,
}

/// Bucket information
#[derive(Debug, Clone)]
pub struct BucketInfo {
    /// Bucket name
    pub name: String,

    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,

    /// Geographic region
    pub region: String,

    /// Storage class
    pub storage_class: String,

    /// Versioning status
    pub versioning: VersioningStatus,

    /// Access policy
    pub access_policy: AccessPolicy,

    /// Total size in bytes
    pub size_bytes: u64,

    /// Number of objects
    pub object_count: u64,

    /// Tags
    pub tags: HashMap<String, String>,
}

/// Versioning status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersioningStatus {
    Enabled,
    Suspended,
    Disabled,
}

/// Lifecycle rule
#[derive(Debug, Clone)]
pub struct LifecycleRule {
    /// Rule ID
    pub id: String,

    /// Rule status
    pub status: LifecycleRuleStatus,

    /// Filter for objects
    pub filter: LifecycleFilter,

    /// Action to perform
    pub action: LifecycleAction,
}

/// Lifecycle rule status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleRuleStatus {
    Enabled,
    Disabled,
}

/// Lifecycle filter
#[derive(Debug, Clone)]
pub struct LifecycleFilter {
    /// Object prefix
    pub prefix: Option<String>,

    /// Object tags
    pub tags: Option<HashMap<String, String>>,

    /// Object size range
    pub size_range: Option<(u64, u64)>,
}

/// Lifecycle action
#[derive(Debug, Clone)]
pub struct LifecycleAction {
    /// Action type
    pub action_type: LifecycleActionType,

    /// Number of days for transition/expiration
    pub days: Option<u32>,

    /// Storage class for transition
    pub storage_class: Option<String>,
}

/// Lifecycle action types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleActionType {
    /// Delete object
    Delete,

    /// Transition to different storage class
    Transition,

    /// Expire object
    Expire,

    /// Archive to glacier
    Archive,
}

/// Access policy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessPolicy {
    Private,
    PublicRead,
    PublicReadWrite,
    Custom,
}

/// Restore operation result
#[derive(Debug, Clone)]
pub struct RestoreResult {
    /// Number of files restored
    pub files_restored: u64,

    /// Total bytes restored
    pub bytes_restored: u64,

    /// Number of files failed
    pub files_failed: u64,

    /// Errors encountered
    pub errors: Vec<String>,

    /// Duration in seconds
    pub duration_seconds: u64,
}

/// Backup operation result
#[derive(Debug, Clone)]
pub struct BackupResult {
    /// Number of files backed up
    pub files_backed_up: u64,

    /// Total bytes backed up
    pub bytes_backed_up: u64,

    /// Backup file path
    pub backup_path: String,

    /// Checksum of backup
    pub backup_checksum: String,

    /// Number of files skipped
    pub files_skipped: u64,

    /// Errors encountered
    pub errors: Vec<String>,

    /// Duration in seconds
    pub duration_seconds: u64,
}

/// Alert thresholds
#[derive(Debug, Clone)]
pub struct AlertThresholds {
    /// Storage usage percentage (0-100)
    pub storage_usage_percent: Option<f64>,

    /// File count limit
    pub file_count_limit: Option<u64>,

    /// Large file size threshold in bytes
    pub large_file_threshold: Option<u64>,

    /// Error rate percentage (0-100)
    pub error_rate_percent: Option<f64>,

    /// Average response time in milliseconds
    pub avg_response_time_ms: Option<f64>,
}

/// Storage alert
#[derive(Debug, Clone)]
pub struct StorageAlert {
    /// Alert ID
    pub id: String,

    /// Alert type
    pub alert_type: StorageAlertType,

    /// Alert severity
    pub severity: AlertSeverity,

    /// Alert message
    pub message: String,

    /// Alert timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Associated bucket
    pub bucket: String,

    /// Additional details
    pub details: HashMap<String, String>,
}

/// Storage alert types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StorageAlertType {
    StorageUsage,
    FileCount,
    LargeFile,
    ErrorRate,
    ResponseTime,
    ConnectionFailure,
    PermissionDenied,
}

/// Alert severity
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Trend period for analytics
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrendPeriod {
    LastHour,
    Last24Hours,
    Last7Days,
    Last30Days,
    Custom { hours: u32 },
}

/// Usage trend data
#[derive(Debug, Clone)]
pub struct UsageTrend {
    /// Trend period
    pub period: TrendPeriod,

    /// Data points
    pub data_points: Vec<TrendDataPoint>,

    /// Growth rate (percentage)
    pub growth_rate: f64,

    /// Predicted usage for next period
    pub predicted_usage: Option<u64>,
}

/// Trend data point
#[derive(Debug, Clone)]
pub struct TrendDataPoint {
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Storage usage in bytes
    pub usage_bytes: u64,

    /// File count
    pub file_count: u64,

    /// Number of operations
    pub operations: u64,
}

/// Report type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportType {
    Usage,
    Performance,
    Cost,
    Security,
    Compliance,
}

/// Storage report
#[derive(Debug, Clone)]
pub struct StorageReport {
    /// Report type
    pub report_type: ReportType,

    /// Generated at timestamp
    pub generated_at: chrono::DateTime<chrono::Utc>,

    /// Report data
    pub data: serde_json::Value,

    /// Summary statistics
    pub summary: HashMap<String, String>,

    /// Recommendations
    pub recommendations: Vec<String>,
}