media

package
v0.62.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package media defines the serializable Media container shared by every modality that accepts non-text payloads.

Use NewBytes for owned inline content, NewURI for an absolute external URI, or NewReference for a provider-native identifier. Source is a tagged union: exactly one source must match its Kind. Constructors and JSON decoding enforce that invariant and validate any declared MIME type. Empty MIME retains an unknown type without guessing from the source. Protocols that require a declaration admit media through Media.RequireMIME.

Example
package main

import (
	"fmt"

	"github.com/Tangerg/scope/core/media"
)

func main() {
	attachment, err := media.NewBytes("image/png", []byte("scope"))
	if err != nil {
		panic(err)
	}
	attachment.ID = "image-1"
	attachment.Name = "scope.png"

	fmt.Println(attachment.Source.Kind, len(attachment.Source.Bytes), attachment.Name)
}
Output:
bytes 5 scope.png

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilMedia      = errors.New("media: nil media")
	ErrInvalidMIME   = errors.New("media: invalid MIME type")
	ErrInvalidSource = errors.New("media: invalid source")
)

Functions

This section is empty.

Types

type Media

type Media struct {
	// Empty means the source did not declare a media type. Adapters that require
	// one must use RequireMIME instead of inventing a declaration from a filename.
	MIME     string       `json:"mime,omitzero"`
	Source   Source       `json:"source"`
	ID       string       `json:"id,omitempty"`
	Name     string       `json:"name,omitempty"`
	Metadata metadata.Map `json:"metadata,omitzero"`
}

Inline-byte construction snapshots the caller's buffer.

func NewBytes

func NewBytes(mimeType string, data []byte) (*Media, error)

NewBytes copies the payload so later caller mutation cannot change the media.

func NewReference

func NewReference(mimeType, reference string) (*Media, error)

NewReference retains an opaque handle valid only with the provider that issued it.

func NewURI

func NewURI(mimeType, uri string) (*Media, error)

NewURI leaves content resolution to the provider or client; the URI must be reachable by it.

func (*Media) Bytes

func (m *Media) Bytes() ([]byte, error)

func (*Media) Clone

func (m *Media) Clone() *Media

func (*Media) DataURI added in v0.62.0

func (m *Media) DataURI() (string, error)

DataURI encodes inline bytes as an RFC 2397 base64 data URI, the form providers accept for inline media where their wire expects a URL. It carries the Type alone: RFC 2397 parameters admit no whitespace, which a MIME declaration may contain.

func (Media) JSONSchemaModel added in v0.57.0

func (Media) JSONSchemaModel() any

func (Media) MarshalJSON

func (m Media) MarshalJSON() ([]byte, error)

func (*Media) Reference

func (m *Media) Reference() (string, error)

func (*Media) RequireMIME added in v0.60.0

func (m *Media) RequireMIME() error

func (*Media) Type added in v0.62.0

func (m *Media) Type() Type

Type is the declared Type, empty when MIME declares none. An invalid declaration is empty as well; Validate reports it.

func (*Media) URI

func (m *Media) URI() (string, error)

func (*Media) UnmarshalJSON

func (m *Media) UnmarshalJSON(data []byte) error

func (*Media) Validate

func (m *Media) Validate() error

type Source

type Source struct {
	Kind  SourceKind `json:"kind"`
	Bytes []byte     `json:"bytes,omitempty"`
	URI   string     `json:"uri,omitempty"`
	Ref   string     `json:"ref,omitempty"`
}

Source is a tagged union. Kind selects exactly one of Bytes, URI, or Ref; all other fields must be empty.

func (Source) Validate

func (s Source) Validate() error

type SourceKind

type SourceKind string
const (
	SourceBytes     SourceKind = "bytes"
	SourceURI       SourceKind = "uri"
	SourceReference SourceKind = "reference"
)

type TopLevelType added in v0.62.0

type TopLevelType string

TopLevelType is the registered class before the slash in a Type, such as image; one wire form often accepts a whole class.

const (
	TopLevelText        TopLevelType = "text"
	TopLevelImage       TopLevelType = "image"
	TopLevelAudio       TopLevelType = "audio"
	TopLevelVideo       TopLevelType = "video"
	TopLevelApplication TopLevelType = "application"
)

func (TopLevelType) Type added in v0.62.0

func (t TopLevelType) Type(subtype string) Type

Type names subtype within this class, the inverse of Type.Subtype for an adapter whose wire spells only the subtype.

type Type added in v0.62.0

type Type string

Type is a media type without parameters, lowercased as mime.ParseMediaType returns it, such as image/png. Adapters classify and compare media by it instead of parsing a MIME declaration again.

func ParseType added in v0.62.0

func ParseType(declaration string) (Type, error)

ParseType reads the Type of a MIME declaration and discards its parameters.

func (Type) Subtype added in v0.62.0

func (t Type) Subtype() string

func (Type) TopLevel added in v0.62.0

func (t Type) TopLevel() TopLevelType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL