media

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 9 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 the MIME type.

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 {
	MIME     string       `json:"mime"`
	Source   Source       `json:"source"`
	ID       string       `json:"id,omitempty"`
	Name     string       `json:"name,omitempty"`
	Metadata metadata.Map `json:"metadata,omitzero"`
}

Media describes a media payload without retaining runtime-only objects. Inline-byte construction snapshots the caller's buffer so the protocol value cannot change when that buffer is reused.

func NewBytes

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

NewBytes owns the payload: it copies the input so a caller reusing its buffer cannot alter media already attached to a message. Use it when the content must travel with the request.

func NewReference

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

NewReference names content already held by the provider, such as an uploaded file handle. The reference is opaque to Core, so it stays valid only with the provider that issued it.

func NewURI

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

NewURI delegates content resolution instead of copying the payload into the protocol value. The URI must therefore be reachable by the provider or client that resolves it.

func (*Media) Bytes

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

func (*Media) Clone

func (m *Media) Clone() *Media

func (Media) MarshalJSON

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

func (*Media) Reference

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

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

SourceKind identifies which Source value is active.

const (
	// SourceBytes carries an inline byte payload.
	SourceBytes SourceKind = "bytes"
	// SourceURI carries an absolute URI resolved by a provider or client.
	SourceURI SourceKind = "uri"
	// SourceReference carries a provider-native media reference.
	SourceReference SourceKind = "reference"
)

Jump to

Keyboard shortcuts

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