Table of Contents

Class DownloadingModelProvider

Namespace
InferenceEngine.Core.ModelProviders
Assembly
InferenceEngine.Core.dll

Model provider that downloads models from URLs with local caching. If the model already exists in the cache, returns the cached path immediately. Downloads are hardened (security assessment MEDIUM-1): model names are validated against path traversal, files are downloaded atomically via a temp file, an optional SHA-256 checksum is enforced, a size cap aborts runaway downloads, and redirects are capped.

public class DownloadingModelProvider : IModelProvider
Inheritance
DownloadingModelProvider
Implements
Inherited Members

Constructors

DownloadingModelProvider(string, string[], string?, ILogger?, string?, long, HttpClient?)

Creates a provider that downloads models from URLs with caching.

public DownloadingModelProvider(string modelName, string[] downloadUrls, string? cacheDirectory = null, ILogger? logger = null, string? sha256 = null, long maxDownloadBytes = 2147483648, HttpClient? httpClient = null)

Parameters

modelName string

Name of the model file (e.g., "yolov8n.onnx"). Must be a plain file name: directory separators, rooted paths, ".." segments, and invalid filename characters are rejected.

downloadUrls string[]

One or more URLs to try downloading from (tried in order)

cacheDirectory string

Directory to cache downloaded models. Defaults to local app data.

logger ILogger

Optional logger

sha256 string

Optional expected SHA-256 of the model file as 64 lowercase hex characters. When set, the downloaded file is verified against it and a mismatch rejects (and deletes) the download. When null, the download is accepted but logged as unverified.

maxDownloadBytes long

Maximum allowed download size in bytes (validated against Content-Length and the running byte count). Must be positive; defaults to DefaultMaxDownloadBytes (2 GiB).

httpClient HttpClient

Optional HTTP client to use (mainly for tests). Defaults to a shared client with automatic redirects capped at 5.

Exceptions

ArgumentNullException

A required argument is null.

ArgumentException

modelName contains path traversal or invalid characters, no download URL is provided, sha256 is malformed, or maxDownloadBytes is not positive.

Fields

DefaultMaxDownloadBytes

Default cap applied when maxDownloadBytes is not specified (2 GiB).

public const long DefaultMaxDownloadBytes = 2147483648

Field Value

long

Methods

GetModelPathAsync(CancellationToken)

Gets the local path to the model file. Implementations should ensure the model is available at the returned path (downloading if necessary) before returning.

public Task<string> GetModelPathAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token for the operation

Returns

Task<string>

Absolute path to the .onnx model file