-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetMusic.cs
108 lines (97 loc) · 3.42 KB
/
GetMusic.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
using System;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace dmc3music
{
public partial class GetMusic : Form
{
public GetMusic()
{
InitializeComponent();
}
private void RecenterLabel1()
{
label1.Left = ((Width - label1.Width) / 2) - 10;
}
private static readonly WebClient wc = new WebClient();
private static readonly ManualResetEvent handle = new ManualResetEvent(true);
private void GetMusic_Load(object sender, EventArgs e)
{
if (Directory.Exists("tracks"))
{
if (Directory.GetFiles("tracks").Length >= 70)
{
label1.Text = "Already have the tracks folder";
RecenterLabel1();
return;
}
else
{
Directory.Delete("tracks", true);
}
}
try
{
progressBar1.Visible = true;
wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
wc.DownloadFileAsync(new Uri(@"https://github.com/644/dmc3music/releases/download/tracks/tracks.zip"), "tracks.zip");
handle.WaitOne();
}
catch
{
label1.Text = "Problem downloading the tracks zipfile";
RecenterLabel1();
}
}
public string ReadableSize(long fileSizeInBytes)
{
string[] suffixes = { "B", "KiB", "MiB" };
double sizeResult = fileSizeInBytes * 1.0;
int suffixIndex = 0;
while (sizeResult > 1024 && suffixIndex < suffixes.Length)
{
sizeResult /= 1024;
suffixIndex++;
}
return $"{sizeResult:0.00} {suffixes[suffixIndex]}";
}
private void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (e.ProgressPercentage >= 0 && e.ProgressPercentage <= 100)
{
string readRecv = ReadableSize(e.BytesReceived);
string maxRecv = ReadableSize(e.TotalBytesToReceive);
label1.Text = $"Downloading {readRecv} of {maxRecv} ({e.ProgressPercentage}%)";
RecenterLabel1();
progressBar1.Value = e.ProgressPercentage;
}
}
private void WcOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
label1.Text = "Download complete";
RecenterLabel1();
try
{
ZipFile.ExtractToDirectory("tracks.zip", System.AppDomain.CurrentDomain.BaseDirectory);
File.Delete("tracks.zip");
progressBar1.Visible = false;
label1.Text = "Successfully downloaded and extracted the tracks";
RecenterLabel1();
}
catch
{
label1.Text = "Unable to extract the tracks zipfile";
RecenterLabel1();
}
}
handle.Set();
}
}
}