-
Notifications
You must be signed in to change notification settings - Fork 14
/
install.go
266 lines (210 loc) · 5.85 KB
/
install.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
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
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"runtime"
"strings"
"time"
log "github.com/Crosse/gosimplelogger"
)
var installedFonts = 0
// InstallFont installs the font specified by fontPath.
// fontPath can either be a URL or a filesystem path.
// For URLs, only the "file", "http", and "https" schemes are currently valid.
func InstallFont(fontPath string) error {
var (
b []byte
err error
fontData *FontData
)
u, err := url.Parse(fontPath)
if err != nil {
return fmt.Errorf("error parsing path: %w", err)
}
switch u.Scheme {
case "file", "":
if b, err = getLocalFile(fontPath); err != nil {
return err
}
case "http", "https":
if b, err = getRemoteFile(fontPath); err != nil {
return err
}
default:
return fmt.Errorf("unhandled URL scheme: %v", u.Scheme)
}
filename := path.Base(u.Path)
ct := getContentType(b)
log.Debugf("content type: %s", ct)
switch ct {
case "application/zip":
return installFromZIP(b)
case "application/x-gzip":
return installFromGZIP(filename, b)
case "application/octet-stream":
if strings.ToLower(path.Ext(filename)) == ".tar" {
return installFromTarball(bytes.NewReader(b))
}
fallthrough
default:
fontData, err = NewFontData(filename, b)
if err != nil {
return err
}
return install(fontData)
}
}
func getContentType(data []byte) string {
contentType := http.DetectContentType(data)
log.Debugf("Detected content type: %v", contentType)
return contentType
}
func getRemoteFile(url string) ([]byte, error) {
log.Infof("Downloading font file from %v", url)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("cannot make http request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error getting remote file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Debugf("HTTP request resulted in status %v", resp.StatusCode)
return nil, fmt.Errorf("server returned non-successful status code %d", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("erorr reading remote file: %w", err)
}
return data, nil
}
func getLocalFile(filename string) ([]byte, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("cannot read local file: %w", err)
}
return data, nil
}
func installFromGZIP(filename string, data []byte) error {
log.Debug("reading gzipped file")
bytesReader := bytes.NewReader(data)
gzipReader, err := gzip.NewReader(bytesReader)
if err != nil {
return fmt.Errorf("cannot read gzip file: %w", err)
}
defer gzipReader.Close()
uncompressedFilename := strings.TrimSuffix(filename, ".gz")
ext := strings.ToLower(path.Ext(uncompressedFilename))
if ext == ".tar" || ext == ".tgz" {
return installFromTarball(gzipReader)
}
// Gzipped files only contain a single compressed file, so we'll just assume that it's one compressed font.
b, err := io.ReadAll(gzipReader)
if err != nil {
return fmt.Errorf("cannot read compressed file: %w", err)
}
fontData, err := NewFontData(path.Base(uncompressedFilename), b)
if err != nil {
return err
}
return install(fontData)
}
func installFromTarball(r io.Reader) error {
log.Debug("reading tarball")
tarReader := tar.NewReader(r)
fonts := make(map[string]*FontData)
log.Debug("Scanning tarball for fonts")
for {
hdr, err := tarReader.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return fmt.Errorf("cannot read tarball: %w", err)
}
data, err := io.ReadAll(tarReader)
if err != nil {
return fmt.Errorf("unable to read file %s from tarball: %w", hdr.Name, err)
}
appendFont(fonts, hdr.Name, data)
}
return installFonts(fonts)
}
func installFromZIP(data []byte) error {
log.Debug("reading zipfile")
bytesReader := bytes.NewReader(data)
zipReader, err := zip.NewReader(bytesReader, int64(bytesReader.Len()))
if err != nil {
return fmt.Errorf("cannot read zip file: %w", err)
}
fonts := make(map[string]*FontData)
log.Debug("Scanning ZIP file for fonts")
for _, zf := range zipReader.File {
rc, err := zf.Open()
if err != nil {
return fmt.Errorf("cannot open compressed file %s: %w", zf.Name, err)
}
defer rc.Close()
data, err := io.ReadAll(rc)
if err != nil {
return fmt.Errorf("cannot read compressed file %s: %w", zf.Name, err)
}
appendFont(fonts, zf.Name, data)
}
return installFonts(fonts)
}
func appendFont(fonts map[string]*FontData, fileName string, data []byte) {
fontData, err := NewFontData(fileName, data)
if err != nil {
log.Errorf(`Skipping non-font file "%s"`, fileName)
return
}
if _, ok := fonts[fontData.Name]; !ok {
fonts[fontData.Name] = fontData
} else {
// Prefer OTF over TTF; otherwise prefer the first font we found.
first := strings.ToLower(path.Ext(fonts[fontData.Name].FileName))
second := strings.ToLower(path.Ext(fontData.FileName))
if first != second && second == ".otf" {
log.Infof(`Preferring "%s" over "%s"`, fontData.FileName, fonts[fontData.Name].FileName)
fonts[fontData.Name] = fontData
}
}
}
func installFonts(fonts map[string]*FontData) error {
for _, font := range fonts {
if strings.Contains(strings.ToLower(font.Name), "windows compatible") {
if runtime.GOOS != "windows" {
// hack to not install the "Windows Compatible" version of every nerd font.
log.Infof(`Ignoring "%s" on non-Windows platform`, font.Name)
continue
}
}
if err := install(font); err != nil {
return err
}
}
return nil
}
func install(fontData *FontData) error {
log.Infof("==> %s", fontData.Name)
err := platformDependentInstall(fontData)
if err == nil {
installedFonts++
}
return err
}