media

package
v0.0.0-...-01cc07e Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package media implements tenant-scoped media upload + retrieval.

Per gocodealone-multisite SPEC.md T25.

Two backends are supported via the Backend interface:

  • LocalFS — files written under <root>/<tenant_id>/<sha256>.<ext>.
  • Spaces — DigitalOcean Spaces (S3-compatible) via presigned URL.

The LocalFS backend ships in this file; Spaces lives in media/spaces.go (added when the host needs it).

Index

Constants

View Source
const MaxUploadBytes = 10 * 1024 * 1024

MaxUploadBytes is the default per-request cap (10 MiB).

Variables

View Source
var ErrNotFound = errors.New("media: not found")

ErrNotFound is returned by Backend.Get on a miss.

View Source
var ErrTooLarge = errors.New("media: payload too large")

ErrTooLarge is returned by the handler when the upload exceeds the configured cap.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Put stores body for tenantID, returning a stable public URL.
	Put(tenantID int64, body io.Reader, contentType string) (string, error)
	// Get returns the file contents + content-type for a stored URL.
	// Implementations may return ErrNotFound.
	Get(url string) (io.ReadCloser, string, error)
}

Backend persists + retrieves tenant-scoped media.

type LocalFS

type LocalFS struct {
	Root      string
	PublicURL string // e.g. "https://gocodealone.tech/media"; appended with /<tenant>/<sha>.<ext>
}

LocalFS is a development backend that writes under root.

Path layout: <root>/<tenant_id>/<sha256>.<ext>. The sha256 is content-addressed — uploading the same file twice yields the same URL (idempotent).

func (*LocalFS) Get

func (l *LocalFS) Get(url string) (io.ReadCloser, string, error)

func (*LocalFS) Put

func (l *LocalFS) Put(tenantID int64, body io.Reader, contentType string) (string, error)

type UploadHandler

type UploadHandler struct {
	Backend Backend
}

UploadHandler is an http.Handler that consumes multipart/form-data uploads under POST /api/v1/admin/tenants/:id/upload. Tenant ID is extracted from the URL by the caller.

The handler:

  1. Caps the read at MaxUploadBytes + 1 byte to detect overflow.
  2. Hands the body to the configured Backend.
  3. Returns 201 + {url, content_type, bytes} on success.

func (*UploadHandler) ServeForTenant

func (h *UploadHandler) ServeForTenant(w http.ResponseWriter, r *http.Request, tenantID int64)

ServeForTenant performs an upload scoped to tenantID. Returns the stored URL.

Jump to

Keyboard shortcuts

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