imageload

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 23 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 imageload fetches image bytes for the native backends: https and http, file paths and file: URIs, data: URIs, "asset:" paths under the app's resources and bytes the app holds. Requests for the same URI share one fetch, the bytes stay in a least-recently-used cache in memory, and downloads in a disk cache under HTTP's caching rules (UseDiskCache).

Index

Constants

CacheBytes is the default byte budget of the in-memory encoded image cache.

View Source
const DiskCacheBytes = 256 << 20

DiskCacheBytes bounds the disk cache.

Variables

View Source
var HTTP func(ctx context.Context, url string, header Header, progress Progress) (*Response, error)

HTTP downloads http(s) sources: it gets url with header, calling progress as the body comes in, and returns the response whatever its status. Each native backend sets it to the system's client (URLSession, WinHTTP, libsoup, the Android runtime's HttpURLConnection) as it starts, so apps link no Go HTTP and TLS stack for their images, and downloads follow the system's proxies, certificates and policies such as App Transport Security.

View Source
var HTTPToFile func(ctx context.Context, url string, header Header, dst string, progress Progress) (*Response, error)

HTTPToFile streams an http(s) response body directly to dst on disk, calling progress as bytes are written, and returns the response status and headers with Body empty. Backends that install update packages set it alongside HTTP so large downloads do not buffer whole files in memory.

View Source
var ResourceRoot = func() string {
	exe, err := os.Executable()
	if err != nil {
		return "."
	}
	if runtime.GOOS == "darwin" {
		return filepath.Join(filepath.Dir(exe), "..", "Resources")
	}
	return filepath.Dir(exe)
}()

ResourceRoot is the directory "asset:" paths resolve against: the package's resource root, Contents/Resources in a macOS bundle and the executable's directory elsewhere, where Windows and Linux packages put their resources. Android reads them from its APK instead.

Functions

func CacheLimit

func CacheLimit() int64

CacheLimit returns the current in-memory encoded image byte budget.

func CacheSize

func CacheSize() int64

CacheSize returns the total encoded image bytes currently cached in memory, including kept entries.

func CanonicalKey

func CanonicalKey(key string) string

CanonicalKey is key with its first letter and those after a hyphen upper-cased and the rest lower-cased, as net/textproto's CanonicalMIMEHeaderKey makes it: "etag" and "ETag" are "Etag".

func Copy

func Copy(w io.Writer, r io.Reader, total int64, progress Progress) (int64, error)

Copy streams r into w to its end, total bytes long or 0 when unknown, calling progress as bytes are written: what a streaming HTTP client writes a body to disk with.

func Info

func Info(it *driver.MountItem, size func(data []byte) (driver.Size, error)) driver.Event

Info answers an ItemImageInfo: it loads the item's source, which keeps its bytes in the cache for the views that show it next, and returns the EventImageInfo with the size read by size from the bytes, or the error. It blocks; call it on a goroutine of its own.

func Keep

func Keep(owner any, key string)

Keep pins key in the in-memory encoded image cache on behalf of owner so it is not evicted while kept. Passing nil owner tracks an unowned pin.

func Load

func Load(ctx context.Context, src driver.ImageSource, progress Progress) ([]byte, error)

Load returns src's bytes. It blocks; call it on a goroutine of its own. Cancelling ctx stops waiting, and stops the fetch once nobody waits.

func ReadAll

func ReadAll(r io.Reader, total int64, progress Progress) ([]byte, error)

ReadAll reads r to its end, total bytes long or 0 when unknown, calling progress as it goes: what an HTTP client reads a body with.

func ReadAsset

func ReadAsset(ctx context.Context, path string) ([]byte, error)

ReadAsset reads the app's bundled file from its native resource directory.

func ReadResource

func ReadResource(path string) ([]byte, error)

ReadResource reads one of the app's resources by its slash-separated path, as "asset:" sources do.

func Release

func Release(owner any, key string)

Release releases one pin of key held by owner, immediately trimming excess unkept entries once key is no longer pinned.

func ReleaseOwner

func ReleaseOwner(owner any)

ReleaseOwner releases every pin held by owner (for example when a surface closes) and trims any newly unkept excess entries.

func SetCacheLimit

func SetCacheLimit(maxBytes int64)

SetCacheLimit sets the maximum number of unkept encoded image bytes kept in memory. Negative values are clamped to zero. Excess unkept entries are evicted immediately in least-recently-used order while kept entries remain pinned until released.

func UnkeptCacheSize

func UnkeptCacheSize() int64

UnkeptCacheSize returns the unkept encoded image bytes currently cached in memory.

func UseDiskCache

func UseDiskCache(cacheDir string)

UseDiskCache keeps downloaded images in an images directory under cacheDir, the app's cache directory, at most DiskCacheBytes; "" keeps them in memory only. Backends call it as they start.

Types

type Header map[string][]string

Header holds HTTP headers by their canonical keys (CanonicalKey), as net/http's Header does.

func (Header) Add

func (h Header) Add(key, value string)

Add appends value to key's values.

func (Header) Get

func (h Header) Get(key string) string

Get returns key's first value, "" when it has none.

func (Header) Set

func (h Header) Set(key, value string)

Set replaces key's values with value.

func (Header) Values

func (h Header) Values(key string) []string

Values returns key's values.

type Progress

type Progress func(loaded, total int64)

Progress receives bytes loaded and the total, 0 when unknown.

type Response

type Response struct {
	Status int
	Header Header
	Body   []byte
}

Response is what HTTP returns: the status, the headers and the whole body.

Directories

Path Synopsis
Package imageloadtest checks a backend's imageload.HTTP against a local server, so each backend's tests hold its system client to the same behavior: status, headers both ways, the body, progress and cancelling.
Package imageloadtest checks a backend's imageload.HTTP against a local server, so each backend's tests hold its system client to the same behavior: status, headers both ways, the body, progress and cancelling.

Jump to

Keyboard shortcuts

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