Help us shape the future of Go! Take the 2026 Go Developer Survey

avatars

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package avatars owns avatar resolution for shithub user profiles. S09 ships the deterministic SVG identicon (rendered server-side, no upload needed) plus the routing skeleton for serving uploaded avatars from object storage. Real upload (resize / EXIF-strip) lands in S10.

Index

Constants

View Source
const IdenticonSize = 5

IdenticonSize is the SVG viewBox side length. The image is square and renders at any CSS px size via width/height attributes.

View Source
const MaxPixelArea = 24 * 1000 * 1000

MaxPixelArea bounds the *decoded* image's pixel area as a defense-in-depth check against decompression-bomb attacks: a small file (well under MaxUploadBytes) can decode to a huge image. We reject anything past 24 megapixels (e.g. 4900×4900), well above any reasonable avatar source.

View Source
const MaxUploadBytes = 5 * 1024 * 1024 // 5 MiB

MaxUploadBytes caps how big an uploaded avatar may be before decoding. Stops trivially large uploads from soaking RAM during decode.

Variables

View Source
var (
	ErrTooLarge      = errors.New("avatars: upload exceeds size limit")
	ErrUnsupported   = errors.New("avatars: unsupported image format")
	ErrDecompression = errors.New("avatars: image dimensions exceed limit")
	ErrDecode        = errors.New("avatars: could not decode image")
)

Errors surfaced to the handler. Each maps to a friendly UI message.

View Source
var VariantSizes = []int{460, 200, 40}

VariantSizes is the set of resize targets we generate per upload. The largest is what the public avatar route serves; smaller ones are kept so a future "/avatars/:user/:size" route can serve them without re-resizing.

Functions

func Identicon

func Identicon(username string, pixelSize int) string

Identicon returns an inline SVG identicon for username. Same input → same output (deterministic). The SVG carries width/height attributes so it can be embedded as inline markup OR served as a standalone file.

The pattern is a 5×5 grid mirrored horizontally — left two columns are mirrored to the right, the middle column is independent. Each cell is colored if the corresponding bit in the digest is 1.

Types

type Variant

type Variant struct {
	Size int    // edge length in pixels (Size × Size)
	Data []byte // PNG bytes
}

Variant is one rendered output: a sized PNG ready for upload to the object store.

func Process

func Process(r io.Reader) ([]Variant, string, error)

Process reads an uploaded image, validates it, and produces resized PNG variants. It strips EXIF as a side effect of re-encoding.

Returns the variants in VariantSizes order plus a content-addressed key component (sha256 of the largest variant's bytes) the caller can embed in the storage path.

Jump to

Keyboard shortcuts

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