4.3 KiB
title, description, section, order
| title | description | section | order |
|---|---|---|---|
| Media Library | Store, organize and serve shared media with immutable SEO-slug URLs, synchronous WebP srcset variants and single-use signed upload tokens. | services | 95 |
Media Library
The album module provides a shared, plugin-agnostic Media Library backed by the application's blob.Bucket (see Storage) and PostgreSQL metadata tables (summer_media_folders, summer_media_items and summer_media_upload_tokens).
URL contract and zero-DB-hit delivery
Uploaded files are written directly to the shared bucket at media/<short-id>/<slug>.<ext>, where <short-id> is a server-generated 12-character lowercase alphanumeric identifier and <slug> is sanitized from the filename and frozen at upload time. Moving an item between virtual folders or updating its alt, title or caption never changes its blob key or public URL.
When media.serve is enabled (or unset while storage.uploads.serve is true), surf mounts GET /media/{short_id}/{file} directly on album.StaticHandler:
ctx := context.Background()
bucket := memblob.OpenBucket(nil)
defer bucket.Close()
key := "media/abc123456789/summer-banner.png"
_ = bucket.WriteAll(ctx, key, []byte("png-bytes"), attach.UploadWriterOptions("image/png"))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/media/abc123456789/summer-banner.png", nil)
album.StaticHandler(bucket, "/media").ServeHTTP(rec, req)
fmt.Println(rec.Code)
fmt.Println(rec.Header().Get("Cache-Control"))
fmt.Println(rec.Header().Get("X-Content-Type-Options"))
// Output:
// 200
// public, max-age=31536000, immutable
// nosniff
Every response sends Cache-Control: public, max-age=31536000, immutable, X-Content-Type-Options: nosniff and a sandboxing Content-Security-Policy. Non-inline types (such as PDFs or archives) always carry Content-Disposition: attachment. SVG, HTML and JavaScript uploads are rejected.
Synchronous WebP variants
Raster image uploads (jpg, jpeg, png, gif, webp) pass attach.IsAllowedImage (including the attach.MaxImagePixels decompression-bomb guard) and synchronously generate WebP width variants at 480w, 960w and 1600w without upscaling past the original width. Variants are stored alongside the original at media/<short-id>/<slug>-<width>w.webp and recorded on summer_media_items.variants:
item := album.Item{
ShortID: "abc123456789",
BlobKey: "media/abc123456789/hero.jpg",
Variants: album.Variants{
{Width: 480, Height: 320, Key: "media/abc123456789/hero-480w.webp"},
{Width: 960, Height: 640, Key: "media/abc123456789/hero-960w.webp"},
},
}
fmt.Println(item.PublicURL(nil))
fmt.Println(item.Srcset(nil))
// Output:
// /media/abc123456789/hero.jpg
// /media/abc123456789/hero-480w.webp 480w, /media/abc123456789/hero-960w.webp 960w
Accessing the service
surf.BuildRouter publishes *album.Service on backpack.App via album.Install. Plugins and admin handlers look it up with album.ServiceFrom:
// installAndLookup shows how application or plugin code obtains the published *album.Service.
func installAndLookup(app *backpack.App) (*album.Service, error) {
svc, err := album.Install(app)
if err != nil {
return nil, err
}
if found, ok := album.ServiceFrom(app); ok {
return found, nil
}
return svc, nil
}
Single-use signed uploads
For binary uploads from CLIs or MCP agents, album.Service.PrepareUpload creates a 32-byte random token, stores only its SHA-256 hash in summer_media_upload_tokens with a 5-minute TTL, and returns the opaque token. The raw PUT / POST /media/upload/{token} route (album.SignedUploadHandler) atomically marks used_at = NOW() before reading bytes, preventing replay.
Configuration
| Key | Default | Controls |
|---|---|---|
media.max_bytes |
26214400 (25 MiB) |
Maximum binary upload size. |
media.base64_max_bytes |
5242880 (5 MiB) |
Maximum decoded size for base64 uploads. |
media.public_path_prefix |
"/media" |
Public URL prefix for media serving and signed upload routes. |
media.serve |
follows storage.uploads.serve |
Mounts the zero-DB-hit GET /media/{short_id}/{file} route. |