upload

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package upload provides a prefab plugin that adds an HTTP handler for uploading files. The handler accepts multipart form data, buffers the file in memory, and then persists the file to a storage backend.

Files are stored using a salted hash of their contents. This means the file is content addressable, but not guessable by someone without the salt.

The plugin optionally integrates with the authz plugin to enforce access controls. For upload, the query parameters "domain" and "folder" may be passed. "domain" would typically be a workspace, organization, or high-level grouping. "folder" might be a user id, a project id, or some other entity that governs upload and download rights.

Roadmap for this plugin: - Support streaming (uploads and backends) to reduce memory usage.

Index

Constants

View Source
const (
	// Constant name for identifying the upload plugin.
	PluginName = "upload"

	// authz action for storing files.
	SaveAction = "upload.save"

	// authz action for downloading files.
	DownloadAction = "upload.download"

	// authz object key used to scope RoleDescribers.
	ObjectKey = "upload"
)

Variables

View Source
var File_plugins_upload_upload_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Save saves the file to the backend.
	Save(path string, data []byte) error

	// Get retrieves the file from the backend.
	Get(path string) ([]byte, error)
}

Backend is an interface for storing and retrieving files.

func NewFSBackend

func NewFSBackend(rootDir string) Backend

NewFSBackend returns a new FSBackend.

func NewMemBackend

func NewMemBackend() Backend

NewMemBackend returns a new MemBackend.

type FSBackend

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

FSBackend is a Backend that stores files on the filesystem.

func (*FSBackend) Get

func (b *FSBackend) Get(path string) ([]byte, error)

func (*FSBackend) Save

func (b *FSBackend) Save(path string, data []byte) error

type MemBackend

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

MemBackend is a Backend that stores files in memory. Intended for testing and development. Not threadsafe.

func (*MemBackend) Get

func (b *MemBackend) Get(path string) ([]byte, error)

func (*MemBackend) Save

func (b *MemBackend) Save(path string, data []byte) error

type UploadOption

type UploadOption func(*UploadPlugin)

UploadOptions customize the configuration of the upload plugin.

func WithBackend

func WithBackend(be Backend) UploadOption

WithBackend configures the storage backend to use.

func WithValidTypes

func WithValidTypes(types ...string) UploadOption

WithValidTypes configures the valid file types that can be uploaded. Overriding any values from the configuration.

type UploadPlugin

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

UploadPlugin provides an HTTP handler for uploading files.

func Plugin

func Plugin(opts ...UploadOption) *UploadPlugin

Plugin returns a new UploadPlugin.

func (*UploadPlugin) Init

func (p *UploadPlugin) Init(ctx context.Context, r *prefab.Registry) error

From prefab.InitializablePlugin.

func (*UploadPlugin) Name

func (p *UploadPlugin) Name() string

From prefab.Plugin.

func (*UploadPlugin) OptDeps

func (p *UploadPlugin) OptDeps() []string

From prefab.OptionalDependentPlugin.

func (*UploadPlugin) ServerOptions

func (p *UploadPlugin) ServerOptions() []prefab.ServerOption

From prefab.OptionProvider.

type UploadResponse

type UploadResponse struct {
	Files []*UploadedFile `protobuf:"bytes,1,rep,name=files,proto3" json:"files,omitempty"`
	// contains filtered or unexported fields
}

Response from calling the upload endpoint.

func (*UploadResponse) Descriptor deprecated

func (*UploadResponse) Descriptor() ([]byte, []int)

Deprecated: Use UploadResponse.ProtoReflect.Descriptor instead.

func (*UploadResponse) GetFiles

func (x *UploadResponse) GetFiles() []*UploadedFile

func (*UploadResponse) ProtoMessage

func (*UploadResponse) ProtoMessage()

func (*UploadResponse) ProtoReflect

func (x *UploadResponse) ProtoReflect() protoreflect.Message

func (*UploadResponse) Reset

func (x *UploadResponse) Reset()

func (*UploadResponse) String

func (x *UploadResponse) String() string

type UploadedFile

type UploadedFile struct {

	// The original filename of the uploaded file, so that the client can match
	// the uploaded file with the file that was uploaded.
	OriginalName string `protobuf:"bytes,1,opt,name=original_name,json=originalName,proto3" json:"original_name,omitempty"`
	// The path where the file was uploaded to.
	UploadPath string `protobuf:"bytes,2,opt,name=upload_path,json=uploadPath,proto3" json:"upload_path,omitempty"`
	// contains filtered or unexported fields
}

func (*UploadedFile) Descriptor deprecated

func (*UploadedFile) Descriptor() ([]byte, []int)

Deprecated: Use UploadedFile.ProtoReflect.Descriptor instead.

func (*UploadedFile) GetOriginalName

func (x *UploadedFile) GetOriginalName() string

func (*UploadedFile) GetUploadPath

func (x *UploadedFile) GetUploadPath() string

func (*UploadedFile) ProtoMessage

func (*UploadedFile) ProtoMessage()

func (*UploadedFile) ProtoReflect

func (x *UploadedFile) ProtoReflect() protoreflect.Message

func (*UploadedFile) Reset

func (x *UploadedFile) Reset()

func (*UploadedFile) String

func (x *UploadedFile) String() string

Jump to

Keyboard shortcuts

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