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 ¶
const MaxUploadBytes = 10 * 1024 * 1024
MaxUploadBytes is the default per-request cap (10 MiB).
Variables ¶
var ErrNotFound = errors.New("media: not found")
ErrNotFound is returned by Backend.Get on a miss.
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).
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:
- Caps the read at MaxUploadBytes + 1 byte to detect overflow.
- Hands the body to the configured Backend.
- 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.