Documentation
¶
Overview ¶
Package branding is the instance's logo and display name: what an admin uploads on the Account page (docs/branding.md), the images muster serves from /brand/, and the row that keeps them (migration 0105, instance_branding).
This file is the image half: Process turns an upload into the three PNGs muster serves, or refuses it with one of the errors below.
🔴 THE FORMAT IS DECIDED BY THE BYTES, NEVER BY A NAME. The filename and the part's Content-Type are the uploader's claims and are not read at all. The first bytes must be a PNG, JPEG or WebP signature, and each is decoded by ITS OWN decoder (png, jpeg, webp) — never image.Decode, whose registry is whatever the binary happens to import, so a GIF or BMP decoder linked in by some other package would widen what is accepted without a line changing here.
🔴 THE SIZE IS CHECKED BEFORE ANYTHING IS DECODED. A 1 MiB file can declare a 65535×65535 image whose pixels compress to nothing; decoding it would ask for 16 GiB. So the header is read first (DecodeConfig, which allocates no pixels), and the width, height and pixel count are refused there.
🔴 WHAT IS STORED IS RE-ENCODED, NEVER THE UPLOAD. The decoded pixels are drawn onto a fresh canvas and written as a new PNG, so nothing of the file but its pixels survives: no EXIF, no text chunks, no ICC profile, no bytes appended after the image (a PNG/HTML polyglot loses its HTML).
Index ¶
Constants ¶
const ( // MaxUploadBytes is the largest upload accepted: 1 MiB. MaxUploadBytes = 1 << 20 // MaxSide is the widest and tallest image accepted, in pixels. MaxSide = 4096 // MaxPixels is the most pixels accepted (width × height): 2048×2048. MaxPixels = 2048 * 2048 // LogoMaxSide is the longest side of the stored logo: a larger upload is // scaled down to fit (never up). The header draws it about 28px tall; this // keeps a crisp copy for high-density screens without storing megabytes. LogoMaxSide = 1024 // Icon192 and Icon512 are the square icons' sides. Icon192 = 192 Icon512 = 512 )
The limits Process enforces.
🔴 MaxPixels IS THE MEMORY BOUND, AND IT IS SET BY MEASUREMENT. Decoding and scaling cost memory per pixel, whatever the file's size: at the old cap of 16,000,000 pixels a progressive CMYK JPEG of a few hundred KB peaked at 560-580 MB RSS, past the server's 512 MiB limit. At 2048×2048 (4,194,304) the worst accepted input measured ~119 MB peak RSS (the figures are on processMu). A logo is stored at most LogoMaxSide (1024) on a side, so this still takes a 2× source.
⚠ MaxSide STAYS ABOVE √MaxPixels SO THE PIXEL GUARD IS REACHABLE. Were the sides capped at 2048 too, no image could exceed MaxPixels and the guard would never run. At 4096 a wide 4096×1024 banner is accepted, and a 4096×1025 or 2049×2048 image passes the side check and is refused here (TestThePixelCapIs2048Squared).
const MaxNameRunes = 64
MaxNameRunes is the longest display name, in characters (Unicode code points — what Postgres's char_length counts, so the column's CHECK and this agree).
Variables ¶
var ( ErrEmpty = &Error{"empty", "the file is empty"} ErrTooLarge = &Error{"too_large", "the file is larger than 1 MiB"} ErrSVG = &Error{"svg_refused", "SVG is not accepted: upload a PNG, JPEG or WebP"} ErrUnsupported = &Error{"unsupported_type", "the file is not a PNG, JPEG or WebP image"} ErrCorrupt = &Error{"corrupt", "the image could not be read: it is truncated or damaged"} ErrDimensions = &Error{"too_wide", "the image is wider or taller than 4096 pixels"} ErrPixels = &Error{"too_many_pixels", "the image has more than 4,194,304 pixels (2048×2048)"} )
The refusals. Each is a distinct value, so a caller (and a test) can tell which guard refused: errors.Is(err, ErrSVG).
var ( ErrNameInvalid = &Error{"name_invalid", "the name is not valid UTF-8"} ErrNameTooLong = &Error{"name_too_long", "the name is longer than 64 characters"} ErrNameControl = &Error{"name_control", "the name contains a control or text-direction character"} )
The name's refusals.
var ErrAppIconNeedsLogo = &Error{"app_icon_needs_logo", "an app icon needs a logo: upload a logo first, or both together"}
ErrAppIconNeedsLogo refuses an app icon on a server with no logo: the app icon replaces the logo's icons, it is not a brand on its own.
var ErrEmptyChange = errors.New("branding: a change must set or clear something (and not both set and clear the logo or the app icon)")
ErrEmptyChange refuses a Change that changes nothing, and one that both sets and clears the logo or the app icon, or sets an app icon while clearing the logo.
var ErrNotAdmin = errors.New("branding: only an admin may change the instance's branding")
ErrNotAdmin refuses a write whose ctx is not an admin's (authz.IsAdmin).
Functions ¶
func NormalizeName ¶
NormalizeName is raw as stored: trimmed of surrounding white space, or refused. An empty result (raw was empty or only white space) means "clear the name", and is not an error.
🔴 TEXT-DIRECTION CONTROLS ARE REFUSED WITH THE C0/C1 CONTROLS. The name is drawn beside the logo on every page and on the sign-in form, where a right-to-left override (U+202E) could make it read as something it is not.
Types ¶
type AppIcon ¶ added in v0.200.0
AppIcon is an accepted app-icon upload as muster stores it: the two square icons drawn from it, in place of the ones drawn from the logo. Nothing else of it is kept.
func ProcessIcon ¶ added in v0.200.0
ProcessIcon is Process for an app icon: the same checks in the same order (the same errors), the same decode onto a fresh canvas, and the same square icons — scaled to fit and centred on a transparent background, nothing cropped — drawn from the upload (scaled to LogoMaxSide first, as the logo is) instead of from the logo.
type Branding ¶
type Branding struct {
Name string
// Logo, Icon192 and Icon512 are nil, together, when no logo is set.
// Icon192 and Icon512 are the icons muster SERVES: the app icon's when one
// is set (HasAppIcon), the logo's otherwise.
Logo, Icon192, Icon512 []byte
// ETag is "" when no logo is set, else the ETagOf the logo and the served
// icons — so it changes when the app icon is set or removed, too.
ETag string
// HasAppIcon reports that the served icons were drawn from a separate app
// icon upload (ProcessIcon), not from the logo (migration 0114).
HasAppIcon bool
// LogoShowsName is the admin's statement that the logo already spells the
// name (a wordmark), so the pages draw the logo alone (migration 0114). It
// is kept when the logo is reset, and means nothing while no logo is set.
LogoShowsName bool
UpdatedAt time.Time
UpdatedBy int64
}
Branding is the instance's branding as stored. A zero value is "nothing set": no name and no logo.
type Change ¶
type Change struct {
// Name, when set, replaces the name; "" clears it. It must already be
// NormalizeName's output.
Name *string
// Logo, when set, replaces the logo and the icons drawn from it. An app
// icon already set stays, and is still what is served.
Logo *Images
// ClearLogo removes the logo, its icons and the app icon. It and Logo are
// exclusive, and so are it and AppIcon.
ClearLogo bool
// AppIcon, when set, replaces the served icons with these. It needs a
// logo: one already set, or Logo in the same change (ErrAppIconNeedsLogo).
AppIcon *AppIcon
// ClearAppIcon removes the app icon, so the logo's icons are served
// again. It and AppIcon are exclusive.
ClearAppIcon bool
// LogoShowsName, when set, replaces Branding.LogoShowsName.
LogoShowsName *bool
}
Change is one write. A nil field is left as it is.
type Error ¶
Error is one reason an upload or a name is refused. Code is stable (the API returns it, and a client may branch on it); Msg is for a person.
type Images ¶
type Images struct {
Logo []byte
Icon192 []byte
Icon512 []byte
// ETag is the first 16 hex digits of the sha256 over the three PNGs. It
// changes whenever any of them does, so it is the cache key everywhere:
// the /brand/ responses' ETag, the ?v= on every link to them, and the
// field the apps compare.
ETag string
}
Images is an accepted upload as muster stores and serves it: the logo and two square icons, each a PNG, and the ETag that names all three.
type PG ¶
type PG struct {
// contains filtered or unexported fields
}
PG is Store on instance_branding (migrations 0105, 0114).
func (*PG) Apply ¶
Apply implements Store: it reads the row FOR UPDATE, applies c in Go (Stored.Next), and writes the whole row back with the ETag of what it now serves (Stored.Resolve), in one transaction.
🔴 IT CHECKS THE CALLER IS AN ADMIN BEFORE IT ACTS AS THE SYSTEM. The write needs the system scope (row-level security admits nothing else), and a store that elevated any caller would make the route's admin check the only one; this keeps a handler wired without it from writing.
type Store ¶
type Store interface {
// Load is the branding; a zero Branding when nothing was ever set.
Load(ctx context.Context) (Branding, error)
// Apply writes c as the admin ctx names, and returns the result.
Apply(ctx context.Context, c Change) (Branding, error)
}
Store keeps the instance's branding.
type Stored ¶ added in v0.200.0
type Stored struct {
Name string
Logo, LogoIcon192, LogoIcon512 []byte
AppIcon192, AppIcon512 []byte
LogoShowsName bool
}
Stored is the row as written: the logo's own icons and the app icon's kept apart. Resolve is the Branding it serves.