media

package
v0.1.0-beta.9 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 35 Imported by: 0

Documentation

Overview

Package media stores uploaded images and serves them under /media/.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTooLarge   = errors.New("the image is too large")
	ErrNotAnImage = errors.New("upload a JPEG, PNG or WebP image")
)
View Source
var ErrNoFile = errors.New("no media file with that key")
View Source
var ErrNotYours = errors.New("no image of yours with that key")
View Source
var ErrQuota = errors.New("your upload allowance is full")

ErrQuota is returned when an upload would pass the owner's total allowance.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket stores media in an S3-compatible bucket.

func NewBucket

func NewBucket(c platform.S3Config) (*Bucket, error)

func (*Bucket) Delete

func (b *Bucket) Delete(ctx context.Context, key string) error

func (*Bucket) Open

func (b *Bucket) Open(ctx context.Context, key string) (io.ReadCloser, error)

func (*Bucket) Put

func (b *Bucket) Put(ctx context.Context, key, mime string, data []byte) error

type Dir

type Dir string

Dir stores media as files in a directory.

func (Dir) Delete

func (d Dir) Delete(_ context.Context, key string) error

func (Dir) Open

func (d Dir) Open(_ context.Context, key string) (io.ReadCloser, error)

func (Dir) Put

func (d Dir) Put(_ context.Context, key, _ string, data []byte) error

type Image

type Image struct {
	Data          []byte
	Mime          string
	Width, Height int
}

Image is an upload after processing: re-encoded, so no metadata survives.

func Process

func Process(r io.Reader, t platform.Tuning) (Image, error)

Process decodes a JPEG, PNG or WebP upload, applies its EXIF orientation, scales it down to the size cap and encodes it again. The new encoding holds no EXIF, GPS or other metadata from the original. Opaque images become JPEG; images with transparency become PNG.

type Media

type Media = mediadb.Medium

func Owned

func Owned(ctx context.Context, db mediadb.DBTX, key string, ownerID int64) (Media, error)

Owned returns a media row that owner uploaded.

type Proxy

type Proxy struct {
	// Key signs proxied URLs; NewProxy derives it from SECRET_KEY.
	Key []byte
	// HTTP is the SSRF-guarded client.
	HTTP      *http.Client
	AppOrigin string
	MaxBytes  int64
	MaxAge    time.Duration
	Log       *slog.Logger
}

Proxy serves remote images through /proxy without storing them. Only URLs this installation signed are fetched, so the endpoint is not an open relay.

func NewProxy

func NewProxy(secret []byte, client *http.Client, appOrigin string, maxBytes int64, maxAge time.Duration, log *slog.Logger) *Proxy

func (*Proxy) Image

func (p *Proxy) Image(src string) string

Image is where a reader loads an image a post or profile body shows: this site's own and relative images stay, remote ones go through the proxy, so a peer never learns who reads what.

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Proxy) URL

func (p *Proxy) URL(remote string) string

URL is where a reader loads the remote image at remote.

type Service

type Service struct {
	Pool      *pgxpool.Pool
	Store     Store
	Tuning    platform.Tuning
	AppOrigin string
	Log       *slog.Logger
	// contains filtered or unexported fields
}

Service accepts uploads and serves stored media. Media belongs to its uploader's host, so URLs are on the app origin.

func (*Service) AddWorkers

func (s *Service) AddWorkers(workers *river.Workers)

AddWorkers registers the media sweep.

func (*Service) GuardUpload

func (s *Service) GuardUpload(next http.Handler) http.Handler

GuardUpload sits in front of the API. Huma parses a multipart body without a size cap and leaves its temp files behind, so this refuses an anonymous upload before reading it, caps the body, parses it here and removes the temp files once the handler is done. Huma reuses the parsed form.

func (*Service) Handler

func (s *Service) Handler() http.Handler

Handler serves /media/{key}. A file never changes, but a moderator can hide it, so browsers keep it for a day only.

func (*Service) PeriodicJobs

func (s *Service) PeriodicJobs() []*river.PeriodicJob

PeriodicJobs sweeps unused uploads every MediaSweepInterval.

func (*Service) Queues

func (s *Service) Queues() map[string]river.QueueConfig

Queues is the River queue config for the media sweep.

func (*Service) Register

func (s *Service) Register(api huma.API)

func (*Service) Save

func (s *Service) Save(ctx context.Context, ownerID int64, r io.Reader) (Media, error)

Save processes an upload, stores the file and records it for owner. The owner's uploads are serialised, so parallel uploads cannot pass the allowance.

func (*Service) Sweep

func (s *Service) Sweep(ctx context.Context, before time.Time) error

Sweep deletes the rows of unused uploads made before before, then their files. A row goes first, so no post can attach a file that is gone.

func (*Service) URL

func (s *Service) URL(key string) string

URL is where a stored file is served.

type Store

type Store interface {
	Put(ctx context.Context, key, mime string, data []byte) error
	Open(ctx context.Context, key string) (io.ReadCloser, error)
	// Delete removes a file; a missing file is not an error.
	Delete(ctx context.Context, key string) error
}

Store keeps media files by key. Installations use a directory or an S3-compatible bucket.

func NewStore

func NewStore(cfg platform.Config) (Store, error)

NewStore picks the store the config names.

type SweepArgs

type SweepArgs struct{}

SweepArgs deletes uploads that nothing uses: images of deleted posts and accounts, replaced banners, and uploads never attached.

func (SweepArgs) InsertOpts

func (SweepArgs) InsertOpts() river.InsertOpts

func (SweepArgs) Kind

func (SweepArgs) Kind() string

type Uploaded

type Uploaded struct {
	Key    string `json:"key" doc:"Pass this as image when you post, or as the banner of a community theme"`
	URL    string `json:"url"`
	Width  int32  `json:"width"`
	Height int32  `json:"height"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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