File Uploads & Object Storage
Relevant source files
The following files were used as context for generating this wiki page:
The File Uploads and Object Storage system in OpenChat facilitates the handling, storage, and retrieval of user-provided attachments and AI-generated images. The system utilizes Cloudflare R2 for scalable object storage and Convex for real-time metadata management and database operations.
This module ensures that files are securely uploaded, validated against strict MIME type and size constraints, and correctly associated with specific chat sessions. It supports multi-modal interactions by allowing users to provide images and documents to compatible AI models.
System Architecture
The architecture is built on a multi-stage pipeline involving the client, the Convex backend, and Cloudflare R2. The process is designed to handle both user-initiated uploads and system-generated images (e.g., from image generation models).
Data Flow Overview
The following sequence diagram illustrates the standard upload flow for a user attachment.
This flow ensures that the backend remains aware of the file's lifecycle, from the initial intent to upload until the file is permanently associated with a chat record.
Sources: convex/files.ts:16-55, src/lib/server-upload-helpers.ts:21-65
Storage Configuration & Constraints
OpenChat enforces strict limits on file uploads to maintain performance and security. These constants are centralized in the configuration layer.
| Configuration | Value | Description |
|---|---|---|
UPLOAD_MAX_BYTES | 10,485,760 (10 MiB) | Maximum allowed size per file. |
UPLOAD_ALLOWED_MIME | JPEG, PNG, GIF, WEBP, PDF | Supported file types for manual uploads. |
DAILY_FILE_UPLOAD_LIMIT | System Defined | Quantitative limit on daily uploads per user. |
Sources: src/lib/config/upload.ts:4-13, src/lib/config/constants.test.ts:22-24
Supported Models for File Input
Not all AI models in the system support multi-modal inputs. The backend maintains a whitelist of models permitted to receive file attachments, including:
- Anthropic: Claude 4.5 series (Opus, Sonnet, Haiku).
- OpenAI: GPT-4o, GPT-5 series, o3/o4 mini.
- Google: Gemini 2.0/2.5/3.1 series.
- Other: Pixtral Large, Grok-3/4.1, GLM-4.5v.
Sources: convex/files.ts:70-120
Core Components & Logic
R2 Integration
The system uses the @convex-dev/r2 package to interface with Cloudflare. The R2 client handles low-level operations like generating signed URLs and deleting objects.
generateUploadUrl: Mutation that provides the client with a temporary pre-signed URL to upload directly to R2.syncMetadata: Ensures the Convex database has the most recent size and content-type information from the physical storage.deleteAttachments: Performs a parallel deletion of the R2 object and the corresponding database record.
Sources: convex/files.ts:13-68, convex/files.ts:335-364
Metadata Management
Files are tracked in the chat_attachments table. The lifecycle of an attachment record transitions through several states:
- Pending: Created via
onUploadwhen the upload process starts. - Synced: Updated via
onSyncMetadataonce R2 has processed the file headers. - Finalized: Associated with a
chatIdand validated viainternalSaveorinternalSaveGenerated.
Sources: convex/files.ts:19-58, convex/files.ts:241-265
Server-Side Helpers
For internal processes (like background agents or API routes), the system provides uploadBlobToR2. This helper encapsulates the entire 4-step client-side sequence into a single asynchronous server-side operation.
export async function uploadBlobToR2(blob: Blob, options: { chatId: Id<"chats">, ... }) {
const { url, key } = await fetchMutation(api.files.generateUploadUrl, ...);
await fetch(url, { method: "PUT", body: blob });
await fetchMutation(api.files.syncMetadata, { key }, ...);
return await fetchAction(api.files.saveFileAttachment, { key, ... });
}
Sources: src/lib/server-upload-helpers.ts:21-65
Data Schema: chat_attachments
The system stores file metadata with the following structure in Convex:
| Field | Type | Description |
|---|---|---|
_id | Id<"chat_attachments"> | Unique identifier. |
userId | Id<"users"> | Owner of the file. |
chatId | Id<"chats"> | Chat session association. |
key | string | The R2 object key. |
fileName | string | Sanitized display name. |
fileType | string | MIME type (e.g., image/png). |
fileSize | number | Size in bytes. |
isGenerated | boolean | Flag for AI-generated images. |
url | string (Optional) | Public access URL (if configured). |
Sources: convex/files.ts:125-135, convex/files.ts:316-324
Conclusion
The File Uploads & Object Storage system provides a robust framework for handling multi-modal data in OpenChat. By offloading heavy lifting to Cloudflare R2 while maintaining transactional metadata in Convex, the system achieves a balance of scalability and real-time consistency. Strict validation layers ensure that storage remains within operational limits and that attachments are strictly isolated by user and chat session.