media

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 11 Imported by: 1 module, 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 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