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
- Variables
- func CacheLimit() int64
- func CacheSize() int64
- func CanonicalKey(key string) string
- func Copy(w io.Writer, r io.Reader, total int64, progress Progress) (int64, error)
- func Info(it *driver.MountItem, size func(data []byte) (driver.Size, error)) driver.Event
- func Keep(owner any, key string)
- func Load(ctx context.Context, src driver.ImageSource, progress Progress) ([]byte, error)
- func ReadAll(r io.Reader, total int64, progress Progress) ([]byte, error)
- func ReadAsset(ctx context.Context, path string) ([]byte, error)
- func ReadResource(path string) ([]byte, error)
- func Release(owner any, key string)
- func ReleaseOwner(owner any)
- func SetCacheLimit(maxBytes int64)
- func UnkeptCacheSize() int64
- func UseDiskCache(cacheDir string)
- type Header
- type Progress
- type Response
Constants ¶
const CacheBytes = int(driver.DefaultImageCacheLimit)
CacheBytes is the default byte budget of the in-memory encoded image cache.
const DiskCacheBytes = 256 << 20
DiskCacheBytes bounds the disk cache.
Variables ¶
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.
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ReadResource reads one of the app's resources by its slash-separated path, as "asset:" sources do.
func Release ¶
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 ¶
Header holds HTTP headers by their canonical keys (CanonicalKey), as net/http's Header does.
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. |