forked from Vencord/Installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_downloader.go
182 lines (159 loc) · 4.79 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
/*
* This part is file of VencordInstaller
* Copyright (c) 2022 Vendicated
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
path "path/filepath"
"strconv"
"strings"
"sync"
)
type GithubRelease struct {
Name string `json:"name"`
Assets []struct {
Name string `json:"name"`
DownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}
const releaseUrl = "https://api.github.com/repos/Vendicated/Vencord/releases/latest"
var ReleaseData GithubRelease
var GithubError error
var GithubDoneChan chan bool
var InstalledHash = "None"
var LatestHash = "Unknown"
var IsDevInstall bool
func InitGithubDownloader() {
GithubDoneChan = make(chan bool, 1)
IsDevInstall = os.Getenv("VENCORD_DEV_INSTALL") == "1"
fmt.Println("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
}()
fmt.Println("Fetching", releaseUrl)
req, err := http.NewRequest("GET", releaseUrl, nil)
if err != nil {
fmt.Println("Failed to create Request", err)
GithubError = err
return
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "VencordInstaller/"+InstallerGitHash+" (https://github.com/Vendicated/VencordInstaller)")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Failed to send Request", err)
GithubError = err
return
}
defer res.Body.Close()
if res.StatusCode >= 300 {
GithubError = errors.New(res.Status)
fmt.Println("Github returned Non-OK status", GithubError)
return
}
if GithubError = json.NewDecoder(res.Body).Decode(&ReleaseData); GithubError != nil {
fmt.Println("Failed to decode GitHub JSON Response", GithubError)
} else {
i := strings.LastIndex(ReleaseData.Name, " ") + 1
LatestHash = ReleaseData.Name[i:]
fmt.Println("Finished fetching GitHub Data")
fmt.Println("Latest hash is", LatestHash, "Local Install is", Ternary(LatestHash == InstalledHash, "up to date!", "outdated!"))
}
}()
// Check hash of installed version if exists
f, err := os.Open(Patcher)
if err != nil {
return
}
//goland:noinspection GoUnhandledErrorResult
defer f.Close()
fmt.Println("Found existing Vencord Install. Checking for hash...")
scanner := bufio.NewScanner(f)
if scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "// Vencord ") {
InstalledHash = line[11:]
fmt.Println("Existing hash is", InstalledHash)
} else {
fmt.Println("Didn't find hash")
}
}
}
func installLatestBuilds() (retErr error) {
fmt.Println("Installing latest builds...")
var wg sync.WaitGroup
for _, ass := range ReleaseData.Assets {
if strings.HasPrefix(ass.Name, "patcher.js") ||
strings.HasPrefix(ass.Name, "preload.js") ||
strings.HasPrefix(ass.Name, "renderer.js") ||
strings.HasPrefix(ass.Name, "renderer.css") {
wg.Add(1)
ass := ass // Need to do this to not have the variable be overwritten halfway through
go func() {
defer wg.Done()
fmt.Println("Downloading file", ass.Name)
res, err := http.Get(ass.DownloadURL)
if err == nil && res.StatusCode >= 300 {
err = errors.New(res.Status)
}
if err != nil {
fmt.Println("Failed to download", ass.Name+":", err)
retErr = err
return
}
outFile := path.Join(FilesDir, ass.Name)
out, err := os.OpenFile(outFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println("Failed to create", outFile+":", err)
retErr = err
return
}
read, err := io.Copy(out, res.Body)
if err != nil {
fmt.Println("Failed to download to", outFile+":", 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)
fmt.Println(err)
retErr = err
return
}
}()
}
}
wg.Wait()
fmt.Println("Done!")
_ = FixOwnership(FilesDir)
InstalledHash = LatestHash
return
}