Automatic Cleanup System:
- Background thread cleans files older than 5 minutes every minute
- Tracks all temporary files in a registry with creation timestamps
- Automatic cleanup on app shutdown with atexit handler
- Orphaned file detection and removal
- Thread-safe cleanup implementation
File Management:
- Unique filenames with timestamps prevent collisions
- Configurable upload folder via UPLOAD_FOLDER environment variable
- Automatic folder creation with proper permissions
- Fallback to system temp if primary folder fails
- File registration for all uploads and generated audio
Health Monitoring:
- /health/storage endpoint shows temp file statistics
- Tracks file count, total size, oldest file age
- Disk space monitoring and warnings
- Real-time cleanup status information
- Warning when files exceed thresholds
Administrative Tools:
- maintenance.sh script for manual operations
- Status checking, manual cleanup, real-time monitoring
- /admin/cleanup endpoint for emergency cleanup (requires auth token)
- Configurable retention period (default 5 minutes)
Security Improvements:
- Filename sanitization in get_audio endpoint
- Directory traversal prevention
- Cache headers to reduce repeated downloads
- Proper file existence checks
Performance:
- Efficient batch cleanup operations
- Minimal overhead with background thread
- Smart registry management
- Automatic garbage collection after operations
This prevents disk space exhaustion by ensuring temporary files are
automatically cleaned up after use, with multiple failsafes.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>