package lazymedia

import "golazy.dev/lazymedia"

Package lazymedia manages generated representations of stored files.

A media variant is a derived file, not the original upload. Examples include thumbnails, Open Graph images, compressed previews, transcript sidecars, or a browser-friendly conversion of an uploaded document. The source file and the generated output are both regular files identified by file IDs; lazymedia only records that source file X has variant key Y whose output file is Z.

The package deliberately does not own byte storage or the durable catalog for original files. Media uses FileStore to open a source file, save a generated output file, and return a URL for that output. Applications commonly satisfy FileStore with golazy.dev/lazyfiles, which in turn stores object bytes through golazy.dev/lazystorage backends such as the local filesystem, S3-compatible storage, or golazy.dev/pg/pgstorage. The lazymedia.Repository stores only the variant relationship and status; PostgreSQL applications can use golazy.dev/pg/pgmedia for that repository while using golazy.dev/pg/pgfiles for file metadata.

Media.Variant first asks Repository for an existing ready variant. If one exists, Media opens the output file through FileStore and returns its file metadata without running the processor again. If no ready variant exists, or Regenerate is passed, Media opens the source file, calls Processor.Process, stores the returned body through FileStore.Put, and saves a ready Variant that points back to the new output file ID. Media.URL builds on Variant and then delegates URL generation to FileStore.URL.

VariantKey is the stable name of the requested representation, such as "thumb", "preview", or "og". Spec carries opaque application-defined JSON that a processor can use for dimensions, codecs, quality settings, or any other generation input. lazymedia stores the spec on the Variant record but does not interpret it.

Options follow the same pass-through convention used by lazystorage and lazyfiles: each layer consumes the options it understands and returns the rest. lazymedia consumes VariantKey, Spec, and Regenerate; generated file metadata is forwarded to FileStore.Put, including lazystorage.ContentType from Result.ContentType and OutputFilename from Result.Filename.

lazymedia is useful outside a GoLazy app. A command-line importer can combine Media with a small JSONL repository and a filesystem-backed file service to generate previews locally, while a server can swap in lazyfiles plus PostgreSQL repositories without changing processors.

The append-only JSONL repository implementation lives in the golazy.dev/lazymedia/jsonl subpackage. It is intended for local tools, tests, and simple single-process applications. Shared production deployments should use a database-backed Repository such as golazy.dev/pg/pgmedia.

Constants

Types

type File

File is the minimal file metadata lazymedia needs from a file service.

type File struct {
	ID		string
	Filename	string
	ContentType	string
	Size		int64
	Metadata	json.RawMessage
}

type FileStore

FileStore is the minimal file service lazymedia needs.

type FileStore interface {
	Open(context.Context, string, ...any) (io.ReadCloser, File, []any, error)
	Put(context.Context, io.Reader, ...any) (File, []any, error)
	URL(context.Context, string, ...any) (string, []any, error)
}

type Media

Media resolves and generates file representations.

type Media struct {
	Files		FileStore
	Repository	Repository
	Processor	Processor
}
func (m *Media) URL

URL returns the URL for a ready or generated variant.

func (m *Media) URL(ctx context.Context, request Request, options ...any) (string, []any, error)
func (m *Media) Variant

Variant returns a ready variant, generating it when missing or requested.

func (m *Media) Variant(ctx context.Context, request Request, options ...any) (File, []any, error)

type Repository

Repository stores variant relationships.

type Repository interface {
	FindVariant(context.Context, string, string, ...any) (Variant, []any, error)
	SaveVariant(context.Context, Variant, ...any) (Variant, []any, error)
	DeleteVariant(context.Context, string, string, ...any) ([]any, error)
}

type Result

Result is the generated file body and metadata returned by processors.

type Result struct {
	Body		io.Reader
	ContentType	string
	Filename	string
	Options		[]any
}

type Spec

Spec supplies opaque app-defined JSON for the requested representation.

type Spec struct {
	JSON json.RawMessage
}

type Variant

Variant identifies a generated representation of a source file.

type Variant struct {
	SourceFileID	string		`json:"source_file_id"`
	VariantKey	string		`json:"variant_key"`
	Spec		json.RawMessage	`json:"spec,omitempty"`
	OutputFileID	string		`json:"output_file_id,omitempty"`
	Status		string		`json:"status,omitempty"`
	Error		string		`json:"error,omitempty"`
	CreatedAt	time.Time	`json:"created_at,omitempty"`
	UpdatedAt	time.Time	`json:"updated_at,omitempty"`
}

Directories

Path Synopsis
lazymedia/jsonl Package jsonl stores lazymedia repository records in an append-only JSONL file.