forked from Vencord/Installer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgithub_downloader.go
188 lines (153 loc) · 3.97 KB
/
github_downloader.go
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
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Installer, a cross platform gui/cli app for installing Vencord
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"os"
path "path/filepath"
"regexp"
"strconv"
"strings"
)
type GithubRelease struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
Assets []struct {
Name string `json:"name"`
DownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}
var ReleaseData GithubRelease
var GithubError error
var GithubDoneChan chan bool
var InstalledHash = "None"
var LatestHash = "Unknown"
var IsDevInstall bool
func GetGithubRelease(url string) (*GithubRelease, error) {
Log.Debug("Fetching", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Log.Error("Failed to create Request", err)
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
Log.Error("Failed to send Request", err)
return nil, err
}
defer res.Body.Close()
var data GithubRelease
if err = json.NewDecoder(res.Body).Decode(&data); err != nil {
Log.Error("Failed to decode GitHub JSON Response", err)
return nil, err
}
return &data, nil
}
func InitGithubDownloader() {
GithubDoneChan = make(chan bool, 1)
IsDevInstall = os.Getenv("EQUICORD_DEV_INSTALL") == "1"
Log.Debug("Is Dev Install: ", IsDevInstall)
if IsDevInstall {
GithubDoneChan <- true
return
}
go func() {
// Make sure UI updates once the request either finished or failed
defer func() {
GithubDoneChan <- GithubError == nil
}()
data, err := GetGithubRelease(ReleaseUrl)
if err != nil {
GithubError = err
return
}
ReleaseData = *data
i := strings.LastIndex(data.Name, " ") + 1
LatestHash = data.Name[i:]
Log.Debug("Finished fetching GitHub Data")
Log.Debug("Latest hash is", LatestHash, "Local Install is", Ternary(LatestHash == InstalledHash, "up to date!", "outdated!"))
}()
// either .asar file or directory with main.js file (in DEV)
EquicordFile := EquicordDirectory
stat, err := os.Stat(EquicordFile)
if err != nil {
return
}
// dev
if stat.IsDir() {
EquicordFile = path.Join(EquicordFile, "main.js")
}
// Check hash of installed version if exists
b, err := os.ReadFile(EquicordFile)
if err != nil {
return
}
Log.Debug("Found existing Equicord Install. Checking for hash...")
re := regexp.MustCompile(`// Equicord (\w+)`)
match := re.FindSubmatch(b)
if match != nil {
InstalledHash = string(match[1])
Log.Debug("Existing hash is", InstalledHash)
} else {
Log.Debug("Didn't find hash")
}
}
func installLatestBuilds() (retErr error) {
Log.Debug("Installing latest builds...")
if IsDevInstall {
Log.Debug("Skipping due to dev install")
return
}
downloadUrl := ""
for _, ass := range ReleaseData.Assets {
if ass.Name == "desktop.asar" {
downloadUrl = ass.DownloadURL
break
}
}
if downloadUrl == "" {
retErr = errors.New("Didn't find desktop.asar download link")
Log.Error(retErr)
return
}
Log.Debug("Downloading desktop.asar")
res, err := http.Get(downloadUrl)
if err == nil && res.StatusCode >= 300 {
err = errors.New(res.Status)
}
if err != nil {
Log.Error("Failed to download desktop.asar:", err)
retErr = err
return
}
out, err := os.OpenFile(EquicordDirectory, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
Log.Error("Failed to create", EquicordDirectory+":", err)
retErr = err
return
}
read, err := io.Copy(out, res.Body)
if err != nil {
Log.Error("Failed to download to", EquicordDirectory+":", err)
retErr = err
return
}
contentLength := res.Header.Get("Content-Length")
expected := strconv.FormatInt(read, 10)
if expected != contentLength {
err = errors.New("Unexpected end of input. Content-Length was " + contentLength + ", but I only read " + expected)
Log.Error(err.Error())
retErr = err
return
}
_ = FixOwnership(EquicordDirectory)
InstalledHash = LatestHash
return
}