markus / MarkusAutoUpdate / src / NetSparkle / Downloaders / WebClientFileDownloader.cs @ c7555c83
이력 | 보기 | 이력해설 | 다운로드 (3.43 KB)
1 |
using NetSparkleUpdater.Interfaces; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.ComponentModel; |
5 |
using System.IO; |
6 |
using System.Net; |
7 |
using System.Net.Http; |
8 |
using System.Text; |
9 |
using System.Threading.Tasks; |
10 |
|
11 |
namespace NetSparkleUpdater.Downloaders |
12 |
{ |
13 |
/// <summary> |
14 |
/// Class that downloads files from the internet and reports |
15 |
/// progress on those files being downloaded. Uses a WebClient |
16 |
/// object as its main method for downloading. |
17 |
/// </summary> |
18 |
public class WebClientFileDownloader : IUpdateDownloader, IDisposable |
19 |
{ |
20 |
private WebClient _webClient; |
21 |
|
22 |
/// <summary> |
23 |
/// Default constructor for the web client file downloader. |
24 |
/// Uses default credentials and default proxy. |
25 |
/// </summary> |
26 |
public WebClientFileDownloader() |
27 |
{ |
28 |
_webClient = new WebClient |
29 |
{ |
30 |
UseDefaultCredentials = true, |
31 |
Proxy = { Credentials = CredentialCache.DefaultNetworkCredentials }, |
32 |
}; |
33 |
_webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; |
34 |
_webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted; |
35 |
} |
36 |
|
37 |
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) |
38 |
{ |
39 |
DownloadProgressChanged?.Invoke(sender, |
40 |
new Events.ItemDownloadProgressEventArgs(e.ProgressPercentage, e.UserState, e.BytesReceived, e.TotalBytesToReceive)); |
41 |
} |
42 |
|
43 |
private void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) |
44 |
{ |
45 |
DownloadFileCompleted?.Invoke(sender, e); |
46 |
} |
47 |
|
48 |
/// <inheritdoc/> |
49 |
public bool IsDownloading |
50 |
{ |
51 |
get => _webClient.IsBusy; |
52 |
} |
53 |
|
54 |
/// <inheritdoc/> |
55 |
public event DownloadProgressEvent DownloadProgressChanged; |
56 |
/// <inheritdoc/> |
57 |
public event AsyncCompletedEventHandler DownloadFileCompleted; |
58 |
|
59 |
/// <inheritdoc/> |
60 |
public void Dispose() |
61 |
{ |
62 |
_webClient.Dispose(); |
63 |
} |
64 |
|
65 |
/// <inheritdoc/> |
66 |
public void StartFileDownload(Uri uri, string downloadFilePath) |
67 |
{ |
68 |
_webClient.DownloadFileAsync(uri, downloadFilePath); |
69 |
} |
70 |
|
71 |
/// <inheritdoc/> |
72 |
public void CancelDownload() |
73 |
{ |
74 |
_webClient.CancelAsync(); |
75 |
} |
76 |
|
77 |
/// <inheritdoc/> |
78 |
public async Task<string> RetrieveDestinationFileNameAsync(AppCastItem item) |
79 |
{ |
80 |
var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; |
81 |
try |
82 |
{ |
83 |
using (var response = |
84 |
await httpClient.SendAsync(new HttpRequestMessage |
85 |
{ |
86 |
Method = HttpMethod.Head, |
87 |
RequestUri = new Uri(item.DownloadLink) |
88 |
}).ConfigureAwait(false)) |
89 |
{ |
90 |
if (response.IsSuccessStatusCode) |
91 |
{ |
92 |
//var totalBytes = response.Content.Headers.ContentLength; // TODO: Use this value as well for a more accurate download %? |
93 |
string destFilename = response.RequestMessage?.RequestUri?.LocalPath; |
94 |
|
95 |
return Path.GetFileName(destFilename); |
96 |
} |
97 |
return null; |
98 |
} |
99 |
} |
100 |
catch |
101 |
{ |
102 |
} |
103 |
return null; |
104 |
} |
105 |
} |
106 |
} |