A customizable server-side hook service for TUS (resumable upload) protocol that enables advanced file processing and validation capabilities.
TUS protocol provides excellent resumable file uploads, but the uploaded files are stored as binary data without additional processing. This hook service addresses that limitation by providing:
- Server-side Hook Processing: Intercepts TUS upload events to add custom business logic
- File Validation: Implements file size limits, MIME type restrictions, and authentication
- Metadata Enhancement: Enriches file metadata for better organization and processing
- Storage Integration: Seamless integration with MinIO/S3 compatible storage
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ TUS Client │───▶│ TUS Server │───▶│ Hook Service │
│ (Any Client) │ │ (tusd) │ │ (FastAPI) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ MinIO/S3 │ │ Custom Logic │
│ (Storage) │ │ (Validation) │
└─────────────────┘ └─────────────────┘
- File Size Validation: Configurable maximum file size limits (default: 1GB)
- Pre-upload Validation: Reject uploads before they start based on custom criteria
- Metadata Processing: Extract and enhance file metadata from TUS headers
- Storage Integration: Direct integration with MinIO/S3 compatible storage
- Webhook Processing: Handle all TUS lifecycle events (pre-create, post-finish, etc.)
- Logging & Monitoring: Comprehensive logging for upload tracking and debugging
- Authentication: Add custom authentication logic in hook handlers
- File Type Restrictions: Validate MIME types and file extensions
- Business Logic: Implement custom processing workflows
- Notifications: Trigger alerts or notifications on upload events
- Data Processing: Automatically process files after successful uploads
# Start the entire stack
docker-compose up -d
# Access the web interface
open http://localhost:9908
# TUS server endpoint
curl -I http://localhost:9508/files
# Hook service health check
curl http://localhost:8000/api/v1/health# Install dependencies
pip install -r requirements.txt
# Start the service
uvicorn app.main:app --host 0.0.0.0 --port 8000# Application settings
APP_NAME="TUS Hook Service"
DEBUG=true
HOST=0.0.0.0
PORT=8000
# Storage configuration
S3_ENDPOINT=localhost:19000
S3_ACCESS_KEY=minio@minio
S3_SECRET_KEY=minio@minio
S3_SECURE=false
S3_BUCKET=ossThe TUS server should be configured to send webhooks to this service:
tusd -hooks-http http://localhost:8000/api/v1/webhook \
-hooks-http-retry 3 \
-hooks-http-backoff 3s- Validate file types and sizes before storage
- Extract metadata for automatic organization
- Trigger content processing workflows
- Validate media file formats
- Trigger encoding or thumbnail generation
- Organize media by type and date
- Verify document integrity
- Extract document metadata
- Implement access control and permissions
- Enforce corporate file policies
- Audit file upload activities
- Integrate with existing authentication systems
├── app/ # Hook service application
│ ├── api/v1/ # REST API endpoints
│ │ ├── webhook.py # TUS webhook handler
│ │ ├── health.py # Health check endpoint
│ │ └── files.py # File management API
│ ├── core/ # Core configuration
│ │ ├── config.py # Application settings
│ │ ├── logger.py # Logging configuration
│ │ └── minio_client.py # Storage client
│ ├── models/ # Data models
│ │ └── webhook.py # TUS webhook models
│ ├── services/ # Business logic
│ │ ├── webhook_service.py # Hook orchestration
│ │ ├── hook_handlers.py # Individual hook handlers
│ │ ├── metadata_service.py # Metadata processing
│ │ └── file_service.py # File operations
│ └── main.py # Application entry point
├── public/ # Web interface for testing
├── docker-compose.yml # Complete development stack
└── env.example # Environment configuration template
Once running, visit http://localhost:8000/docs for interactive API documentation.
POST /api/v1/webhook- TUS webhook receiverGET /api/v1/health- Service health checkGET /api/v1/files- File listing and management
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
pytest
# Run with coverage
pytest --cov=app- Extend the appropriate handler in
app/services/hook_handlers.py - Add validation logic in the
handle()method - Return appropriate
HookResponsewith actions
Example:
class PreCreateHandler(BaseHookHandler):
def handle(self, hook_request: HookRequest, metadata_context: dict) -> HookResponse:
# Add your custom validation logic here
if should_reject_upload(hook_request):
response = HookResponse()
response.reject_upload = True
response.http_response = HTTPResponse(
StatusCode=403,
Body="Upload rejected by policy"
)
return response
return HookResponse()For command-line TUS uploads, check out our companion CLI tool: go-tus-cli
This project builds upon several excellent open source projects:
- TUS Protocol - The resumable upload protocol specification
- tusd - Official TUS server implementation
- FastAPI - Modern Python web framework
- MinIO - S3-compatible object storage server
- Uppy - File uploader for web browsers
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request