Overview
Mirage allows you to upload and share files within chat rooms. Files are uploaded to an external storage service and shared as URLs in room messages.File Upload Process
Files are uploaded through a dedicated endpoint and automatically posted as messages in the target room.Prepare your file and authentication
You’ll need:
- Your authentication token
- The file to upload
- The target room ID
Unlike other endpoints, file uploads don’t use
Content-Type: application/json because they use multipart form data.File Size Limit
The 24MB limit is enforced server-side to prevent resource exhaustion.Calculating File Size
Upload Service Configuration
Files are uploaded to an external service configured inapp/config.py:9:
Upload Implementation
The upload process uses the requests library to forward files to the storage service:Error Handling
Error Handling
The file uploader handles multiple error scenarios:
Room Membership Requirement
You must be a member of the target room to upload files.File URLs as Messages
When a file is successfully uploaded, its URL is automatically posted as a message in the room.Message Lifecycle
File URLs follow the same lifecycle as regular messages:- Subject to 30-minute expiration (
MESSAGE_LIFESPAN) - Count toward the 100-message limit (
MAX_MESSAGES) - Removed when room reaches capacity
While the message containing the file URL expires, the file itself remains on the storage service. Save important file URLs externally if needed.
Supported File Types
Mirage usesContent-Type: application/octet-stream for uploads, making it file-type agnostic.
Documents
PDF, DOC, DOCX, TXT, MD
Images
PNG, JPG, JPEG, GIF, SVG, WEBP
Archives
ZIP, TAR, GZ, RAR, 7Z
Media
MP4, MP3, WAV, AVI, MOV
Error Handling
Common Upload Errors
| Status Code | Error | Description | Solution |
|---|---|---|---|
400 | No file provided | File missing from request | Include file in request |
400 | No file name provided | File has empty filename | Provide valid filename |
400 | No room ID provided | Missing room_id parameter | Include room ID in form data |
400 | File size exceeds 24MB limit | File too large | Reduce file size or split file |
401 | Invalid token or token missing | Auth token invalid | Provide valid auth token |
401 | Unauthorized access | Token doesn’t match a user | Re-authenticate |
403 | Not a member of this room | User not in target room | Join room first |
500 | File upload failed | External service error | Retry or contact support |
Handling Upload Failures
Implement retry logic for transient failures:Complete Upload Example
Here’s a full example with error handling:Upload Flow Diagram
Server Ping Mechanism
When uploading files, Mirage pings the external storage service to keep it awake:This background thread prevents the external service from going to sleep during upload operations.
Best Practices
Compress files before uploading
Compress files before uploading
Stay well under the 24MB limit by compressing files when possible. Use ZIP for multiple files or compress images/videos.
Use descriptive filenames
Use descriptive filenames
Clear filenames help other room members understand what they’re downloading.
Validate files client-side
Validate files client-side
Check file size before uploading to provide immediate feedback to users.
Save important file URLs
Save important file URLs
Since messages expire after 30 minutes, save file URLs externally if you need long-term access.
Handle upload errors gracefully
Handle upload errors gracefully
Network issues can occur. Implement retry logic and provide clear error messages to users.
Consider bandwidth
Consider bandwidth
Large files take time to upload. Provide progress indicators for better UX.
Security Considerations
Token Security
Never expose your auth token in client-side code or logs. Always use secure server-side uploads.
File Validation
While Mirage doesn’t restrict file types, implement your own validation for security.
Virus Scanning
Consider scanning files for malware before upload if dealing with untrusted sources.
Access Control
Room membership verification ensures only authorized users can upload files.
Troubleshooting
Upload times out
Upload times out
Large files or slow connections can cause timeouts. Try:
- Compressing the file
- Using a faster internet connection
- Increasing timeout values in your HTTP client
- Splitting large files into smaller chunks
File URL not appearing in messages
File URL not appearing in messages
If the upload succeeds but you don’t see the message:
- Check room membership status
- Verify you’re listening to the correct room ID
- Ensure messages haven’t expired (30-minute limit)
- Check if room has exceeded 100 message limit
External storage service unreachable
External storage service unreachable
The storage service (
cpp-webserver.onrender.com) may occasionally be down:- Wait a few minutes and retry
- Check service status if available
- Contact administrator if issue persists
File size error despite being under 24MB
File size error despite being under 24MB
Some clients report
Content-Length differently:- Ensure you’re measuring raw file size, not encoded size
- Check for multipart form encoding overhead
- Try uploading a slightly smaller file to test
Alternative: Direct Storage Service Upload
For advanced use cases, you could potentially upload directly to the storage service:Next Steps
Creating Rooms
Learn how to create rooms for file sharing
Content Moderation
Understand content safety features