|
|
@ -1,6 +1,7 @@ |
|
|
|
using System.Diagnostics.CodeAnalysis; |
|
|
|
using System.Diagnostics.CodeAnalysis; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using AsyncAwaitBestPractices; |
|
|
|
using AsyncAwaitBestPractices; |
|
|
|
|
|
|
|
using NLog; |
|
|
|
using StabilityMatrix.Core.Helper; |
|
|
|
using StabilityMatrix.Core.Helper; |
|
|
|
using StabilityMatrix.Core.Models.FileInterfaces; |
|
|
|
using StabilityMatrix.Core.Models.FileInterfaces; |
|
|
|
using StabilityMatrix.Core.Models.Progress; |
|
|
|
using StabilityMatrix.Core.Models.Progress; |
|
|
@ -16,6 +17,8 @@ public class TrackedDownloadProgressEventArgs : EventArgs |
|
|
|
|
|
|
|
|
|
|
|
public class TrackedDownload |
|
|
|
public class TrackedDownload |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
|
|
|
|
|
|
|
|
|
|
|
[JsonIgnore] |
|
|
|
[JsonIgnore] |
|
|
|
private IDownloadService? downloadService; |
|
|
|
private IDownloadService? downloadService; |
|
|
|
|
|
|
|
|
|
|
@ -128,6 +131,7 @@ public class TrackedDownload |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new InvalidOperationException($"Download state must be inactive to start, not {ProgressState}"); |
|
|
|
throw new InvalidOperationException($"Download state must be inactive to start, not {ProgressState}"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Logger.Debug("Starting download {Download}", FileName); |
|
|
|
|
|
|
|
|
|
|
|
EnsureDownloadService(); |
|
|
|
EnsureDownloadService(); |
|
|
|
|
|
|
|
|
|
|
@ -138,34 +142,59 @@ public class TrackedDownload |
|
|
|
.ContinueWith(OnDownloadTaskCompleted); |
|
|
|
.ContinueWith(OnDownloadTaskCompleted); |
|
|
|
|
|
|
|
|
|
|
|
ProgressState = ProgressState.Working; |
|
|
|
ProgressState = ProgressState.Working; |
|
|
|
|
|
|
|
OnProgressStateChanged(ProgressState); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Resume() |
|
|
|
public void Resume() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ProgressState != ProgressState.Inactive) return; |
|
|
|
if (ProgressState != ProgressState.Inactive) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Logger.Warn("Attempted to resume download {Download} but it is not paused ({State})", FileName, ProgressState); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Logger.Debug("Resuming download {Download}", FileName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the temp file to get the current size |
|
|
|
|
|
|
|
var tempSize = 0L; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tempFile = DownloadDirectory.JoinFile(TempFileName); |
|
|
|
|
|
|
|
if (tempFile.Exists) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
tempSize = tempFile.Info.Length; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
EnsureDownloadService(); |
|
|
|
EnsureDownloadService(); |
|
|
|
|
|
|
|
|
|
|
|
downloadCancellationTokenSource = new CancellationTokenSource(); |
|
|
|
downloadCancellationTokenSource = new CancellationTokenSource(); |
|
|
|
downloadPauseTokenSource = new CancellationTokenSource(); |
|
|
|
downloadPauseTokenSource = new CancellationTokenSource(); |
|
|
|
|
|
|
|
|
|
|
|
downloadTask = StartDownloadTask(0, AggregateCancellationTokenSource.Token) |
|
|
|
downloadTask = StartDownloadTask(tempSize, AggregateCancellationTokenSource.Token) |
|
|
|
.ContinueWith(OnDownloadTaskCompleted); |
|
|
|
.ContinueWith(OnDownloadTaskCompleted); |
|
|
|
|
|
|
|
|
|
|
|
ProgressState = ProgressState.Working; |
|
|
|
ProgressState = ProgressState.Working; |
|
|
|
|
|
|
|
OnProgressStateChanged(ProgressState); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Pause() |
|
|
|
public void Pause() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ProgressState != ProgressState.Working) return; |
|
|
|
if (ProgressState != ProgressState.Working) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Logger.Warn("Attempted to pause download {Download} but it is not in progress ({State})", FileName, ProgressState); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Pausing download {Download}", FileName); |
|
|
|
downloadPauseTokenSource?.Cancel(); |
|
|
|
downloadPauseTokenSource?.Cancel(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Cancel() |
|
|
|
public void Cancel() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ProgressState is not (ProgressState.Working or ProgressState.Inactive)) return; |
|
|
|
if (ProgressState is not (ProgressState.Working or ProgressState.Inactive)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Logger.Warn("Attempted to cancel download {Download} but it is not in progress ({State})", FileName, ProgressState); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Cancelling download {Download}", FileName); |
|
|
|
downloadCancellationTokenSource?.Cancel(); |
|
|
|
downloadCancellationTokenSource?.Cancel(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|