-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Downloader.cs
103 lines (92 loc) · 3.44 KB
/
Downloader.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
using System;
using System.Threading.Tasks;
using XVLauncher.Resources;
namespace XVLauncher
{
/// <summary>
/// Abstract class that provides the template method Download(). It is implemented by <see cref="WebDownloader"/> and <see cref="MegaDownloader"/>.
/// </summary>
abstract class Downloader
{
protected readonly string url;
protected readonly MainWindow window;
/// <summary>
/// Constructor method.
/// </summary>
/// <param name="window"><see cref="MainWindow"/> instance where are placed GUI elements to be updated to show progress.</param>
/// <param name="url"></param>
public Downloader(MainWindow window, string url)
{
this.url = url;
this.window = window;
}
/// <summary>
/// Template method that only handle exceptions and do some simples GUI changes.<br/>
/// Real Download implementation is delegated to sublasses that must implement the abstract method <see cref="DownloadImplementation"/>.
/// </summary>
/// <returns></returns>
public async Task Download()
{
try
{
await DownloadImplementation();
}
catch (Exception ex)
{
window.infoLabel.Content = String.Format(Properties.Langs.Lang.unable_download, ex.Message);
PhpManager.ReportError(String.Format("Error in {0}.DownloadImplementation: {1}", GetType().Name, ex.Message));
window.UpdateBarProgress(0);
return;
}
window.infoLabel.Content = Properties.Langs.Lang.download_completed;
}
/// <summary>
/// Abstract method to be implemented by subclasses, that must provide download implementation, possibly asynchronously. <br/>
/// It is called by the template method <see cref="Download"/>
/// </summary>
/// <returns></returns>
protected abstract Task DownloadImplementation();
public long GetFileSize(string url)
{
long size = -1;
var req = System.Net.WebRequest.Create(url);
req.Method = "HEAD";
using (var resp = req.GetResponse()) //WebClient
{
if (long.TryParse(resp.Headers.Get("Content-Length"), out long ContentLenght))
size = ContentLenght;
}
return size;
}
protected void UpdateUnknownProgress(long bytes)
{
var mb = bytes / 1048576f;
var unit = "MB";
if (mb > 1024)
{
mb = mb / 1024f;
unit = "GB";
}
window.Dispatcher.Invoke(() =>
{
window.infoLabel.Content = String.Format(Properties.Langs.Lang.download_progress_unknown, mb, unit, "2GB");
});
}
protected void ShowProgress(double x)
{
window.Dispatcher.Invoke(() =>
{
window.infoLabel.Content = String.Format(Properties.Langs.Lang.download_progress, x);
window.UpdateBarProgress(x);
});
}
protected void ShowError(string error, bool buttonEnabled = false)
{
window.Dispatcher.Invoke(() =>
{
window.infoLabel.Content = error;
window.button.IsEnabled = buttonEnabled;
});
}
}
}