upload

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package upload decodes multipart upload requests with validation errors.

The package is stable core API. It owns transport-level multipart parsing, request and file size checks, required file fields, content-type allowlists, and Problem Details-compatible field errors. It does not own object storage, virus scanning, media processing, persistence, or authorization.

Use DecodeMultipart with Config to parse a request into a Form, then RequireFile to fetch required file fields by name. AllowedContentTypes and MaxFileBytes are small helpers for constructing readable Config values. ValidationProblem and WriteValidationProblem preserve the same error shape as binding and httpx.

Always apply authentication, authorization, total request limits, and application-owned scanning before trusting uploaded content. For examples, see docs/cookbook.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowedContentTypes

func AllowedContentTypes(types ...string) []string

AllowedContentTypes returns a normalized content-type allowlist for Config.

func MaxFileBytes

func MaxFileBytes(n int64) int64

MaxFileBytes returns a file-size limit value for Config.

func ValidationProblem

func ValidationProblem(err error) httpx.Problem

ValidationProblem maps upload validation errors to Problem Details.

func WriteValidationProblem

func WriteValidationProblem(w http.ResponseWriter, err error)

WriteValidationProblem writes upload validation errors as Problem Details.

Types

type Config

type Config struct {
	MaxMemory           int64
	MaxRequestBytes     int64
	MaxFileBytes        int64
	RequiredFiles       []string
	AllowedContentTypes []string
}

Config configures multipart decoding.

type File

type File struct {
	FieldName   string
	Filename    string
	ContentType string
	Size        int64
	Header      textproto.MIMEHeader
	FileHeader  *multipart.FileHeader
}

File describes an uploaded multipart file.

func RequireFile

func RequireFile(form Form, field string) (File, error)

RequireFile returns the first file for a required field.

func (File) Open

func (f File) Open() (multipart.File, error)

Open opens the uploaded file stream.

type Form

type Form struct {
	Values map[string][]string
	Files  map[string][]File
}

Form describes decoded multipart values and files.

func DecodeMultipart

func DecodeMultipart(r *http.Request, config Config) (Form, error)

DecodeMultipart decodes a multipart form request.

Jump to

Keyboard shortcuts

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