RunicBlob class

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public sealed class RunicBlob

Binary content that a ViewModel shows or offers to the page, such as an image, a thumbnail or a download (Views protocol 4, feature blobs/1).

Remarks

Expose it as a state member, inside a DTO or collection item, or as a command or operation result. It crosses the Bridge as a blob value: a URL that the Window's host serves, with the content type, length and file name. The page uses the URL directly, in <img src>, <a href download> or fetch; the bytes never cross as JSON. [ObservableProperty] private RunicBlob? _avatar = RunicBlob.FromFile(avatarPath, "image/png");

The URL is a capability: it is unguessable, it works only on the Window it was issued for, and it is released when the state member stops holding the blob, when the route (the ViewModel's presentation) is released, or when the Window closes. A blob in a collection item or a result is released with its route. One RunicBlob instance has one URL per Window; create a new instance for new content, because the page and the browser cache assume that a URL's content never changes.

The host opens the content for each request, so a RunicBlob.FromFile or RunicBlob.FromStream blob is never buffered. A seekable stream also answers byte ranges, which audio and video elements need.

Properties

ContentType

public string ContentType { get; }

The media type the host serves the content with, such as image/png.

Disposition

public RunicBlobDisposition Disposition { get; }

Whether the browser shows the content or downloads it when the page navigates to its URL.

FileName

public string? FileName { get; }

The file name that a download saves the content as, and that the page receives as the blob's name; or null.

Length

public long? Length { get; }

The content's length in bytes, or null when it is known only once it is opened.

Methods

FromBytes

public static RunicBlob FromBytes(ReadOnlyMemory<byte> content, string contentType, string? fileName = null, RunicBlobDisposition disposition = RunicBlobDisposition.Inline)

Creates a blob of bytes in memory. The blob keeps the memory; do not change it afterwards.

Parameters

content

The content.

contentType

The media type, such as image/png.

fileName

The file name of a download, or null.

disposition

Whether a navigation to the URL shows or downloads the content.

Exceptions

ArgumentException

contentType is not a media type, or fileName is not a file name.

FromFile

public static RunicBlob FromFile(string path, string contentType, string? fileName = null, RunicBlobDisposition disposition = RunicBlobDisposition.Inline)

Creates a blob of a file, which the host opens for each request and streams. Its RunicBlob.Length is the file's length now; a request reads the file as it is then.

Parameters

path

The file's full path.

contentType

The media type, such as image/png.

fileName

The file name of a download, or null.

disposition

Whether a navigation to the URL shows or downloads the content.

Exceptions

ArgumentException

path is not fully qualified, or another argument is invalid.

FileNotFoundException

The file does not exist.

FromStream

public static RunicBlob FromStream(Func<CancellationToken, ValueTask<Stream>> open, string contentType, long? length = null, string? fileName = null, RunicBlobDisposition disposition = RunicBlobDisposition.Inline)

Creates a blob whose content open returns for each request. The host reads the stream to its end and disposes it; a seekable stream also answers byte ranges.

Parameters

open

Opens the content; called once for each request, on any thread. Its token is cancelled when the request ends.

contentType

The media type, such as image/png.

length

The content's length in bytes, when known; a stream of another length ends the response early.

fileName

The file name of a download, or null.

disposition

Whether a navigation to the URL shows or downloads the content.

OpenReadAsync

public ValueTask<Stream> OpenReadAsync(CancellationToken cancellationToken = default)

Opens the content, as the host does for each request.

Parameters

cancellationToken

Cancels opening.

Returns

A readable stream that the caller disposes.

Release

public void Release()

Stops serving this blob in every Window that serves it now: its URLs answer 404 from now on, whatever still holds it. Release a blob that a long-lived View no longer shows, such as a command result the page has downloaded; a state property's blob is released when the property changes, and every blob when its View or Window goes away.

Remarks

If the blob crosses the Bridge again, it gets a new URL.