Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fxsth committed Jan 17, 2023
1 parent 2f791af commit e6f2912
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions Web/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class DownloadService : IDownloadService
private readonly IAsyncPolicy<int> _resilientStreamPolicy;
private readonly ILogger _logger;

private readonly Collection<DownloadElement> _pendingDownloads;
private bool _isDownloading;

public DownloadService(HttpClient httpClient, IServiceScopeFactory scopeFactory, ILogger<DownloadService> logger)
{
_httpClient = httpClient;
Expand All @@ -30,8 +33,6 @@ public DownloadService(HttpClient httpClient, IServiceScopeFactory scopeFactory,
});
}

private bool _isDownloading;

public IReadOnlyCollection<DownloadElement> GetPendingDownloads()
{
return _pendingDownloads;
Expand All @@ -48,15 +49,7 @@ public IReadOnlyCollection<DownloadElement> GetAll()

return returnList;
}

public event EventHandler<DownloadElement>? TaskStarted;
public event EventHandler? AllTasksFinished;

public bool IsRunning => _isDownloading;
public string TaskName => "File Download";

private readonly Collection<DownloadElement> _pendingDownloads;


private async Task<DownloadElement> CreateDownloadElement(string key, ElementType elementType)
{
using (var scope = _scopeFactory.CreateScope())
Expand Down Expand Up @@ -172,12 +165,6 @@ public Task CancelDownload(string mediaKey)
return Task.CompletedTask;
}

private void AddToPendingDownloads(IEnumerable<DownloadElement> toDownload)
{
foreach (DownloadElement element in toDownload)
AddToPendingDownloads(element);
}

private void AddToPendingDownloads(DownloadElement toDownload)
{
if (_pendingDownloads.All(x => x.MediaKey != toDownload.MediaKey))
Expand All @@ -203,7 +190,6 @@ private async Task DownloadQueue()
{
_isDownloading = true;
DownloadElement downloadElement = _pendingDownloads.First();
TaskStarted?.Invoke(this, downloadElement);
_logger.LogInformation("Start download of next element in queue: {0}", downloadElement.Name);

await Preprocess(downloadElement);
Expand All @@ -218,7 +204,6 @@ private async Task DownloadQueue()
_logger.LogInformation("No more elements in download queue.");

_isDownloading = false;
AllTasksFinished?.Invoke(this, new EventArgs());
}

private async Task Preprocess(DownloadElement downloadElement)
Expand All @@ -241,7 +226,6 @@ private async Task DownloadFile(DownloadElement downloadElement)
{
try
{
// downloadElement.CancellationTokenSource.CancelAfter(60000);
Stream response = await _httpClient.GetStreamAsync(downloadElement.Uri,
downloadElement.CancellationTokenSource.Token);

Expand All @@ -268,7 +252,6 @@ private static async Task CopyToAsync(Stream source, Stream destination, Downloa
CancellationToken cancellationToken = downloadElement.CancellationTokenSource.Token;
var buffer = new byte[bufferSize];
int bytesRead;
// DownloadProgress downloadProgress = new DownloadProgress() { Total = source.Length, Downloaded = 0 };
while ((bytesRead =
await policy.ExecuteAsync(() => source.ReadAsync(buffer, 0, buffer.Length, cancellationToken))) >
0)
Expand Down

0 comments on commit e6f2912

Please sign in to comment.