media

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 34 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), resized and compressed for email with their metadata removed (optimize.go), 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 (
	// MaxWidth is the widest image stored: twice the 600 px email column,
	// so images stay sharp on high-density screens.
	MaxWidth = 1200
	// MaxUploadBytes is the largest file accepted for upload (20 MB, a
	// phone photo). What is stored is resized and compressed and must fit
	// MaxBytes.
	MaxUploadBytes = 20 << 20

	// DecodeBudget is the most memory decoding one image may take, with the
	// copies made to resize it (decode.go): larger uploads are only
	// stripped of their metadata.
	DecodeBudget = 256 << 20
)
View Source
const MaxBytes = 5 << 20

MaxBytes is the largest image stored (5 MB), and the largest GIF accepted, since GIFs are kept as uploaded. Email images should be far smaller; Gmail and Outlook slow down above a few hundred KB.

View Source
const MaxPixels = 50_000_000

MaxPixels guards against decompression bombs (50 megapixels, a phone's full-resolution photo).

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 DecodeCost

func DecodeCost(data []byte) (cost int64, width, height int, ok bool)

DecodeCost is about the most memory decoding an image takes, in bytes, and its size in pixels, read from its header: for a PNG its pixels (8 bytes each at 16 bits, 4 otherwise, and twice that for an interlaced one, decoded pass by pass next to the whole image), for a GIF its first frame (a byte a pixel, within the screen), for a JPEG what image/jpeg allocates for its frame (jpegFrame.memory). ok is false when the header isn't one a decoder would read, or the image is empty.

func DecodeSlot

func DecodeSlot(ctx context.Context) (release func(), err error)

DecodeSlot waits for the slot images are decoded in, one at a time across ReputeCast, and returns the function that frees it. err is ctx's when it ends first.

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. Its ImageError reads in English.

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"`
	// Original is the file as uploaded, when what's stored differs from it.
	Original *MediaOriginal `json:"original,omitempty" doc:"The file as uploaded, when ReputeCast resized or compressed it or removed its metadata"`
}

MediaAsset is one image in the library.

type MediaAssetList

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

MediaAssetList is the image library.

type MediaOriginal

type MediaOriginal struct {
	SizeBytes   int    `json:"sizeBytes"`
	Width       int    `json:"width"`
	Height      int    `json:"height"`
	ContentType string `json:"contentType" enum:"image/png,image/jpeg,image/gif"`
}

MediaOriginal is what was uploaded, kept as numbers only (the file itself isn't kept: its metadata can hold where a photo was taken).

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), resized and compressed for email with its metadata removed.

Jump to

Keyboard shortcuts

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