2022-10-08 01:23:11 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
2022-10-08 08:47:27 -07:00
|
|
|
|
using System.Diagnostics;
|
2022-10-08 01:23:11 -07:00
|
|
|
|
using System.IO;
|
2022-10-08 08:47:27 -07:00
|
|
|
|
using System.IO.Compression;
|
2022-10-08 01:23:11 -07:00
|
|
|
|
using System.Net;
|
2022-10-08 08:47:27 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-10-08 01:23:11 -07:00
|
|
|
|
using System.Windows.Forms;
|
2023-02-20 04:26:53 -08:00
|
|
|
|
using Newtonsoft.Json;
|
2022-10-08 01:23:11 -07:00
|
|
|
|
|
|
|
|
|
namespace UptimeKuma {
|
|
|
|
|
public partial class DownloadForm : Form {
|
|
|
|
|
private readonly Queue<DownloadItem> downloadQueue = new();
|
|
|
|
|
private readonly WebClient webClient = new();
|
2022-10-08 08:47:27 -07:00
|
|
|
|
private DownloadItem currentDownloadItem;
|
2022-10-08 01:23:11 -07:00
|
|
|
|
|
|
|
|
|
public DownloadForm() {
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DownloadForm_Load(object sender, EventArgs e) {
|
|
|
|
|
webClient.DownloadProgressChanged += DownloadProgressChanged;
|
|
|
|
|
webClient.DownloadFileCompleted += DownloadFileCompleted;
|
|
|
|
|
|
2023-02-20 04:26:53 -08:00
|
|
|
|
label.Text = "Reading latest version...";
|
|
|
|
|
|
|
|
|
|
// Read json from https://uptime.kuma.pet/version
|
2023-02-20 10:50:25 -08:00
|
|
|
|
var versionJson = new WebClient().DownloadString("https://uptime.kuma.pet/version");
|
|
|
|
|
var versionObj = JsonConvert.DeserializeObject<Version>(versionJson);
|
2023-02-20 04:26:53 -08:00
|
|
|
|
|
|
|
|
|
var nodeVersion = versionObj.nodejs;
|
|
|
|
|
var uptimeKumaVersion = versionObj.latest;
|
2023-02-20 10:50:25 -08:00
|
|
|
|
var hasUpdateFile = File.Exists("update");
|
2023-02-20 04:26:53 -08:00
|
|
|
|
|
2022-10-08 08:47:27 -07:00
|
|
|
|
if (!Directory.Exists("node")) {
|
2022-10-08 01:23:11 -07:00
|
|
|
|
downloadQueue.Enqueue(new DownloadItem {
|
2023-02-20 04:26:53 -08:00
|
|
|
|
URL = $"https://nodejs.org/dist/v{nodeVersion}/node-v{nodeVersion}-win-x64.zip",
|
2022-10-08 08:47:27 -07:00
|
|
|
|
Filename = "node.zip",
|
|
|
|
|
TargetFolder = "node"
|
2022-10-08 01:23:11 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 10:50:25 -08:00
|
|
|
|
if (!Directory.Exists("core") || hasUpdateFile) {
|
|
|
|
|
|
|
|
|
|
// It is update, rename the core folder to core.old
|
|
|
|
|
if (Directory.Exists("core")) {
|
|
|
|
|
// Remove the old core.old folder
|
|
|
|
|
if (Directory.Exists("core.old")) {
|
|
|
|
|
Directory.Delete("core.old", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Directory.Move("core", "core.old");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 01:23:11 -07:00
|
|
|
|
downloadQueue.Enqueue(new DownloadItem {
|
2023-02-20 04:26:53 -08:00
|
|
|
|
URL = $"https://github.com/louislam/uptime-kuma/archive/refs/tags/{uptimeKumaVersion}.zip",
|
2022-10-08 08:47:27 -07:00
|
|
|
|
Filename = "core.zip",
|
|
|
|
|
TargetFolder = "core"
|
2022-10-08 01:23:11 -07:00
|
|
|
|
});
|
2023-02-20 10:50:25 -08:00
|
|
|
|
|
|
|
|
|
File.WriteAllText("version.json", versionJson);
|
|
|
|
|
|
|
|
|
|
// Delete the update file
|
|
|
|
|
if (hasUpdateFile) {
|
|
|
|
|
File.Delete("update");
|
|
|
|
|
}
|
2022-10-08 01:23:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DownloadNextFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DownloadNextFile() {
|
|
|
|
|
if (downloadQueue.Count > 0) {
|
|
|
|
|
var item = downloadQueue.Dequeue();
|
2022-10-08 08:47:27 -07:00
|
|
|
|
|
|
|
|
|
currentDownloadItem = item;
|
|
|
|
|
|
|
|
|
|
// Download if the zip file is not existing
|
|
|
|
|
if (!File.Exists(item.Filename)) {
|
|
|
|
|
label.Text = item.URL;
|
|
|
|
|
webClient.DownloadFileAsync(new Uri(item.URL), item.Filename);
|
|
|
|
|
} else {
|
|
|
|
|
progressBar.Value = 100;
|
|
|
|
|
label.Text = "Use local " + item.Filename;
|
|
|
|
|
DownloadFileCompleted(null, null);
|
|
|
|
|
}
|
2022-10-08 01:23:11 -07:00
|
|
|
|
} else {
|
2022-10-08 08:47:27 -07:00
|
|
|
|
npmSetup();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void npmSetup() {
|
|
|
|
|
labelData.Text = "";
|
|
|
|
|
|
2023-02-20 10:50:25 -08:00
|
|
|
|
var npm = "..\\node\\npm.cmd";
|
|
|
|
|
var cmd = $"{npm} ci --production & {npm} run download-dist & exit";
|
|
|
|
|
|
2022-10-08 08:47:27 -07:00
|
|
|
|
var startInfo = new ProcessStartInfo {
|
|
|
|
|
FileName = "cmd.exe",
|
2023-02-20 10:50:25 -08:00
|
|
|
|
Arguments = $"/k \"{cmd}\"",
|
2022-10-08 08:47:27 -07:00
|
|
|
|
RedirectStandardOutput = false,
|
|
|
|
|
RedirectStandardError = false,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = false,
|
|
|
|
|
WorkingDirectory = "core"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var process = new Process();
|
|
|
|
|
process.StartInfo = startInfo;
|
|
|
|
|
process.EnableRaisingEvents = true;
|
2023-02-20 10:50:25 -08:00
|
|
|
|
process.Exited += (_, e) => {
|
2022-10-08 12:47:06 -07:00
|
|
|
|
progressBar.Value = 100;
|
2022-10-08 08:47:27 -07:00
|
|
|
|
|
|
|
|
|
if (process.ExitCode == 0) {
|
2023-02-20 10:50:25 -08:00
|
|
|
|
Task.Delay(2000).ContinueWith(_ => {
|
2022-10-08 12:47:06 -07:00
|
|
|
|
Application.Restart();
|
|
|
|
|
});
|
2022-10-08 08:47:27 -07:00
|
|
|
|
label.Text = "Done";
|
|
|
|
|
} else {
|
|
|
|
|
label.Text = "Failed, exit code: " + process.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
process.Start();
|
2022-10-08 12:47:06 -07:00
|
|
|
|
label.Text = "Installing dependencies and download dist files";
|
|
|
|
|
progressBar.Value = 50;
|
2023-02-20 10:50:25 -08:00
|
|
|
|
process.WaitForExit();
|
2022-10-08 01:23:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) {
|
|
|
|
|
progressBar.Value = e.ProgressPercentage;
|
|
|
|
|
var total = e.TotalBytesToReceive / 1024;
|
|
|
|
|
var current = e.BytesReceived / 1024;
|
2022-10-08 08:47:27 -07:00
|
|
|
|
|
|
|
|
|
if (total > 0) {
|
|
|
|
|
labelData.Text = $"{current}KB/{total}KB";
|
|
|
|
|
}
|
2022-10-08 01:23:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 12:47:06 -07:00
|
|
|
|
void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) {
|
2022-10-08 08:47:27 -07:00
|
|
|
|
Extract(currentDownloadItem);
|
2022-10-08 01:23:11 -07:00
|
|
|
|
DownloadNextFile();
|
|
|
|
|
}
|
2022-10-08 08:47:27 -07:00
|
|
|
|
|
|
|
|
|
void Extract(DownloadItem item) {
|
|
|
|
|
if (Directory.Exists(item.TargetFolder)) {
|
|
|
|
|
var dir = new DirectoryInfo(item.TargetFolder);
|
|
|
|
|
dir.Delete(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists("temp")) {
|
|
|
|
|
var dir = new DirectoryInfo("temp");
|
|
|
|
|
dir.Delete(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labelData.Text = $"Extracting {item.Filename}...";
|
|
|
|
|
|
|
|
|
|
ZipFile.ExtractToDirectory(item.Filename, "temp");
|
|
|
|
|
|
|
|
|
|
string[] dirList;
|
|
|
|
|
|
|
|
|
|
// Move to the correct level
|
|
|
|
|
dirList = Directory.GetDirectories("temp");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (dirList.Length > 0) {
|
|
|
|
|
var dir = dirList[0];
|
|
|
|
|
|
|
|
|
|
// As sometime ExtractToDirectory is still locking the directory, loop until ok
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
Directory.Move(dir, item.TargetFolder);
|
|
|
|
|
break;
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
MessageBox.Show("Unexcepted Error: Cannot move extracted files, folder not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labelData.Text = $"Extracted";
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists("temp")) {
|
|
|
|
|
var dir = new DirectoryInfo("temp");
|
|
|
|
|
dir.Delete(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Delete(item.Filename);
|
|
|
|
|
}
|
2022-10-08 01:23:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DownloadItem {
|
|
|
|
|
public string URL { get; set; }
|
|
|
|
|
public string Filename { get; set; }
|
2022-10-08 08:47:27 -07:00
|
|
|
|
public string TargetFolder { get; set; }
|
2022-10-08 01:23:11 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|