Files
archie/internal/domain/messaging/media.go
T
sam 360da3058f fix(messaging): say what the inbound media check actually checks
The check compares two declarations; it never reads the bytes, so it cannot
tell whether a file is what its content type says. The comment and the test
row claiming otherwise have gone, and so has an allowance letting a video
carry an image type, which no producer emits.

A caller that named the attachment type itself -- a browser upload -- must now
also state a content type, since the previous empty-type escape made the check
optional there. The Telegram declared-size rule is deleted: the shared check
runs first with the same ceiling, so only the rule on the bytes actually
received can still fire.
2026-10-25 18:13:30 +11:00

144 lines
4.7 KiB
Go

package messaging
import (
"fmt"
"mime"
"strings"
)
// MaxInboundAttachmentBytes is the largest attachment carried to the Gateway.
const MaxInboundAttachmentBytes = 20 << 20
// Media types. MediaTypeVoice is recorded speech, which is transcribed;
// MediaTypeAudio is other audio.
const (
MediaTypeImage = "image"
MediaTypeVideo = "video"
MediaTypeAudio = "audio"
MediaTypeDocument = "document"
MediaTypeVoice = "voice"
)
// MediaAttachment describes a file attached to a message. Pointer fields
// distinguish zero from unknown.
type MediaAttachment struct {
// Type is "image", "video", "audio", "voice", or "document".
Type string `json:"type"`
// FileID is the platform-assigned identifier for download APIs.
FileID string `json:"file_id"`
// URL is a direct download URL when available. May be empty for
// platforms that require a download API call.
URL string `json:"url,omitempty"`
// Path is a local file to upload. Exactly one of URL and Path is used for an
// outbound send.
Path string `json:"path,omitempty"`
// MIMEType is the content type (e.g. "image/png", "audio/ogg").
MIMEType string `json:"mime_type,omitempty"`
// FileName is the original file name when known.
FileName string `json:"file_name,omitempty"`
// FileSize is the attachment size in bytes. nil means unknown.
FileSize *int64 `json:"file_size,omitempty"`
// Width is the pixel width for images and video. nil means unknown
// or not applicable.
Width *int `json:"width,omitempty"`
// Height is the pixel height for images and video. nil means unknown
// or not applicable.
Height *int `json:"height,omitempty"`
// Duration is the play time in seconds for audio and video.
Duration *int `json:"duration,omitempty"`
// Raw carries platform-specific fields the common vocabulary does not
// cover, so adapters never need a second attachment type.
Raw map[string]any `json:"raw,omitempty"`
// Data holds the attachment bytes in-process on the inbound path. Never
// persisted.
Data []byte `json:"data,omitempty"`
}
// ValidateInbound refuses an attachment whose declared content type contradicts
// its declared attachment type, or whose declared size is over the ceiling. The
// declaration decides how the file is used -- a voice note is transcribed, an
// image is sent to a model -- so two declarations that disagree are refused
// rather than resolved by guessing. It cannot tell whether the bytes are what
// the content type says: nothing here reads them.
func ValidateInbound(att MediaAttachment) error {
fileName := att.FileName
if fileName == "" {
fileName = att.Type
}
switch att.Type {
case MediaTypeImage, MediaTypeVideo, MediaTypeAudio, MediaTypeVoice, MediaTypeDocument:
default:
return fmt.Errorf("media: %s: unknown attachment type %q", fileName, att.Type)
}
for _, size := range []int64{int64(len(att.Data)), int64Of(att.FileSize)} {
if size > MaxInboundAttachmentBytes {
return fmt.Errorf("media: %s is %d bytes, over the %d byte limit", fileName, size, MaxInboundAttachmentBytes)
}
}
if att.MIMEType == "" {
// A platform adapter may genuinely not know the content type. A caller
// that chose the attachment type itself must supply one; see
// ValidateAuthoredInbound.
return nil
}
mediaType, _, err := mime.ParseMediaType(att.MIMEType)
if err != nil {
return fmt.Errorf("media: %s: %q is not a content type", fileName, att.MIMEType)
}
if !typeCarriesMIME(att.Type, mediaType) {
return fmt.Errorf("media: %s: %q cannot be a %s", fileName, mediaType, att.Type)
}
return nil
}
// typeCarriesMIME reports whether an attachment of this type can hold a file of
// that content type. A document carries anything; the others would be used as
// what they claim to be.
func typeCarriesMIME(attachmentType, mediaType string) bool {
family := mediaType
if slash := strings.IndexByte(family, '/'); slash >= 0 {
family = family[:slash]
}
switch attachmentType {
case MediaTypeImage:
return family == "image"
case MediaTypeVideo:
return family == "video"
case MediaTypeAudio, MediaTypeVoice:
// Ogg is served as application/ogg, and the voice notes browsers and
// platforms record are Ogg, WebM or (Safari) mp4.
return family == "audio" || mediaType == "application/ogg"
default:
return true
}
}
func int64Of(p *int64) int64 {
if p == nil {
return 0
}
return *p
}
// ValidateAuthoredInbound is ValidateInbound for an attachment the caller
// described itself, as a browser upload is: its content type is a claim, not an
// observation, so it must be stated. A platform adapter uses ValidateInbound,
// where an unknown content type is not a lie.
func ValidateAuthoredInbound(att MediaAttachment) error {
if att.MIMEType == "" {
return fmt.Errorf("media: %s: no content type", att.Type)
}
return ValidateInbound(att)
}