media

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 35 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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. Images that come from elsewhere (posts imported from another platform) go through Prepare, which also takes WebP and stores it as JPEG, or PNG when it has transparency: WebP doesn't display in Outlook.

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 WebP 8 bytes a pixel (a lossless one is decoded as 32-bit words, then copied to the image it returns), 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. WebP is refused: its callers store the bytes as they are.

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,image/webp"`
}

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 Prepared

type Prepared struct {
	Data          []byte
	ContentType   string
	Width, Height int
	HasAlpha      bool
	// Sha256 is the file's own: the same file found again is the same asset.
	Sha256 [32]byte
	// Original is what came in, when what's stored differs from it.
	Original *MediaOriginal
}

Prepared is an image ready for the library: checked by its content, resized and compressed for email, its metadata removed.

func Prepare

func Prepare(ctx context.Context, data []byte) (Prepared, error)

Prepare readies an image that comes from elsewhere (an imported post's) for the library, as Upload readies an upload, without its audit entry: up to MaxUploadBytes in, MaxBytes stored. WebP is accepted and stored as JPEG, or PNG when it has transparency. Its ImageError reads in English.

func (Prepared) Params

func (p Prepared) Params(ws uuid.UUID, fileName string, by *uuid.UUID, at time.Time) store.InsertMediaAssetParams

Params are the library row of a prepared image: its file name made safe, who added it and when.

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) Hidden

func (s *Service) Hidden(ctx context.Context, q *store.Queries, ws *store.Workspace, fileName string, data []byte) (MediaAsset, error)

Hidden keeps an image someone outside the team sent (a sponsor's logo or image with a self-serve request, M65): checked and stored the way uploads are (resized for email, its metadata removed), served at its /media/ address, and never shown in the library. It's no member's upload, so nothing is audited; the caller records what it's for. A refused image's error is an ImageError in the reader's language.

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