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
modelNamestringName 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.
downloadUrlsstring[]One or more URLs to try downloading from (tried in order)
cacheDirectorystringDirectory to cache downloaded models. Defaults to local app data.
loggerILoggerOptional logger
sha256stringOptional 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.
maxDownloadByteslongMaximum allowed download size in bytes (validated against Content-Length and the running byte count). Must be positive; defaults to DefaultMaxDownloadBytes (2 GiB).
httpClientHttpClientOptional 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
modelNamecontains path traversal or invalid characters, no download URL is provided,sha256is malformed, ormaxDownloadBytesis not positive.
Fields
DefaultMaxDownloadBytes
Default cap applied when maxDownloadBytes is not specified (2 GiB).
public const long DefaultMaxDownloadBytes = 2147483648
Field Value
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
cancellationTokenCancellationTokenCancellation token for the operation