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
- func Absolutize(html, publicURL string) string
- func DecodeCost(data []byte) (cost int64, width, height int, ok bool)
- func DecodeSlot(ctx context.Context) (release func(), err error)
- func Inspect(data []byte) (contentType string, width, height int, alpha bool, err error)
- func Register(h huma.API, s *Service)
- func SafeFileName(name, contentType string) string
- func URL(id uuid.UUID, fileName string) string
- type ImageError
- type MediaAsset
- type MediaAssetList
- type MediaOriginal
- type Service
- func (s *Service) List(ctx context.Context, ws *store.Workspace, limit int) ([]MediaAsset, error)
- func (s *Service) Remove(ctx context.Context, ws *store.Workspace, id uuid.UUID) error
- func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Service) Upload(ctx context.Context, ws *store.Workspace, actor *uuid.UUID, fileName string, ...) (MediaAsset, error)
Constants ¶
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 )
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.
const MaxPixels = 50_000_000
MaxPixels guards against decompression bombs (50 megapixels, a phone's full-resolution photo).
const PathPrefix = "/media/"
PathPrefix is where images are served.
Variables ¶
This section is empty.
Functions ¶
func Absolutize ¶
Absolutize rewrites root-relative /media/ URLs in HTML to the public URL, for anything that leaves ReputeCast (sends, tests, exported HTML).
func DecodeCost ¶
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 ¶
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 ¶
Inspect validates image bytes by their content and returns the type, dimensions and whether the image has transparency. Its ImageError reads in English.
func SafeFileName ¶
SafeFileName keeps a readable, URL-safe file name with the extension the content type implies.
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) Remove ¶
Remove hides an image from the library. It stays reachable so emails that were already sent keep showing it.