-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadTask.cs
154 lines (137 loc) · 4.91 KB
/
DownloadTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace DataLocalizer
{
public enum DownloadStatus
{
[Description("等待下载")]//添加后没开始下载
Pending,
[Description("下载中")]
Running,
[Description("已完成")]
Completed,
[Description("异常")]
Failed,
[Description("已取消")]
Canceled
}
public class DownloadTask : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Id { get; private set; }
public string Url { get; private set; }
public string FilePath { get; private set; }
private long downloadedBytes;
public long DownloadedBytes
{
get { return downloadedBytes; }
private set { downloadedBytes = value; OnPropertyChanged(); Progress = TotalBytes == 0 ? 0 : (double)DownloadedBytes / TotalBytes; }
}
private long totalBytes;
public long TotalBytes
{
get { return totalBytes; }
private set { totalBytes = value; OnPropertyChanged(); Progress = TotalBytes == 0 ? 0 : (double)DownloadedBytes / TotalBytes; }
}
private double progress;
public double Progress
{
get { return progress; }
private set { progress = value; OnPropertyChanged(); }
}
private DownloadStatus status;
public DownloadStatus Status
{
get { return status; }
set { status = value; OnPropertyChanged(); }
}
private HttpClient _client;
private CancellationTokenSource _cancellationTokenSource;
public event Action<DownloadTask> OnStatusChanged;
public DownloadTask(string url, string filePath, HttpClient client)
{
Id = Guid.NewGuid().ToString();
Url = url;
FilePath = filePath;
Status = DownloadStatus.Pending;
_client = client ?? throw new ArgumentNullException(nameof(client));
}
private void InitDirectory()
{
if (!Directory.Exists(Path.GetDirectoryName(FilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
}
/// <summary>
/// 开始
/// </summary>
public async Task StartAsync()
{
try
{
InitDirectory();
DownloadedBytes = 0;
_cancellationTokenSource = new CancellationTokenSource();
Status = DownloadStatus.Running;
OnStatusChanged?.Invoke(this);
var response = await _client.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead, _cancellationTokenSource.Token);
response.EnsureSuccessStatusCode();
TotalBytes = response.Content.Headers.ContentLength ?? 0;
using (var fileStream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (var httpStream = await response.Content.ReadAsStreamAsync())
{
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await httpStream.ReadAsync(buffer, 0, buffer.Length, _cancellationTokenSource.Token)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
DownloadedBytes += bytesRead;
OnStatusChanged?.Invoke(this);
}
}
if (DownloadedBytes == TotalBytes || TotalBytes == 0)
{
Status = DownloadStatus.Completed;
}
else
{
Status = DownloadStatus.Failed;
}
}
catch (OperationCanceledException)
{
Status = DownloadStatus.Canceled;
}
catch (Exception)
{
Status = DownloadStatus.Failed;
}
finally
{
OnStatusChanged?.Invoke(this);
}
}
/// <summary>
/// 取消
/// </summary>
public void Cancel()
{
if (Status == DownloadStatus.Running)
_cancellationTokenSource.Cancel();
else if (Status == DownloadStatus.Pending)
{
Status = DownloadStatus.Canceled;
_cancellationTokenSource?.Cancel();
OnStatusChanged?.Invoke(this);
}
}
}
}