Documentation
¶
Overview ¶
Package media stores uploaded files on a pluggable backend: the local filesystem (development, single replica) or any S3-compatible object store (AWS S3, MinIO, Cloudflare R2, Garage, Ceph...).
Index ¶
- Constants
- Variables
- func Allowed(mimeType string, patterns []string) bool
- func DetectType(head []byte, fileName string) string
- func ImageSize(r io.Reader) (width, height int, ok bool)
- func InlineSafe(mimeType string) bool
- func Within(root, path string) bool
- type Config
- type Local
- func (l *Local) Delete(_ context.Context, key string) error
- func (l *Local) Driver() string
- func (l *Local) Path(key string) (string, error)
- func (l *Local) Put(_ context.Context, key string, r io.Reader, _ int64, _ ObjectMeta) error
- func (l *Local) Root() string
- func (l *Local) URL(_ context.Context, key string) (string, error)
- type LocalConfig
- type ObjectMeta
- type S3
- type S3Config
- type Store
Constants ¶
const LocalPathPrefix = "/uploads"
LocalPathPrefix is where the API serves local files.
Variables ¶
var DefaultAllowedTypes = []string{
"image/jpeg", "image/png", "image/gif", "image/webp", "image/avif",
"video/mp4", "video/webm", "video/quicktime",
"audio/mpeg", "audio/ogg", "audio/wav", "audio/webm", "audio/aac", "audio/mp4",
"application/pdf",
"text/plain", "text/csv", "text/markdown",
"application/zip", "application/json",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.oasis.opendocument.text",
"application/vnd.oasis.opendocument.spreadsheet",
}
DefaultAllowedTypes are accepted when media.allowed_types is not set: common images, video, audio and documents. SVG, HTML and scripts are left out on purpose: served from your domain they can run scripts.
var ErrInvalidKey = errors.New("invalid media key")
ErrInvalidKey is returned for keys that could escape the storage root.
Functions ¶
func Allowed ¶
Allowed reports whether mimeType matches one of the patterns, which are exact types ("image/png"), wildcards ("image/*") or "*/*".
func DetectType ¶
DetectType returns the MIME type of a file from its first bytes, using the file name only when the content alone is ambiguous. The client's declared Content-Type is never trusted.
func ImageSize ¶
ImageSize returns the pixel dimensions of a JPEG, PNG, GIF or WebP image, reading only its header.
func InlineSafe ¶
InlineSafe reports whether browsers may display a type inline. Everything else is served as an attachment.
Types ¶
type Config ¶
type Config struct {
Driver string // "local" (default) or "s3"
// PublicURL is the base URL files are served from, e.g. a CDN
// ("https://cdn.example.com") or the bucket's public endpoint. Keys are
// appended to it. Optional: local files default to /uploads/<key> on
// the API, and S3 files to short-lived presigned URLs.
PublicURL string
Local LocalConfig
S3 S3Config
}
Config selects and configures a Store.
type Local ¶
type Local struct {
// contains filtered or unexported fields
}
Local stores files in a directory. It suits development and single-replica deployments; replicas do not share it unless the directory is a shared volume.
func NewLocal ¶
NewLocal returns a Local store writing under root (created if missing). publicURL is the base download URL; empty means LocalPathPrefix on the API.
type LocalConfig ¶
type LocalConfig struct {
Path string // directory files are written to
}
LocalConfig configures the filesystem driver.
type ObjectMeta ¶
type ObjectMeta struct {
ContentType string
// Attachment makes browsers download the file instead of rendering it
// (used for every type that is not safe to display inline).
Attachment bool
FileName string
}
ObjectMeta is stored with an object and sent back when it is served.
type S3 ¶
type S3 struct {
// contains filtered or unexported fields
}
S3 stores files in an S3-compatible bucket.
type S3Config ¶
type S3Config struct {
Endpoint string // URL ("https://s3.amazonaws.com") or host[:port] with UseSSL
Region string
Bucket string
AccessKey string
SecretKey string
UseSSL bool
// PathStyle uses endpoint/bucket/key URLs instead of bucket.endpoint/key
// (needed by MinIO and most self-hosted stores).
PathStyle bool
// Prefix is prepended to every key, to share a bucket.
Prefix string
// PresignExpiry is the lifetime of download URLs when PublicURL is
// empty.
PresignExpiry time.Duration
}
S3Config configures the S3-compatible driver.
type Store ¶
type Store interface {
// Driver names the backend ("local" or "s3").
Driver() string
// Put stores size bytes from r under key.
Put(ctx context.Context, key string, r io.Reader, size int64, meta ObjectMeta) error
// Delete removes key. Deleting a missing key is not an error.
Delete(ctx context.Context, key string) error
// URL returns the address clients use to download key.
URL(ctx context.Context, key string) (string, error)
}
Store saves and serves file contents. Keys are relative, slash-separated paths such as "2026/10/3f2a9c-photo.jpg".