uploads

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: EUPL-1.2 Imports: 13 Imported by: 0

Documentation

Overview

Package uploads provides file upload storage as a burrow contrib app. It offers a pluggable Storage interface with a local filesystem implementation and content-hashed filenames for deduplication.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeNotAllowed = errors.New("uploads: file type not allowed")
	ErrFileTooLarge   = errors.New("uploads: file too large")
	ErrEmptyFile      = errors.New("uploads: empty file")
	ErrMissingField   = errors.New("uploads: missing form field")
)

Sentinel errors for upload validation.

Functions

func StoreFile

func StoreFile(r *http.Request, fieldName string, storage Storage, opts StoreOptions) (string, error)

StoreFile extracts a file from a multipart request and stores it. It returns the storage key on success.

func URL

func URL(ctx context.Context, key string) string

URL returns the public URL for a storage key. If no Storage is in the context, it returns the key as-is (safe fallback for templates).

func WithStorage

func WithStorage(ctx context.Context, s Storage) context.Context

WithStorage returns a new context carrying the given Storage.

Types

type App

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

App implements the uploads contrib app for file storage and serving.

func New

func New(opts ...Option) *App

New creates an uploads app with sensible defaults:

  • Upload directory: ./uploads (relative to working directory)
  • URL prefix: /uploads/
  • Allowed types: all

Use options to customize, or override at runtime via CLI flags (--upload-dir, --upload-url-prefix, --upload-allowed-types).

func (*App) AllowedTypes

func (a *App) AllowedTypes() []string

AllowedTypes returns the globally configured allowed MIME types.

func (*App) Configure

func (a *App) Configure(cmd *cli.Command) error

func (*App) Flags

func (a *App) Flags(configSource func(key string) cli.ValueSource) []cli.Flag

func (*App) Middleware

func (a *App) Middleware() []func(http.Handler) http.Handler

func (*App) Name

func (a *App) Name() string

func (*App) Register

func (a *App) Register(_ *burrow.AppConfig) error

func (*App) Routes

func (a *App) Routes(r chi.Router)

func (*App) Storage

func (a *App) Storage() Storage

Storage returns the configured Storage backend.

type LocalStorage

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

LocalStorage stores files on the local filesystem.

func NewLocalStorage

func NewLocalStorage(root, urlPrefix string) (*LocalStorage, error)

NewLocalStorage creates a LocalStorage that persists files under root and serves them at urlPrefix.

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(_ context.Context, key string) error

Delete removes the file at the given key. It does not return an error if the file does not exist.

func (*LocalStorage) Open

func (s *LocalStorage) Open(_ context.Context, key string) (io.ReadCloser, error)

Open returns a reader for the file at the given key.

func (*LocalStorage) Path

func (s *LocalStorage) Path(key string) string

Path returns the filesystem path for the given storage key. This is specific to LocalStorage and not part of the Storage interface.

func (*LocalStorage) Store

func (s *LocalStorage) Store(_ context.Context, file io.Reader, opts StoreOptions) (string, error)

Store persists a file and returns its content-hashed storage key.

func (*LocalStorage) URL

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

URL returns the public URL for the given storage key.

type Option

type Option func(*App)

Option configures the uploads app.

func WithAllowedTypes

func WithAllowedTypes(types ...string) Option

WithAllowedTypes sets the default allowed MIME types for uploads. Per-call StoreOptions.AllowedTypes overrides this default.

func WithBaseDir

func WithBaseDir(dir string) Option

WithBaseDir sets the base directory for uploads. The upload directory becomes {baseDir}/uploads. Defaults to "data" (i.e. data/uploads).

func WithURLPrefix

func WithURLPrefix(prefix string) Option

WithURLPrefix sets the URL prefix for serving uploaded files.

type Storage

type Storage interface {
	// Store persists a file and returns its storage key.
	Store(ctx context.Context, file io.Reader, opts StoreOptions) (key string, err error)

	// Delete removes a file by its storage key.
	Delete(ctx context.Context, key string) error

	// Open returns a reader for the file at the given key.
	Open(ctx context.Context, key string) (io.ReadCloser, error)

	// URL returns the public URL for the given storage key.
	URL(key string) string
}

Storage defines the interface for file storage backends.

func StorageFromContext

func StorageFromContext(ctx context.Context) Storage

StorageFromContext returns the Storage from the context, or nil.

type StoreOptions

type StoreOptions struct {
	Prefix       string   // subdirectory, e.g. "avatars"
	Filename     string   // original filename (for extension extraction)
	AllowedTypes []string // e.g. ["image/jpeg", "image/png"]; empty = all
	MaxSize      int64    // per-file limit in bytes (0 = no limit)
}

StoreOptions configures a single file store operation.

Jump to

Keyboard shortcuts

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