resourcedownload

package
v1.170.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 17 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 resourcedownload is the short-lived download link to a managed resource (#2847): manage_resource action=download_link, or Copy link in the portal, hands back a URL on the platform's public address that anybody holding it can GET the file from, with no session or key, until it expires. It is for handing a file to a connection that needs it now (an API that takes a PDF by URL), and comes in two kinds: a single download, which ends after the first full download or an hour, and one that works for six hours. A HEAD request or a byte-range read is not the download, since a fetcher may make one before it (a PDF reader reads by range).

The link is the asset content URL's token (internal/portal/contenturl, #1848) under a key of its own, so a token minted for one is never valid as the other, every replica verifies what any replica minted, and nothing is stored.

Index

Constants

View Source
const (
	// SingleDownload ends after its first full download, or after
	// SingleTTL.
	SingleDownload = "single_download"
	// SixHours works for anyone holding it for SixHoursTTL.
	SixHours = "6h"

	SingleTTL   = time.Hour
	SixHoursTTL = 6 * time.Hour
)

The two kinds of link (a link's expires), and how long each lives.

View Source
const KeyLabel = "resource-download-link"

KeyLabel derives this package's key from the browser-session signing key.

View Source
const Route = "GET " + RoutePrefix + "{token}/{filename}"

Route is the pattern a link is served on.

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

RoutePrefix is where a link is served: the token, then the file's name, so a reader that judges a file by its URL sees what it is.

Variables

View Source
var (
	// ErrUnavailable is a deployment that cannot hand out links.
	ErrUnavailable = errors.New("this deployment cannot give out download links: it needs auth.browser_session.signing_key " +
		"to sign them and portal.public_base_url to name them")
	// ErrBadExpiry is a kind of link there is none of.
	ErrBadExpiry = fmt.Errorf("expires is %q (ended by the first full download, or an hour) or %q (six hours)", SingleDownload, SixHours)
)

Errors a caller tells apart.

Functions

This section is empty.

Types

type Link struct {
	URL       string    `json:"url" example:"https://data.example.com/api/v1/resources/downloads/eyJ...Q/report.pdf"`
	Expires   string    `json:"expires" example:"single_download"`
	ExpiresAt time.Time `json:"expires_at"`
}

Link is a minted link.

type Options

type Options struct {
	// Master is the browser-session signing key the link key is derived from.
	Master []byte
	// Base is the platform's public address (portal.public_base_url).
	Base    string
	Store   Store
	Objects resource.S3Client
	Bucket  string
	// Uses records single downloads (NewUses).
	Uses Uses
}

Options is what a Service is made from.

type Service

type Service struct {

	// Now is the clock; nil is time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

Service mints links and serves the files they name.

func New

func New(o Options) *Service

New is the service o describes, nil when it has no signing key, public address, store or record of single downloads: a deployment that cannot sign, name or count a link hands out none.

func (*Service) Mint

func (s *Service) Mint(res *resource.Resource, expires string) (Link, error)

Mint returns a link to res of the kind expires names, a single download when it names none. The caller has already checked that the person asking can read res.

func (*Service) Mount

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

Mount registers the route links are served on. It takes no session or key: the link is the credential.

type Store

type Store interface {
	Get(ctx context.Context, id string) (*resource.Resource, error)
}

Store reads the resource a link names.

type Uses

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

Uses records single downloads (postgresUses).

func NewUses

func NewUses(db *sql.DB) Uses

NewUses is the record over db, nil (an empty Uses, so New hands out no links) without one.

Jump to

Keyboard shortcuts

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