namespace Febe.Core.Collection;
/// How a file download ended.
public enum FetchOutcome
{
/// All bytes were written to the destination.
Completed,
/// The server answered with an error status (e.g. an expired signed address, spec §6.5 point 5).
HttpError,
/// The request failed before a response (network error, refused by the browser).
NetworkError,
/// The file exceeded the maximum size allowed by the caller; the destination holds a partial copy.
TooLarge,
/// The address is not on an allowed host (only CDN addresses may be downloaded).
HostNotAllowed,
}
/// Result of a download. Never contains the address: it carries signatures (spec §13.2).
/// How it ended.
/// Bytes written to the destination.
/// HTTP status code, when a response arrived.
/// Short error description without the address, when it failed.
public sealed record FetchResult(FetchOutcome Outcome, long Bytes, int? HttpStatus, string? Error)
{
public bool Succeeded => Outcome == FetchOutcome.Completed;
///
/// Bytes that crossed the network when they are known to be more than : a file over the size
/// limit is loaded whole by the browser before the first byte can be read (its declared length), even though
/// only part of it, or nothing, is written. Null when it is .
///
public long? TransferredBytes { get; init; }
/// What the daily data budget must count for this download: the larger of written and transferred.
public long BudgetBytes => Math.Max(Bytes, TransferredBytes ?? 0);
}
/// Options of a single download.
///
/// Abort with past this size; null for no limit. Through the browser the limit
/// saves disk, not network: Network.loadNetworkResource loads the whole resource before handing out its stream.
///
/// Receives the number of bytes written so far.
public sealed record FetchOptions(long? MaxBytes = null, IProgress? Progress = null);
///
/// Downloads one file indicated by the page into a stream (spec §6.5). The preferred implementation goes through
/// the browser (Network.loadNetworkResource + IO.read); a fallback through HttpClient to CDN hosts
/// only may be added if M0 shows the first one does not work with the real CDN (H7).
///
///
/// The caller owns the destination: it writes to a temporary name, hashes and renames (spec §6.5 point 4).
/// Implementations never log the address. Cancellation throws .
///
public interface IFileFetcher
{
Task FetchAsync(Uri address, Stream destination, FetchOptions? options, CancellationToken ct);
}