media

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const LocalPathPrefix = "/uploads"

LocalPathPrefix is where the API serves local files.

Variables

View Source
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.

View Source
var ErrInvalidKey = errors.New("invalid media key")

ErrInvalidKey is returned for keys that could escape the storage root.

Functions

func Allowed

func Allowed(mimeType string, patterns []string) bool

Allowed reports whether mimeType matches one of the patterns, which are exact types ("image/png"), wildcards ("image/*") or "*/*".

func DetectType

func DetectType(head []byte, fileName string) string

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

func ImageSize(r io.Reader) (width, height int, ok bool)

ImageSize returns the pixel dimensions of a JPEG, PNG, GIF or WebP image, reading only its header.

func InlineSafe

func InlineSafe(mimeType string) bool

InlineSafe reports whether browsers may display a type inline. Everything else is served as an attachment.

func Within

func Within(root, path string) bool

Within reports whether path is inside root once both are cleaned.

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

func NewLocal(root, publicURL string) (*Local, error)

NewLocal returns a Local store writing under root (created if missing). publicURL is the base download URL; empty means LocalPathPrefix on the API.

func (*Local) Delete

func (l *Local) Delete(_ context.Context, key string) error

func (*Local) Driver

func (l *Local) Driver() string

func (*Local) Path

func (l *Local) Path(key string) (string, error)

Path returns the file path of key, refusing keys that leave the root.

func (*Local) Put

func (l *Local) Put(_ context.Context, key string, r io.Reader, _ int64, _ ObjectMeta) error

func (*Local) Root

func (l *Local) Root() string

Root is the directory files are stored in.

func (*Local) URL

func (l *Local) URL(_ context.Context, key string) (string, error)

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.

func NewS3

func NewS3(ctx context.Context, cfg S3Config, publicURL string) (*S3, error)

NewS3 connects to the bucket described by cfg and checks that it exists.

func (*S3) Delete

func (s *S3) Delete(ctx context.Context, key string) error

func (*S3) Driver

func (s *S3) Driver() string

func (*S3) Put

func (s *S3) Put(ctx context.Context, key string, r io.Reader, size int64, meta ObjectMeta) error

func (*S3) URL

func (s *S3) URL(ctx context.Context, key string) (string, error)

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".

func New

func New(ctx context.Context, cfg Config) (Store, error)

New builds the Store described by cfg.

Jump to

Keyboard shortcuts

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