feat(album): add core Media Library module, migrations, and surf routes

This commit is contained in:
Jakub Zych
2026-11-27 12:46:28 +01:00
parent 6b1dfec45e
commit cff3e34e10
17 changed files with 2583 additions and 1 deletions

View File

@@ -17,5 +17,5 @@ Start with [Installation](setup/installation.md) to set up the toolchain and the
- [Plugins](plugins/registration.md): registering a plugin, scheduling, extending other plugins, [menu item types](plugins/menu-item-types.md) and testing.
- [Backend](backend/admin-controllers.md): admin controllers, forms, lists, relations, [plugin field types](backend/field-types.md), users and [API tokens](backend/users-and-permissions.md#api-tokens), settings and the admin SPA.
- [Database](database/models.md): models, migrations, queries, relations, casts, attachments and transactions.
- [Services](services/configuration.md): configuration, events, routing, authentication, [caching](services/caching.md), mail, jobs, realtime, search and the other services, plus what SummerCMS does not provide for the frontend.
- [Services](services/configuration.md): configuration, events, routing, authentication, [caching](services/caching.md), mail, jobs, realtime, search, [media library](services/media-library.md) and the other services, plus what SummerCMS does not provide for the frontend.
- [Console](console/introduction.md): the `summer` tool, the application binary's commands and writing your own.

View File

@@ -0,0 +1,89 @@
---
title: Media Library
description: Store, organize and serve shared media with immutable SEO-slug URLs, synchronous WebP srcset variants and single-use signed upload tokens.
section: services
order: 95
---
# Media Library
The [album](../../modules/album/README.md) module provides a shared, plugin-agnostic Media Library backed by the application's `blob.Bucket` (see [Storage](storage.md)) 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](../../modules/surf/README.md) mounts `GET /media/{short_id}/{file}` directly on `album.StaticHandler`:
```go src=modules/album/example_test.go#ExampleStaticHandler
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`:
```go src=modules/album/example_test.go#ExampleItem_Srcset
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`:
```go src=modules/album/example_test.go#installAndLookup
// 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. |