elif-storage
A comprehensive multi-backend file storage system for the elif.rs framework.
Features
- Multi-backend support: Local filesystem, AWS S3 support
- File upload validation: Size, type, and content validation with security checks
- Image processing: Resize, crop, optimize, and format conversion (optional feature)
- CDN integration: Signed URLs and public URL generation
- Access control: File permissions and access control system (optional feature)
- Temporary file management: Automatic cleanup of temporary files
- Streaming support: Handle large files efficiently with async streaming
- Async-first: Built for modern async Rust applications
Quick Start
Add to your Cargo.toml:
[]
= { = "0.1.0", = "path/to/elif/crates/elif-storage" }
Basic Usage
use ;
async
AWS S3 Backend
Enable the AWS S3 feature:
[]
= { = "0.1.0", = "path/to/elif", = ["aws-s3"] }
use ;
async
Features
File Validation
The storage system includes comprehensive file validation:
use ;
// Create validator with custom rules
let validation_config = new
.max_size // 10MB max
.allow_mime_types
.block_extensions
.validate_content; // Check file content matches declared type
let validator = new;
// Validate a file before storage
let file_data = b"some file content";
validator.validate_file?;
Upload Options
Customize file uploads with metadata and options:
use UploadOptions;
let options = new
.content_type
.metadata
.metadata
.cache_control
.overwrite; // Allow overwriting existing files
let metadata = storage.put.await?;
Image Processing
Enable image processing features:
[]
= { = "0.1.0", = ["image-processing"] }
use ;
let processor = new
.with_max_dimensions
.with_allowed_formats;
let operations = vec!;
let processed_data = processor.process_image?;
Temporary File Management
Automatic cleanup of temporary files:
use ;
use Duration;
// Create temporary file that auto-deletes on drop
let temp_file = new.await?;
// Use temp_file.path() to get the path
// File is automatically deleted when temp_file is dropped
// Or manage cleanup manually
let cleanup_manager = new
.add_temp_directory
.with_max_age // 24 hours
.with_cleanup_interval; // Check hourly
let handle = cleanup_manager.start.await?;
Storage Backend API
All storage backends implement the StorageBackend trait:
Available Features
default: Local filesystem backend onlyaws-s3: Enable AWS S3 backend supportimage-processing: Enable image manipulation capabilitiesaccess-control: Enable file permissions and access controlall: Enable all features
Examples
Run the included examples:
# Basic usage example
# Image processing example (requires image-processing feature)
# S3 example (requires aws-s3 feature)
Safety and Security
- Path sanitization: Prevents directory traversal attacks
- Content validation: Verifies file content matches declared MIME type
- Malware detection: Basic detection of executable files and dangerous content
- Size limits: Configurable file size restrictions
- Type restrictions: MIME type allowlists and blocklists
License
This project is licensed under the MIT OR Apache-2.0 license.