media

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxAssetBytes is the global max accepted payload size.
	MaxAssetBytes int64 = 200 * 1024 * 1024
)

Variables

View Source
var (
	// ErrAssetNotFound indicates the requested media asset does not exist.
	ErrAssetNotFound = errors.New("media asset not found")
	// ErrProviderUnavailable indicates the storage provider is not configured or reachable.
	ErrProviderUnavailable = errors.New("storage provider unavailable")
	// ErrAssetTooLarge indicates the payload exceeds the configured max asset size.
	ErrAssetTooLarge = errors.New("media asset too large")
	// ErrPathTraversal indicates a storage key attempted directory traversal.
	ErrPathTraversal = errors.New("path traversal is forbidden")
)

Functions

func MimeFromPath

func MimeFromPath(filePath string) string

MimeFromPath derives MIME from an already-persisted filename or storage key. It is deliberately metadata-only: history reads can use it without opening workspace storage or waking a runtime provider.

func ReadAllWithLimit

func ReadAllWithLimit(reader io.Reader, maxBytes int64) ([]byte, error)

ReadAllWithLimit reads from reader and rejects payloads larger than maxBytes.

Types

type Asset

type Asset struct {
	ContentHash string `json:"content_hash"`
	BotID       string `json:"bot_id"`
	Mime        string `json:"mime"`
	SizeBytes   int64  `json:"size_bytes"`
	StorageKey  string `json:"storage_key"`
}

Asset is the domain representation of a persisted media object. ContentHash is the content-addressed identifier (SHA-256 hex).

type IngestInput

type IngestInput struct {
	BotID string
	Mime  string
	// Reader provides the raw bytes; caller is responsible for closing.
	Reader io.Reader
	// MaxBytes optionally overrides the default size limit.
	MaxBytes int64
	// OriginalExt preserves the source file extension (e.g. ".md") so it
	// survives even when the MIME type is unknown / generic.
	OriginalExt string
}

IngestInput carries the data needed to persist a new media asset.

type MediaType

type MediaType string

MediaType classifies the kind of media asset.

const (
	MediaTypeImage MediaType = "image"
	MediaTypeAudio MediaType = "audio"
	MediaTypeVideo MediaType = "video"
	MediaTypeFile  MediaType = "file"
)

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides content-addressed media asset persistence. All metadata is derived from the filesystem — no database, no sidecar files.

func NewService

func NewService(log *slog.Logger, provider storage.Provider) *Service

NewService creates a media service with the given storage provider.

func (*Service) AccessPath

func (s *Service) AccessPath(ctx context.Context, asset Asset) string

AccessPath returns a reachable consumer reference for a persisted asset. Providers that support materialization may promote spill storage into their consumer-addressable primary store. Errors are intentionally collapsed for legacy callers; new routing code should use EnsureAccessPath.

func (*Service) EnsureAccessPath

func (s *Service) EnsureAccessPath(ctx context.Context, asset Asset) (string, error)

EnsureAccessPath returns a consumer-visible path, materializing the asset into addressable storage when the provider supports it.

func (*Service) GetByStorageKey

func (s *Service) GetByStorageKey(ctx context.Context, botID, storageKey string) (Asset, error)

GetByStorageKey returns an asset derived from a known storage key.

func (*Service) Ingest

func (s *Service) Ingest(ctx context.Context, input IngestInput) (Asset, error)

Ingest persists a new media asset. It hashes the content, deduplicates by checking the filesystem, and stores the bytes. Returns a derived Asset.

func (*Service) IngestContainerFile

func (s *Service) IngestContainerFile(ctx context.Context, botID, containerPath string) (Asset, error)

IngestContainerFile reads an arbitrary file from a bot's /data/ directory and ingests it into the media store. The provider must implement ContainerFileOpener.

func (*Service) IngestWorkspaceFile added in v1.0.0

func (s *Service) IngestWorkspaceFile(ctx context.Context, botID, containerPath string) (Asset, error)

IngestWorkspaceFile archives a file below /data that a model referenced in Markdown. It reads the file the same way the workspace file manager does.

func (*Service) Open

func (s *Service) Open(ctx context.Context, botID, contentHash string) (io.ReadCloser, Asset, error)

Open returns a reader for the media asset identified by content hash. It locates the file by scanning extensions under the hash prefix and derives MIME from the extension.

func (*Service) Resolve

func (s *Service) Resolve(ctx context.Context, botID, contentHash string) (Asset, error)

Resolve finds an asset by content hash (no stream open). Used to fill mime/storage_key when DB has none.

func (*Service) Stat

func (s *Service) Stat(ctx context.Context, botID, contentHash string) (Asset, error)

Stat returns asset metadata for the given content hash without opening the file. It satisfies the channel.OutboundAttachmentStore interface.

Directories

Path Synopsis
Package lottie rasterises Lottie animations — Telegram animated stickers in particular — without cgo, a native library or a helper process.
Package lottie rasterises Lottie animations — Telegram animated stickers in particular — without cgo, a native library or a helper process.

Jump to

Keyboard shortcuts

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