Files
summercms/modules/album/album.go

131 lines
4.2 KiB
Go

// Package album provides the core Media Library for SummerCMS: Postgres-indexed
// media metadata and virtual folders, immutable SEO-slug blob keys under
// media/<short-id>/<slug>.<ext>, synchronous WebP responsive srcset variants,
// single-use DB-backed upload tokens, and zero-DB-hit static delivery.
package album
import (
"fmt"
"strings"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/compass"
)
// Configuration keys read from compass.Config.
const (
// ConfigMaxBytes sets the maximum binary upload size in bytes (default 25 MiB).
ConfigMaxBytes = "media.max_bytes"
// ConfigBase64MaxBytes sets the maximum decoded base64 upload size in bytes (default 5 MiB).
ConfigBase64MaxBytes = "media.base64_max_bytes"
// ConfigPublicPathPrefix sets the public URL path prefix for /media serving (default "/media").
ConfigPublicPathPrefix = "media.public_path_prefix"
// ConfigServe toggles mounting the public /media static route (defaults to storage.uploads.serve).
ConfigServe = "media.serve"
)
// Default limits and paths.
const (
// DefaultMaxBytes is the default maximum binary upload size (25 MiB).
DefaultMaxBytes int64 = 25 << 20
// DefaultBase64MaxBytes is the default maximum decoded base64 upload size (5 MiB).
DefaultBase64MaxBytes int64 = 5 << 20
// DefaultPublicPathPrefix is the default public URL path prefix for media items.
DefaultPublicPathPrefix = "/media"
// DefaultUploadTokenTTL is the default lifetime of a single-use upload token.
DefaultUploadTokenTTL = 5 * time.Minute
// BlobKeyPrefix is the leading bucket key segment for all album blobs.
BlobKeyPrefix = "media"
)
// Database table names owned by album.
const (
// FoldersTable stores virtual media folders.
FoldersTable = "summer_media_folders"
// ItemsTable stores uploaded media item metadata and variant records.
ItemsTable = "summer_media_items"
// UploadTokensTable stores hashed single-use signed upload tokens.
UploadTokensTable = "summer_media_upload_tokens"
)
// Install idempotently publishes a *Service onto app and returns it.
// If a *Service is already published on app, that instance is returned.
func Install(app *backpack.App) (*Service, error) {
if app == nil {
return nil, fmt.Errorf("album: app is nil")
}
if svc, ok := ServiceFrom(app); ok {
return svc, nil
}
svc := NewService(app.Config)
if err := app.Publish[*Service](svc); err != nil {
// Handle concurrent publish gracefully by returning the existing service.
if existing, ok := ServiceFrom(app); ok {
return existing, nil
}
return nil, fmt.Errorf("album: publish service: %w", err)
}
return svc, nil
}
// ServiceFrom returns the *Service published on app, if any.
func ServiceFrom(app *backpack.App) (*Service, bool) {
if app == nil {
return nil, false
}
svc, ok := app.Lookup[*Service]()
if !ok || svc == nil {
return nil, false
}
return svc, true
}
// MaxBytes returns the configured maximum binary upload size, defaulting to DefaultMaxBytes.
func MaxBytes(cfg *compass.Config) int64 {
if cfg != nil && cfg.Has(ConfigMaxBytes) {
if n := int64(cfg.Int(ConfigMaxBytes)); n > 0 {
return n
}
}
return DefaultMaxBytes
}
// Base64MaxBytes returns the configured maximum decoded base64 upload size,
// defaulting to DefaultBase64MaxBytes.
func Base64MaxBytes(cfg *compass.Config) int64 {
if cfg != nil && cfg.Has(ConfigBase64MaxBytes) {
if n := int64(cfg.Int(ConfigBase64MaxBytes)); n > 0 {
return n
}
}
return DefaultBase64MaxBytes
}
// PublicPathPrefix returns the configured public URL path prefix for media,
// trimmed of any trailing slash, defaulting to DefaultPublicPathPrefix.
func PublicPathPrefix(cfg *compass.Config) string {
if cfg != nil {
if raw := strings.TrimSpace(cfg.String(ConfigPublicPathPrefix)); raw != "" {
trimmed := strings.TrimRight(raw, "/")
if trimmed != "" {
return trimmed
}
}
}
return DefaultPublicPathPrefix
}
// ServeEnabled reports whether the application should mount the public /media
// static handler. When media.serve is explicitly set it wins; otherwise it
// follows storage.uploads.serve.
func ServeEnabled(cfg *compass.Config) bool {
if cfg == nil {
return false
}
if cfg.Has(ConfigServe) {
return cfg.Bool(ConfigServe)
}
return cfg.Bool("storage.uploads.serve")
}