Documentation
¶
Overview ¶
Package media stores uploaded images and serves them under /media/.
Index ¶
- Variables
- type Bucket
- type Dir
- type Image
- type Media
- type Proxy
- type Service
- func (s *Service) AddWorkers(workers *river.Workers)
- func (s *Service) GuardUpload(next http.Handler) http.Handler
- func (s *Service) Handler() http.Handler
- func (s *Service) PeriodicJobs() []*river.PeriodicJob
- func (s *Service) Queues() map[string]river.QueueConfig
- func (s *Service) Register(api huma.API)
- func (s *Service) Save(ctx context.Context, ownerID int64, r io.Reader) (Media, error)
- func (s *Service) Sweep(ctx context.Context, before time.Time) error
- func (s *Service) URL(key string) string
- type Store
- type SweepArgs
- type Uploaded
Constants ¶
This section is empty.
Variables ¶
var ( ErrTooLarge = errors.New("the image is too large") ErrNotAnImage = errors.New("upload a JPEG, PNG or WebP image") )
var ErrNoFile = errors.New("no media file with that key")
var ErrNotYours = errors.New("no image of yours with that key")
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.
type Image ¶
Image is an upload after processing: re-encoded, so no metadata survives.
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.
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 ¶
AddWorkers registers the media sweep.
func (*Service) GuardUpload ¶
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 ¶
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) Save ¶
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.
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.
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