uploads

package
v2.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: GPL-3.0 Imports: 13 Imported by: ? modules, 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 uploads receives a file from a client in chunks. A session carries the bytes; a purpose, registered by the feature that wants the file, owns the rules: how large it may be, where it is staged and what it must hold. The feature then takes the finished file by its upload ID.

Index

Constants

View Source
const (
	// ChunkBytes is the largest chunk a session accepts, and the size a
	// client is told to send.
	ChunkBytes = 128 << 10
	// MaxSessions caps the uploads in flight at once.
	MaxSessions = 4
	// IdleTimeout ends a session no chunk or request has touched.
	IdleTimeout = 10 * time.Minute
)

Variables

View Source
var (
	ErrUnknownPurpose  = errors.New("unknown upload purpose")
	ErrTooLarge        = errors.New("upload is larger than its purpose allows")
	ErrInvalidSize     = errors.New("upload size must be greater than zero")
	ErrInvalidHash     = errors.New("upload sha256 must be 64 hex characters")
	ErrTooManySessions = errors.New("too many uploads in progress")
	ErrNotFound        = errors.New("upload not found")
	ErrWrongOffset     = errors.New("chunk offset is not the next byte of the upload")
	ErrChunkTooLarge   = errors.New("chunk is larger than the upload's chunk size")
	ErrEmptyChunk      = errors.New("chunk is empty")
	ErrOverrun         = errors.New("chunk runs past the upload's declared size")
	ErrIncomplete      = errors.New("upload is not complete")
	ErrHashMismatch    = errors.New("upload does not match its sha256")
	ErrFinished        = errors.New("upload is already finished")
	ErrNotFinished     = errors.New("upload is not finished")
	ErrWrongPurpose    = errors.New("upload was made for another purpose")
)

Functions

This section is empty.

Types

type Info

type Info struct {
	ID        string
	Purpose   string
	Size      int64
	Received  int64
	ChunkSize int
	Finished  bool
}

Info describes a session to its client.

type Manager

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

Manager holds the upload sessions of one Core process. Sessions live in memory: a restart ends them, and CleanStaging removes what they left.

func NewManager

func NewManager(fs afero.Fs) *Manager

NewManager returns a manager with no purposes registered.

func (*Manager) Begin

func (m *Manager) Begin(purposeName, owner string, size int64, sha256Hex string) (Info, error)

Begin opens a session for size bytes whose SHA-256 is sha256Hex. owner names the client; only it may continue the session.

func (*Manager) Cancel

func (m *Manager) Cancel(id, owner string) error

Cancel discards a session and its staged bytes.

func (*Manager) Chunk

func (m *Manager) Chunk(id, owner string, offset int64, data []byte) (Info, error)

Chunk appends data at offset, which must be the next byte the session expects: chunks arrive in order, and a client that lost its place asks Status.

func (*Manager) Finish

func (m *Manager) Finish(id, owner string) (Info, error)

Finish checks a fully received upload against its size, its SHA-256 and its purpose's content rule. An upload that fails a check is discarded.

func (*Manager) Purpose

func (m *Manager) Purpose(name string) (Purpose, bool)

Purpose returns a registered purpose.

func (*Manager) Ready

func (m *Manager) Ready(id, owner, purposeName string) error

Ready reports whether Take would hand over the upload: a finished upload of owner for the given purpose. It does not use the upload up.

func (*Manager) Register

func (m *Manager) Register(purpose Purpose)

Register adds a purpose and removes staging files a previous run left in its folder.

func (*Manager) Status

func (m *Manager) Status(id, owner string) (Info, error)

Status reports a session, so a client that lost its connection can resume from Received.

func (*Manager) Take

func (m *Manager) Take(id, owner, purposeName string, use func(path string) error) error

Take hands a finished upload of the given purpose to use, which gets the staged file's path and may read or move it. The session ends when use returns without an error, and the staged file is removed if it is still there; an error leaves the upload to be taken again.

type Purpose

type Purpose struct {
	// Validate checks a complete staged file before the upload may finish.
	// Nil accepts any content.
	Validate func(fs afero.Fs, path string) error
	Name     string
	// StagingDir is where chunks are written. It should be on the volume the
	// finished file will live on, so taking it is a rename.
	StagingDir string
	MaxBytes   int64
	// AdminOnly limits the purpose to local and admin clients. The API
	// layer enforces it.
	AdminOnly bool
}

Purpose is one kind of upload and its rules.

Jump to

Keyboard shortcuts

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