resourceupload

package
v1.170.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 24 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 resourceupload is the single-use upload link (#2639): manage_resource action=upload_link issues a short-lived URL on the platform's own public origin, and one PUT of a file to it from the agent's shell files the file as a managed resource at the address the link was issued for, as the person who asked for it, with the checks a portal upload gets. The bytes never pass through a tool call, and nothing about object storage has to be public.

The link's token is a bearer credential for the files it was issued for. It is handed to the caller once and never stored: a link is found by the token's SHA-256, a place on it taken by one atomic statement that also refuses a full, revoked or expired link, and it is bound to its folder, its declared type and its size.

A link an external service uses (#2844) lives up to a day, so it outlasts a job queue, takes up to max_files files, and, issued with no filename, has the service name each file in the URL. A PUT that files nothing gives its place back, so the service can try again. The person who asked for a link sees it, and can revoke it, on the portal's Resources page.

Index

Constants

View Source
const (
	Route      = "PUT " + RoutePrefix + "{token}"
	NamedRoute = "PUT " + RoutePrefix + "{token}/{filename}"
)

Route is the pattern a link is PUT to, and NamedRoute the one a link issued with no filename is PUT to, the file's name after the token (#2844).

View Source
const (
	DefaultTTL = 15 * time.Minute
	MaxTTL     = 24 * time.Hour
)

How long a link lives: DefaultTTL unless the caller asks for another lifetime, never more than MaxTTL, which a job waiting in a queue fits in (#2844).

View Source
const MaxFiles = 100

MaxFiles is the most files one link takes.

View Source
const (
	PortalRoute = "/api/v1/resources/upload-links"
)

Portal routes.

View Source
const RoutePrefix = "/api/v1/resources/uploads/"

RoutePrefix is where a link is PUT to, followed by its token.

Variables

View Source
var (
	// ErrNoLink is a token no link answers to: unknown, used or expired,
	// which are told apart to no one.
	ErrNoLink = errors.New("this upload link is unknown, used or expired: ask for a new one with manage_resource action=upload_link")
	// ErrTooLarge is a file past the size the link was issued for.
	ErrTooLarge = errors.New("the file is larger than the size this upload link was issued for")
	// ErrRefused is a link that cannot be issued as asked.
	ErrRefused = errors.New("refused")
	// ErrBadName is a PUT whose file name the link does not take: none for a
	// link issued without one, another for a link issued with one, or a name
	// that is not a plain file name.
	ErrBadName = errors.New("bad file name")
)

Errors the HTTP surface answers by identity.

Functions

func Hash

func Hash(token string) string

Hash is the form a token is kept and found by.

func MountFrom

func MountFrom[T any](mux *http.ServeMux, kits []T)

MountFrom registers the link's route on mux from the toolkit among kits that issues links (the portal toolkit's MountUploadlinks).

func MountPortalFrom added in v1.169.5

func MountPortalFrom[T any](mux *http.ServeMux, kits []T, extract resource.ClaimsExtractor)

MountPortalFrom registers the portal's link routes on mux from the toolkit among kits that issues links.

Types

type IssueRequest

type IssueRequest struct {
	Scope       resource.Scope
	ScopeID     string
	Path        string
	Filename    string
	DisplayName string
	Description string
	Tags        []string
	ContentType string
	IfExists    string
	// MaxBytes is the most each file may be.
	MaxBytes int64
	// TTL is how long the link lives; zero is DefaultTTL.
	TTL time.Duration
	// MaxFiles is how many files the link takes; zero is one.
	MaxFiles int
}

IssueRequest is a link as the caller asks for it, its address already resolved and checked by manage_resource.

type Issued

type Issued struct {
	ID        string    `json:"id"`
	URL       string    `json:"upload_url"`
	Method    string    `json:"method"`
	ExpiresAt time.Time `json:"expires_at"`
	MaxBytes  int64     `json:"max_bytes"`
	MaxFiles  int       `json:"max_files"`
	// NameInURL says each file is named by the path segment after URL.
	NameInURL bool `json:"name_in_url,omitempty"`
}

Issued is a link as the caller is handed it, once. With a filename the file is PUT to URL; without one each file is PUT to URL/<filename>.

type Open added in v1.169.5

type Open struct {
	ID          string    `json:"id"`
	Scope       string    `json:"scope"`
	ScopeID     string    `json:"scope_id,omitempty"`
	Path        string    `json:"path"`
	Filename    string    `json:"filename,omitempty"`
	ContentType string    `json:"content_type"`
	MaxBytes    int64     `json:"max_bytes"`
	MaxFiles    int       `json:"max_files"`
	FilesUsed   int       `json:"files_used"`
	CreatedAt   time.Time `json:"created_at"`
	ExpiresAt   time.Time `json:"expires_at"`
}

Open is a link that can still take a file, as its issuer sees it on the Resources page. It never carries the token.

type PostgresStore

type PostgresStore struct {
	DB *sql.DB
}

PostgresStore keeps the links in resource_upload_links.

func NewStore

func NewStore(db *sql.DB) *PostgresStore

NewStore is the links' store over db, nil without one.

type Service

type Service struct {
	Store  Store
	Writer Writer
	// BaseURL is the platform's public origin (portal.public_base_url); a
	// link is not issued without one, since there is nothing to name.
	BaseURL func() string
	// MaxUploadBytes is the deployment's upload ceiling; non-positive is
	// resource.MaxUploadBytes.
	MaxUploadBytes int64
	// Now is the clock; nil is time.Now.
	Now func() time.Time
}

Service issues links and files what is PUT to them.

func (*Service) Issue

func (s *Service) Issue(ctx context.Context, in IssueRequest, claims resource.Claims, userID string) (Issued, error)

Issue keeps a link for in, writable by claims, and returns it.

func (*Service) Mount

func (s *Service) Mount(mux *http.ServeMux)

Mount registers the link's route on mux. The route takes no session or key: the link is the credential.

func (*Service) MountPortal added in v1.169.5

func (s *Service) MountPortal(mux *http.ServeMux, extract resource.ClaimsExtractor)

MountPortal registers the portal's link routes on mux, each reading the caller's resource claims with extract, as the resource routes do.

func (*Service) Opened added in v1.169.5

func (s *Service) Opened(ctx context.Context, claims resource.Claims) ([]Open, error)

Opened lists the links the person claims names asked for that can still take a file.

func (*Service) Revoke added in v1.169.5

func (s *Service) Revoke(ctx context.Context, id string, claims resource.Claims) error

Revoke ends the link id, which its issuer or an administrator may.

func (*Service) Upload

func (s *Service) Upload(
	ctx context.Context, token, name string, body io.Reader, declared int64,
) (res *resource.Resource, remaining int, err error)

Upload files body, PUT to the link token names, in the link's folder as the person who asked for it. name is the file name the URL carries after the token, empty when it carries none; declared is the request's Content-Length, -1 when it sent none. A file filed takes one of the link's places; a PUT that files nothing gives its place back. remaining is how many more files the link takes.

type Store

type Store interface {
	// contains filtered or unexported methods
}

Store keeps the links (PostgresStore).

type Writer

type Writer interface {
	Create(ctx context.Context, in resource.NewResource, claims resource.Claims) (*resource.Resource, error)
	Replace(ctx context.Context, id string, up resource.RevisionUpload, claims resource.Claims) (*resource.Resource, int, error)
	Locate(ctx context.Context, addr toolkit.ResourceAddress, claims resource.Claims) (*resource.Resource, string, error)
}

Writer is what files the upload: the writer behind manage_resource.

Jump to

Keyboard shortcuts

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