RunicUpload class

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

public sealed class RunicUpload

A file the page uploaded to the Window's host, which a command or setter receives as its input (Views protocol 4, feature blobs/1).

Remarks

The page streams the file over HTTP with client.upload(file), which resolves with a BlobRef, and passes that reference to a command whose parameter is a RunicUpload. The bytes never cross as JSON, so the Bridge input limit does not apply; the Window's upload limits do (WindowContentSessionOptions.MaximumUploadBytes). [RelayCommand] private async Task AttachAsync(RunicUpload file, CancellationToken cancellationToken) { await file.SaveToFileAsync(Path.Combine(attachments, Guid.NewGuid().ToString("N")), cancellationToken: cancellationToken); }

An upload decodes only on the Window and the page connection that uploaded it, and only until it is claimed: the first RunicUpload.OpenReadAsync, RunicUpload.SaveToFileAsync or RunicUpload.Discard takes it out of the Window, and a later request that names it is rejected as invalid input. An unclaimed upload is deleted when its connection closes, when the Window closes, or 10 minutes after it arrived. The host stores uploads in a new folder of the Window that only the user can access, and reads them through the file it created, never through its path again.

Everything about an upload comes from the page and is untrusted: its content, RunicUpload.ContentType and RunicUpload.FileName. Check the content before you use it, and never use the file name as a path.

Properties

ContentType

public string ContentType { get; }

The media type the page reported, or application/octet-stream. Untrusted.

FileName

public string FileName { get; }

The file name the page reported, reduced to a file name: no directories, path separators or control characters, at most 255 characters, and upload when nothing is left. Untrusted.

Id

public string Id { get; }

The upload's id on the wire: the id of the page's BlobRef.

IsClaimed

public bool IsClaimed { get; }

Whether the upload was claimed, by this instance or by another request that named it.

Length

public long Length { get; }

The number of bytes the host received.

Methods

Discard

public void Discard()

Claims the upload and deletes its content without reading it. Does nothing when it was claimed already.

Remarks

Discard, not Release as on RunicBlob.Release: the ViewModel refuses what the page sent and its bytes are gone, while a released blob is content the ViewModel served and still has.

FromBytes

public static RunicUpload FromBytes(ReadOnlyMemory<byte> content, string fileName = "upload", string contentType = "application/octet-stream")

Creates an upload of bytes in memory, for tests of a ViewModel without a Window. A Window's uploads come from the page; RunicWindowTestHost.UploadAsync simulates one.

Parameters

content

The content.

fileName

The file name, reduced as an uploaded one is.

contentType

The media type.

OpenReadAsync

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

Claims the upload and opens its content. Disposing the stream deletes the content.

Parameters

cancellationToken

Cancels opening.

Returns

A readable, seekable stream of RunicUpload.Length bytes.

Exceptions

InvalidOperationException

The upload was already claimed, or its Window closed.

SaveToFileAsync

public ValueTask SaveToFileAsync(string path, bool overwrite = false, CancellationToken cancellationToken = default)

Claims the upload and moves its content to path: a rename when the path is on the volume of the host's upload folder, otherwise a copy, so a large upload is not read through the application.

Parameters

path

The destination's full path.

overwrite

Whether an existing file is replaced.

cancellationToken

Cancels the move before it starts, and a copy from the upload's own handle while it runs; a rename or the operating system's copy of a move to another volume is not interrupted once it started. A cancelled copy deletes its partial destination. The upload is claimed and its content deleted whether or not it was cancelled.

Exceptions

InvalidOperationException

The upload was already claimed, or its Window closed.

IOException

The destination exists and overwrite is false, or the move failed.