-
Notifications
You must be signed in to change notification settings - Fork 34
/
Download.cs
176 lines (153 loc) · 6.25 KB
/
Download.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Downloader;
using System.ComponentModel;
using YuukiPS_Launcher.Utils;
using YuukiPS_Launcher.Yuuki;
namespace YuukiPS_Launcher
{
public partial class Download : Form
{
private readonly string setDownload = "";
private readonly string setFolder = "";
private DownloadService? dl = null;
public Download(string urlDownload = "", string folderDownload = "")
{
setDownload = urlDownload;
setFolder = folderDownload;
InitializeComponent();
}
private void btDownload_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(setDownload))
{
Logger.Error("Download", "Download failed: No URL provided");
MessageBox.Show("Download failed because no URL was found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Directory.Exists(Path.GetDirectoryName(setFolder)))
{
Logger.Error("Download", $"Download failed: Folder not found or inaccessible - {setFolder}");
MessageBox.Show("Can't save file because the destination folder can't be found or accessed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (File.Exists(setFolder))
{
Logger.Info("Download", $"File old found {setFolder} remove for redownload?");
File.Delete(setFolder);
}
btDownload.Enabled = false;
GetNameDownload.Text = setDownload;
if (dl == null)
{
var downloadOpt = new DownloadConfiguration()
{
ChunkCount = 8, // file parts to download, default value is 1
//OnTheFlyDownload = true, // caching in-memory or not? default values is true
ParallelDownload = true // download parts of file as parallel or not. Default value is false
};
dl = new DownloadService(downloadOpt);
dl.DownloadStarted += Dl_DownloadStarted;
dl.DownloadFileCompleted += Dl_DownloadFileCompleted;
dl.DownloadProgressChanged += Dl_DownloadProgressChanged;
dl.ChunkDownloadProgressChanged += Dl_ChunkDownloadProgressChanged;
try
{
dl.DownloadFileTaskAsync(setDownload, setFolder);
}
catch (Exception ek)
{
MessageBox.Show($"Failed downloading Data: " + ek.Message, "Oh Snap!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
else
{
MessageBox.Show($"Error Download", "Oh Snap!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void Dl_DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
{
btDownload.Invoke(delegate
{
btDownload.Enabled = true;
});
GetNumDownload.Invoke(delegate
{
GetNumDownload.Text = "Done";
});
DialogResult = DialogResult.OK;
}
private void Dl_ChunkDownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
{
//Console.WriteLine($@"Update {e.ReceivedBytes} of {e.TotalBytesToReceive}");
}
private void Dl_DownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
{
if (dl == null || dl.IsCancelled) return;
double nonZeroSpeed = e.BytesPerSecondSpeed + 0.0001;
int estimateTime = (int)((e.TotalBytesToReceive - e.ReceivedBytesSize) / nonZeroSpeed);
bool isMinutes = estimateTime >= 60;
string timeLeftUnit = "seconds";
if (isMinutes)
{
timeLeftUnit = "minutes";
estimateTime /= 60;
}
if (estimateTime < 0)
{
estimateTime = 0;
timeLeftUnit = "unknown";
}
else
{
DLBar.Invoke(delegate
{
DLBar.Value = (int)e.ProgressPercentage;
});
}
string bytesReceived = Tool.CalcMemoryMensurableUnit(e.ReceivedBytesSize);
string totalBytesToReceive = Tool.CalcMemoryMensurableUnit(e.TotalBytesToReceive);
GetNumDownload.Invoke(delegate
{
GetNumDownload.Text = $"{bytesReceived} of {totalBytesToReceive} | {estimateTime} {timeLeftUnit} left | Speed: {Tool.CalcMemoryMensurableUnit(e.BytesPerSecondSpeed)}/s";
});
}
private void Dl_DownloadStarted(object? sender, DownloadStartedEventArgs e)
{
Logger.Info("Download", $"Starting download - URL: {setDownload}, Destination: {setFolder}");
}
private async void BTCancel_Click(object sender, EventArgs e)
{
if (dl != null)
{
// dl.CancelAsync();
// dl.Dispose();
try
{
btCancel.Enabled = false;
btDownload.Enabled = false;
GetNumDownload.Text = "Canceling...";
dl.CancelAsync();
await Task.Delay(1000); // give some time to cancel
dl.Dispose();
dl = null;
GetNumDownload.Text = "Canceled";
DLBar.Value = 0;
}
catch (Exception ex)
{
Logger.Error("Download", $"Error during canceling download: {ex.Message}");
MessageBox.Show($"Error during canceling download: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btCancel.Enabled = true;
btDownload.Enabled = true;
}
}
DialogResult = DialogResult.Cancel;
}
private void Download_Load(object sender, EventArgs e)
{
btDownload.PerformClick();
}
}
}