Files
gochat/internal/model/widget_file_upload.go
T
2026-06-04 15:44:48 +08:00

72 lines
4.1 KiB
Go

package model
import (
"time"
"gorm.io/datatypes"
)
// WidgetFileUploadStatus constants for the staged upload process.
type WidgetFileUploadStatus string
const (
WidgetFileUploadStatusPending WidgetFileUploadStatus = "pending" // File uploaded, awaiting attachment to message
WidgetFileUploadStatusAttached WidgetFileUploadStatus = "attached" // File attached to a message
WidgetFileUploadStatusExpired WidgetFileUploadStatus = "expired" // Upload expired (not attached within timeout)
)
// WidgetFileUpload stores metadata about a file uploaded through the web widget.
// Reference: Chatwoot widget SDK file upload — attachment processing pipeline
// Widget file uploads go through a staged process:
// 1. Visitor selects file in widget
// 2. File is uploaded to storage via /widget/uploads endpoint
// 3. Upload record is created with status "pending"
// 4. When message is sent with attachment, status changes to "attached"
type WidgetFileUpload struct {
Base
UploadUUID string `gorm:"size:36;uniqueIndex;not null" json:"upload_uuid"` // UUID for public-facing tracking (website_token staging)
WidgetToken string `gorm:"size:255;index" json:"widget_token,omitempty"` // Links to the widget session (widget_token auth)
InboxID uint `gorm:"index;not null" json:"inbox_id"` // The inbox this upload belongs to
ContactID uint `gorm:"index" json:"contact_id,omitempty"` // Contact who uploaded (may be 0 for anonymous)
Status WidgetFileUploadStatus `gorm:"size:20;default:'pending';index" json:"status"` // "pending", "attached", "expired"
// === File metadata ===
OriginalName string `gorm:"size:255;not null" json:"original_name"` // Original filename from user
FileType string `gorm:"size:50;not null" json:"file_type"` // MIME type / category: "image", "audio", "video", "file"
MimeType string `gorm:"size:100" json:"mime_type,omitempty"` // Full MIME type (e.g. "image/png")
FileSize int64 `gorm:"not null" json:"file_size"` // File size in bytes
FileURL string `gorm:"size:512" json:"file_url,omitempty"` // URL where file is stored (after upload)
ThumbURL string `gorm:"size:512" json:"thumb_url,omitempty"` // Thumbnail URL (for images)
// === Dimensions (for images/videos) ===
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
// === Processing ===
Metadata datatypes.JSON `gorm:"type:jsonb" json:"metadata,omitempty"` // Additional metadata (checksums, etc)
MessageID *uint `gorm:"index" json:"message_id,omitempty"` // Linked message (after attachment)
ExpiresAt time.Time `gorm:"index" json:"expires_at"` // Expiry timestamp for cleanup
Inbox Inbox `gorm:"foreignKey:InboxID" json:"inbox,omitempty"`
}
func (WidgetFileUpload) TableName() string { return "widget_file_uploads" }
// WidgetUploadAllowedTypes defines the allowed MIME types for widget uploads.
// Reference: Chatwoot widget SDK config — allowed file types restriction
var WidgetUploadAllowedTypes = map[string][]string{
"image": {"image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"},
"audio": {"audio/mp3", "audio/ogg", "audio/wav", "audio/webm", "audio/mpeg"},
"video": {"video/mp4", "video/webm", "video/ogg"},
"file": {"application/pdf", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain", "text/csv"},
}
// WidgetUploadMaxSizeByType defines max file sizes per category.
var WidgetUploadMaxSizeByType = map[string]int64{
"image": 5 * 1024 * 1024, // 5MB for images
"audio": 10 * 1024 * 1024, // 10MB for audio
"video": 15 * 1024 * 1024, // 15MB for video
"file": 10 * 1024 * 1024, // 10MB for documents
}