imageload

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 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

View Source
const CacheBytes = 32 << 20

CacheBytes bounds the 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 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 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 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 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 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 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