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 CanonicalKey(key string) string
- func Info(it *driver.MountItem, size func(data []byte) (driver.Size, error)) driver.Event
- func Load(ctx context.Context, src driver.ImageSource, progress Progress) ([]byte, error)
- func ReadAll(r io.Reader, total int64, progress Progress) ([]byte, error)
- func ReadResource(path string) ([]byte, error)
- func UseDiskCache(cacheDir string)
- type Header
- type Progress
- type Response
Constants ¶
const CacheBytes = 32 << 20
CacheBytes bounds the 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 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 ¶
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 ¶
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 ¶
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 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. |