Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
63 lines
3.1 KiB
C#
63 lines
3.1 KiB
C#
namespace Febe.Core.Collection;
|
|
|
|
/// <summary>How a file download ended.</summary>
|
|
public enum FetchOutcome
|
|
{
|
|
/// <summary>All bytes were written to the destination.</summary>
|
|
Completed,
|
|
|
|
/// <summary>The server answered with an error status (e.g. an expired signed address, spec §6.5 point 5).</summary>
|
|
HttpError,
|
|
|
|
/// <summary>The request failed before a response (network error, refused by the browser).</summary>
|
|
NetworkError,
|
|
|
|
/// <summary>The file exceeded the maximum size allowed by the caller; the destination holds a partial copy.</summary>
|
|
TooLarge,
|
|
|
|
/// <summary>The address is not on an allowed host (only CDN addresses may be downloaded).</summary>
|
|
HostNotAllowed,
|
|
}
|
|
|
|
/// <summary>Result of a download. Never contains the address: it carries signatures (spec §13.2).</summary>
|
|
/// <param name="Outcome">How it ended.</param>
|
|
/// <param name="Bytes">Bytes written to the destination.</param>
|
|
/// <param name="HttpStatus">HTTP status code, when a response arrived.</param>
|
|
/// <param name="Error">Short error description without the address, when it failed.</param>
|
|
public sealed record FetchResult(FetchOutcome Outcome, long Bytes, int? HttpStatus, string? Error)
|
|
{
|
|
public bool Succeeded => Outcome == FetchOutcome.Completed;
|
|
|
|
/// <summary>
|
|
/// Bytes that crossed the network when they are known to be more than <see cref="Bytes"/>: 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 <see cref="Bytes"/>.
|
|
/// </summary>
|
|
public long? TransferredBytes { get; init; }
|
|
|
|
/// <summary>What the daily data budget must count for this download: the larger of written and transferred.</summary>
|
|
public long BudgetBytes => Math.Max(Bytes, TransferredBytes ?? 0);
|
|
}
|
|
|
|
/// <summary>Options of a single download.</summary>
|
|
/// <param name="MaxBytes">
|
|
/// Abort with <see cref="FetchOutcome.TooLarge"/> past this size; null for no limit. Through the browser the limit
|
|
/// saves disk, not network: <c>Network.loadNetworkResource</c> loads the whole resource before handing out its stream.
|
|
/// </param>
|
|
/// <param name="Progress">Receives the number of bytes written so far.</param>
|
|
public sealed record FetchOptions(long? MaxBytes = null, IProgress<long>? Progress = null);
|
|
|
|
/// <summary>
|
|
/// Downloads one file indicated by the page into a stream (spec §6.5). The preferred implementation goes through
|
|
/// the browser (<c>Network.loadNetworkResource</c> + <c>IO.read</c>); a fallback through <c>HttpClient</c> to CDN hosts
|
|
/// only may be added if M0 shows the first one does not work with the real CDN (H7).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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 <see cref="OperationCanceledException"/>.
|
|
/// </remarks>
|
|
public interface IFileFetcher
|
|
{
|
|
Task<FetchResult> FetchAsync(Uri address, Stream destination, FetchOptions? options, CancellationToken ct);
|
|
}
|