files

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 8 Imported by: 0 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 files owns source Files: Project-owned uploaded bytes with immutable metadata, independent of Sessions and of the workspace copies made from them. It defines their vocabulary, the upload and listing rules, and the create and delete use cases, and declares the storage they need.

Index

Constants

View Source
const (
	// MaxBytes bounds the content of one File.
	MaxBytes int64 = 512 << 20
	// MaxPageSize bounds one List page.
	MaxPageSize = 10000
	// PurposeUserData is the only purpose Core stores.
	PurposeUserData = "user_data"
)

Variables

View Source
var (
	// ErrNotFound reports a File the tenant does not own, including a malformed
	// or foreign ID, so missing and foreign Files are indistinguishable.
	ErrNotFound = errors.New("file not found")
	// ErrInvalidInput reports an upload envelope, list query or tenant that
	// Core does not accept.
	ErrInvalidInput = errors.New("invalid file request")
	// ErrTooLarge reports content beyond the operation's limit.
	ErrTooLarge = errors.New("file exceeds the content limit")
)

Functions

func FormatID

func FormatID(id uuid.UUID) string

FormatID returns the ID of the File stored under id.

func ParseID

func ParseID(id string) (uuid.UUID, bool)

ParseID returns the stored UUID that a File ID names. ok is false for an ID Core never assigns: another spelling, another prefix or the nil UUID.

Types

type CreateCommand

type CreateCommand struct {
	TenantID string
	Upload   func(io.Writer) (Upload, error)
}

CreateCommand uploads one File. Upload writes the content to the writer it receives and returns the envelope read with it.

type DeleteCommand

type DeleteCommand struct {
	TenantID string
	FileID   string
}

DeleteCommand deletes one File.

type File

type File struct {
	ID        string
	Filename  string
	Purpose   string
	SizeBytes int64
	CreatedAt time.Time
}

File is a stored File's immutable metadata.

type ListQuery

type ListQuery struct {
	// After is the ID of the previous page's last File, or empty for the first
	// page.
	After     string
	Limit     int
	Ascending bool
	// Purpose, when not nil, keeps only Files with this purpose.
	Purpose *string
}

ListQuery selects one page of a tenant's Files, ordered by creation time and then ID.

func (ListQuery) Validate

func (q ListQuery) Validate() error

Validate accepts a page size of 1 to MaxPageSize and a purpose filter that PostgreSQL can compare.

type Page

type Page struct {
	Files      []File
	NextCursor string
}

Page is one List page. NextCursor is the ID of its last File when more Files follow, and empty otherwise.

type Reader

type Reader interface {
	// Get returns the File's metadata, or ErrNotFound.
	Get(ctx context.Context, tenantID, fileID string) (File, error)
	// List returns one page. An After cursor that names no File of the tenant
	// returns ErrNotFound.
	List(ctx context.Context, tenantID string, query ListQuery) (Page, error)
	// Read passes consume the File's metadata and content from one snapshot, so
	// a concurrent Delete never truncates a read it admitted. It returns
	// consume's error unchanged.
	Read(ctx context.Context, tenantID, fileID string, consume func(File, io.Reader) error) error
}

Reader reads a tenant's Files.

type Service

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

Service runs the File use cases that change stored state.

func NewService

func NewService(storage Storage) (*Service, error)

func (*Service) Create

func (s *Service) Create(ctx context.Context, command CreateCommand) (File, error)

Create stores the File only after its complete content fits MaxBytes and its envelope validates. A content write that failed, including one beyond MaxBytes, fails the upload even when Upload ignored the write error.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, command DeleteCommand) error

Delete removes the File and its content. Workspace copies made from it are independent and stay.

type Storage

type Storage interface {
	// Create allocates the content, calls write with a writer for it, and stores
	// the File with the returned envelope only when write returns nil. A
	// storage failure while writing takes precedence over write's own error.
	Create(ctx context.Context, tenantID string, write func(io.Writer) (Upload, error)) (File, error)
	// Delete removes the File and its content, or returns ErrNotFound.
	Delete(ctx context.Context, tenantID, fileID string) error
}

Storage persists Files. Each method runs in one transaction.

type Upload

type Upload struct {
	Filename string
	Purpose  string
}

Upload is the envelope read alongside an upload's body.

func (Upload) Validate

func (u Upload) Validate() error

Validate accepts the envelopes Core stores: a filename of 1 to 1024 bytes of valid UTF-8 without U+0000, and the user_data purpose.

Jump to

Keyboard shortcuts

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