media

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package media is the workspace image library used by the composer's asset manager. Uploads are validated by their content (PNG, JPEG or GIF, decoded to read the dimensions), stored in Postgres and served publicly at /media/{id}/{file} with immutable caching, because email clients fetch them without a session. Content stores root-relative URLs; Absolutize turns them into public URLs when an email leaves ReputeCast.

Index

Constants

View Source
const MaxBytes = 5 << 20

MaxBytes is the largest image accepted (5 MB). Email images should be far smaller; Gmail and Outlook slow down above a few hundred KB.

View Source
const MaxPixels = 100_000_000

MaxPixels guards against decompression bombs (100 megapixels).

View Source
const PathPrefix = "/media/"

PathPrefix is where images are served.

Variables

This section is empty.

Functions

func Absolutize

func Absolutize(html, publicURL string) string

Absolutize rewrites root-relative /media/ URLs in HTML to the public URL, for anything that leaves ReputeCast (sends, tests, exported HTML).

func Inspect

func Inspect(data []byte) (contentType string, width, height int, alpha bool, err error)

Inspect validates image bytes by their content and returns the type, dimensions and whether the image has transparency.

func Register

func Register(h huma.API, s *Service)

Register adds the image library operations.

func SafeFileName

func SafeFileName(name, contentType string) string

SafeFileName keeps a readable, URL-safe file name with the extension the content type implies.

func URL

func URL(id uuid.UUID, fileName string) string

URL is the root-relative URL of an asset.

Types

type ImageError

type ImageError string

ImageError is why an upload was refused, worded for the person uploading.

func (ImageError) Error

func (e ImageError) Error() string

type MediaAsset

type MediaAsset struct {
	ID          string    `json:"id"`
	URL         string    `json:"url" doc:"Root-relative URL; emails get the absolute public URL"`
	FileName    string    `json:"fileName"`
	ContentType string    `json:"contentType" enum:"image/png,image/jpeg,image/gif"`
	SizeBytes   int       `json:"sizeBytes"`
	Width       int       `json:"width"`
	Height      int       `json:"height"`
	HasAlpha    bool      `json:"hasAlpha" doc:"Transparent pixels (logos survive dark mode)"`
	CreatedAt   time.Time `json:"createdAt"`
	CreatedBy   string    `json:"createdBy"`
}

MediaAsset is one image in the library.

type MediaAssetList

type MediaAssetList struct {
	Items []MediaAsset `json:"items"`
}

MediaAssetList is the image library.

type Service

type Service struct {
	Pool  *pgxpool.Pool
	Q     *store.Queries
	Audit *audit.Logger
	Now   func() time.Time
	// Static serves /media/starters/{file}: the placeholder images of the
	// built-in starter templates, shipped in the binary.
	Static fs.FS
	// contains filtered or unexported fields
}

Service holds the media domain dependencies.

func (*Service) List

func (s *Service) List(ctx context.Context, ws *store.Workspace, limit int) ([]MediaAsset, error)

List returns the library, newest first.

func (*Service) Remove

func (s *Service) Remove(ctx context.Context, ws *store.Workspace, id uuid.UUID) error

Remove hides an image from the library. It stays reachable so emails that were already sent keep showing it.

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves GET /media/{id}/{file}. Images are immutable (a new upload gets a new id), so they are cached for a year. The file name in the path is cosmetic; the id decides.

func (*Service) Upload

func (s *Service) Upload(ctx context.Context, ws *store.Workspace, actor *uuid.UUID, fileName string, data []byte) (MediaAsset, error)

Upload stores an image (or returns the identical one already stored).

Jump to

Keyboard shortcuts

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