-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cs
More file actions
281 lines (259 loc) · 11.1 KB
/
FileHandler.cs
File metadata and controls
281 lines (259 loc) · 11.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Text.Json;
namespace Lamp
{
internal static class FileHandler
{
private static readonly HttpClient Client = new HttpClient();
public static readonly string LocalDirectory = AppDomain.CurrentDomain.BaseDirectory;
public static readonly string LampFilepath = Path.Combine(LocalDirectory, "Lamp.exe");
public static void DownloadZip(string downloadURL, string destinationPath)
{
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Add("User-Agent", "Genie Client Updater");
var response = Client.GetAsync(new Uri(downloadURL)).Result;
string directory = Path.GetDirectoryName(destinationPath);
if(!Directory.Exists(directory)) Directory.CreateDirectory(directory);
using (var zipFile = new FileStream(destinationPath, FileMode.Create))
{
response.Content.CopyToAsync(zipFile);
}
}
public static MemoryStream DownloadToMemoryStream(string downloadURL)
{
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Add("User-Agent", "Genie Client Updater");
var response = Client.GetAsync(new Uri(downloadURL)).Result;
MemoryStream memoryStream = new MemoryStream();
response.Content.CopyToAsync(memoryStream);
return memoryStream;
}
public static async Task<bool> AcquirePackageInMemory(string packageURL, string packageDestination)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.Write("The Updater is not currently supported for this system.");
return false;
}
try
{
MemoryStream zipFile = DownloadToMemoryStream(packageURL);
ZipArchive archive = new ZipArchive(zipFile);
int stripFromBeginning = 0;
if (archive.Entries.Count > 0 && archive.Entries[0].FullName.EndsWith("-main/"))
{
//if this is from a github repo it will be in a folder in the zip's root that ends -main/
//and we want to not extract that and extract from it so we need to strip it off the path
stripFromBeginning = archive.Entries[0].FullName.Length;
}
foreach (ZipArchiveEntry entry in archive.Entries)
{
string packageFile = Path.Combine(packageDestination, entry.FullName.Remove(0, stripFromBeginning).Replace("/", "\\"));
if (packageFile.ToUpper() == LampFilepath.ToUpper()) continue; //don't try to update Lamp, we're mid-execution.
if (entry.FullName.Length - stripFromBeginning > 0)
{
if (File.Exists(packageFile))
{
FileInfo existingFile = new FileInfo(packageFile);
if (existingFile.LastWriteTime != entry.LastWriteTime)
{
File.Delete(packageFile);
}
}
if (packageFile.EndsWith("\\"))
{
if(!Directory.Exists(packageFile)) Directory.CreateDirectory(packageFile);
}
else
{
if (!File.Exists(packageFile)) entry.ExtractToFile(packageFile);
}
}
}
return true;
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public static bool ArchiveExtensions(string directory, string archiveName, string extension)
{
try
{
string archiveFile = Path.Combine(directory, archiveName) + ".zip";
int archiveNumber = 0;
while(File.Exists(archiveFile))
{
string numberedArchiveFilename = Path.Combine(directory, archiveName) + $"({archiveNumber}).zip";
if (!File.Exists(numberedArchiveFilename)) archiveFile = numberedArchiveFilename;
archiveNumber++;
}
using (FileStream saveStream = new FileStream(archiveFile, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(saveStream, ZipArchiveMode.Create, true))
{
foreach (string file in Directory.GetFiles(directory, $"*.{extension}"))
{
archive.CreateEntryFromFile(file, Path.GetFileName(file));
}
}
}
return File.Exists(archiveFile);
}
catch
{
throw;
}
}
public static int ChangeAllExtensionsInDirectory(string directory, string oldExtension, string newExtension)
{
//returns the number of files changed
int changed = 0;
foreach (string file in Directory.GetFiles(directory, $"*.{oldExtension}"))
{
changed += ChangeExtension(new FileInfo(file), newExtension) ? 1 : 0;
}
return changed;
}
public static bool ChangeExtension(FileInfo file, string newExtension)
{
try
{
do { Thread.Sleep(10); } while (FileIsLocked(file));
string destinationFile = Path.Combine(file.DirectoryName, Path.GetFileNameWithoutExtension(file.FullName)) + $".{newExtension}";
bool overwrite = false;
if (File.Exists(destinationFile))
{
Console.WriteLine($"The file {Path.GetFileName(destinationFile)} already exists. Do you wish to overwrite it? This may result in a loss of data. It is advised that you do not. You should compare the individual files to determine which to keep.");
Console.WriteLine($"Enter Y to overwrite.");
Console.WriteLine(Fluff.Prompt);
string? response = Console.ReadLine();
overwrite = response.ToLower().StartsWith("y");
if (!overwrite) return false;
}
File.Move(file.FullName, destinationFile, overwrite);
return File.Exists(destinationFile);
}
catch
{
throw;
}
}
public static async Task<bool> LoadReleaseAssets(Release release)
{
if (!string.IsNullOrWhiteSpace(release.AssetsURL))
{
release.Assets = new Dictionary<string, Asset>();
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
Client.DefaultRequestHeaders.Add("User-Agent", "Genie Client Updater");
var streamTask = Client.GetStreamAsync(release.AssetsURL).Result;
List<Asset> latestAssets = JsonSerializer.Deserialize<List<Asset>>(streamTask);
foreach (Asset asset in latestAssets)
{
release.Assets.Add(asset.Name, asset);
}
return true;
}
return false;
}
public static async Task<Release> GetRelease(string githubPath)
{
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
Client.DefaultRequestHeaders.Add("User-Agent", "Genie Client Updater");
try
{
var streamTask = Client.GetStreamAsync(githubPath);
Release latest = await JsonSerializer.DeserializeAsync<Release>(await streamTask);
return latest;
}
catch (Exception ex)
{
return new Release() { Version = "404", AssetsURL = "Something's wrong. No server version could be found." };
}
}
public static async Task<Release> GetCurrentVersion()
{
Release current = new Release();
if (File.Exists($"{LocalDirectory}\\genie.exe"))
{
current.Version = FileVersionInfo.GetVersionInfo($"{LocalDirectory}\\genie.exe").FileVersion;
}
else
{
current.Version = "0";
}
return current;
}
public static async Task<bool> LaunchGenie()
{
if (File.Exists(@$"{LocalDirectory}\genie.exe"))
{
Console.WriteLine("[Launching Genie]");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
FileInfo file = new FileInfo(@$"{LocalDirectory}\genie.exe");
do { Thread.Sleep(10); } while (FileIsLocked(file));
Process genie = Process.Start($"{LocalDirectory}\\genie.exe");
return genie != null;
}
}
return false;
}
public static string GetDataDirectory(bool local)
{
if (local) return LocalDirectory;
string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
dir = System.IO.Path.Combine(dir, Lamp.GenieProductName);
if (!System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
}
return dir;
}
public static void Move(string source, string destinationPath)
{
string filename = Path.GetFileName(source);
try
{
File.Move(source, $@"{destinationPath}\{filename}", true);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public static bool FileIsLocked(FileInfo file)
{
try
{
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
//file is not locked
return false;
}
}
}