-
Notifications
You must be signed in to change notification settings - Fork 4
/
asset.go
94 lines (84 loc) · 2.5 KB
/
asset.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
package module
import (
"context"
"io"
"net/http"
"path"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/util"
)
const (
assetURL = "https://api.github.com/repos/kyleu/projectforge/releases/latest"
backupAssetURL = "https://projectforge.dev/assets/module"
assetPrefix = "projectforge_module_"
assetSuffix = ".zip"
)
var assetMap map[string]string
func (s *Service) AssetURL(ctx context.Context, key string, logger util.Logger) (string, error) {
if assetMap == nil {
if err := loadAssetMap(ctx, logger); err != nil {
return "", err
}
}
ret, ok := assetMap[key]
if !ok {
msg := "no URL available for module [%s] among candidates [%s]"
return "", errors.Errorf(msg, key, strings.Join(util.ArraySorted(lo.Keys(assetMap)), ", "))
}
return ret, nil
}
type ghAsset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int `json:"size"`
}
type ghRsp struct {
Assets []*ghAsset `json:"assets"`
}
func loadAssetMap(ctx context.Context, logger util.Logger) error {
assetMap = map[string]string{}
if runtime.GOOS == "js" {
return loadBackupAssetMap(logger)
}
logger.Infof("loading assets from [%s]", assetURL)
httpClient := telemetry.WrapHTTPClient(http.DefaultClient)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, assetURL, nil)
if err != nil {
return errors.Wrapf(err, "unable to create request from [%s]", assetURL)
}
req.Header.Set("Access-Control-Allow-Origin", "*")
rsp, err := httpClient.Do(req)
if err != nil {
return loadBackupAssetMap(logger)
}
if rsp.StatusCode != 200 {
return errors.Errorf("release asset [%s] returned status [%d]", assetURL, rsp.StatusCode)
}
bts, err := io.ReadAll(rsp.Body)
if err != nil {
return errors.Wrapf(err, "unable to read release asset from [%s]", assetURL)
}
x := &ghRsp{}
err = util.FromJSON(bts, &x)
if err != nil {
return errors.Wrapf(err, "release asset at [%s] returned invalid JSON", assetURL)
}
lo.ForEach(x.Assets, func(asset *ghAsset, _ int) {
if strings.HasPrefix(asset.Name, assetPrefix) {
key := strings.TrimSuffix(asset.Name[len(assetPrefix):], assetSuffix)
assetMap[key] = asset.URL
}
})
return nil
}
func loadBackupAssetMap(logger util.Logger) error {
logger.Info("unable to download assets from github, using backup option")
lo.ForEach(nativeModuleKeys, func(key string, _ int) {
assetMap[key] = path.Join(backupAssetURL, key+".zip")
})
return nil
}