Files
Beam/Beam.Downloaders/DownloadContext.cs
T
qwsdcvghyu89 7ed05abdb8 refactor: modularize Beam into new projects and interfaces
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders.
- Refactored existing classes into appropriate namespaces and projects.
- Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.).
- Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility.
- Removed deprecated classes like SourceLinkBuilder and StateChangerFactory.
- Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`.
- Consolidated shared logic under Beam.Abstractions.
2025-09-22 01:51:46 +10:00

75 lines
3.1 KiB
C#

using Beam.Abstractions;
using Beam.Models;
using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
namespace Beam.Downloaders {
//public delegate T HtmlTransformer<out T>(HtmlDocument doc);
//public delegate Task<T> AsyncHtmlTransformer<T>(HtmlDocument doc);
//public delegate Task<T> AsyncBinaryTransformer<T>(byte[] bin);
public class DownloadContext<RawType> {
private bool disposedValue;
public HttpClient Client { get; }
public HtmlWeb Web { get; }
public IProgress<IDownloadReport>? DownloadReporter { get; set; }
public IProgress<IRetryReport>? RetryReporter { get; set; }
public AsyncDownloadFailurePredicate<RawType>?[]? AsyncFailurePredicates { get; }
public TimeSpan TimeOut { get; set; }
public IEnumerable<string> Links { get; }
public CancellationToken CancellationToken { get; }
public DocumentCache Cache { get; private set; } = [];
public ILogger? DownloadLogger { get; set; }
public DownloadContext(HtmlWeb web,
HttpClient client,
IEnumerable<string> links,
CancellationToken cancellationToken = default,
IProgress<IDownloadReport>? downloadReporter = null,
IProgress<IRetryReport>? retryReporter = null,
AsyncDownloadFailurePredicate<RawType>?[]? asyncFailurePredicates = null,
TimeSpan? timeOut = null,
ILogger? downloadLogger = null) {
ArgumentNullException.ThrowIfNull(web, nameof(web));
ArgumentNullException.ThrowIfNull(links, nameof(links));
Web = web;
Client = client;
Links = links;
CancellationToken = cancellationToken;
DownloadReporter = downloadReporter;
RetryReporter = retryReporter;
AsyncFailurePredicates = asyncFailurePredicates;
TimeOut = timeOut ?? TimeSpan.FromMinutes(1);
DownloadLogger = downloadLogger;
}
protected virtual void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
// TODO: dispose managed state (managed objects)
Cache = null;
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~DownloadContext()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}