Documentation
¶
Overview ¶
Package media is a media library for togo — upload files to the storage plugin, attach them to any model, and generate image conversions (thumbnails).
It is the togo answer to Spatie Media Library / Rails Active Storage: blobs live in the storage plugin (k.Storage); metadata + conversions are recorded so you can attach media to a model by (model_type, model_id, collection) and fetch it back with derived sizes.
m, _ := media.FromKernel(k) rec, _ := m.Attach(ctx, "User", userID, "avatar", "me.png", bytes) url := m.URL(rec, "thumb")
Index ¶
- Variables
- type Conversion
- type Media
- type Service
- func (s *Service) Attach(ctx context.Context, modelType, modelID, collection, name string, data []byte) (*Media, error)
- func (s *Service) Bytes(m *Media, conversion string) ([]byte, error)
- func (s *Service) Delete(ctx context.Context, id string) error
- func (s *Service) Find(ctx context.Context, id string) (*Media, error)
- func (s *Service) Get(ctx context.Context, modelType, modelID, collection string) ([]*Media, error)
- func (s *Service) SetConversions(c []Conversion)
- func (s *Service) URL(m *Media, conversion string) string
- type Store
Constants ¶
This section is empty.
Variables ¶
var DefaultConversions = []Conversion{ {Name: "thumb", Width: 200, Height: 200, Fit: false}, {Name: "preview", Width: 1024, Height: 1024, Fit: true}, }
DefaultConversions are generated for image uploads unless overridden.
Functions ¶
This section is empty.
Types ¶
type Conversion ¶
type Conversion struct {
Name string
Width int
Height int
Fit bool // true = Fit (preserve aspect within WxH); false = Thumbnail (crop to fill)
}
Conversion is a named image transformation applied on attach.
type Media ¶
type Media struct {
ID string `json:"id"`
ModelType string `json:"model_type"`
ModelID string `json:"model_id"`
Collection string `json:"collection"`
Disk string `json:"disk"`
Path string `json:"path"`
Name string `json:"name"`
Mime string `json:"mime"`
Size int64 `json:"size"`
Conversions map[string]string `json:"conversions,omitempty"` // conversion name → storage path
Custom map[string]any `json:"custom,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Media is a stored file attached to a model.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the media runtime stored on the kernel (k.Get("media")).
func FromKernel ¶
FromKernel returns the media Service registered on the kernel.
func (*Service) Attach ¶
func (s *Service) Attach(ctx context.Context, modelType, modelID, collection, name string, data []byte) (*Media, error)
Attach stores file bytes in the storage plugin, records the media row against (modelType, modelID, collection), and generates image conversions when the upload is an image.
func (*Service) Delete ¶
Delete removes a media record and its stored blobs (original + conversions).
func (*Service) SetConversions ¶
func (s *Service) SetConversions(c []Conversion)
SetConversions overrides the image conversions generated on attach.
type Store ¶
type Store interface {
Save(ctx context.Context, m *Media) error
List(ctx context.Context, modelType, modelID, collection string) ([]*Media, error)
Get(ctx context.Context, id string) (*Media, error)
Delete(ctx context.Context, id string) error
}
Store persists media metadata. The plugin uses a SQL-backed store when a DB is reachable, and an in-memory store otherwise.