-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
api_plist.go
92 lines (83 loc) · 2.37 KB
/
api_plist.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
package yolosvc
import (
"fmt"
"math/rand"
"net/http"
"strings"
"berty.tech/yolo/v2/go/pkg/plistgen"
"github.com/go-chi/chi"
"github.com/stretchr/signature"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"google.golang.org/grpc/codes"
)
func (svc *service) PlistGenerator(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "artifactID")
artifact, err := svc.store.GetArtifactByID(id)
if err != nil {
httpError(w, err, codes.InvalidArgument)
return
}
scheme := r.Header.Get("X-Forwarded-Proto")
if scheme == "" {
scheme = "http"
}
baseURL := fmt.Sprintf("%s://%s", scheme, r.Host)
var (
bundleID = "tech.berty.yolo"
title = ""
subtitle = ""
version = ""
displayImage = baseURL + "/bundle-57x57.png"
fullSizeImage = baseURL + "/bundle-512x512.png"
url = "/api/artifact-dl/" + id
)
if artifact.HasBuild != nil && artifact.HasBuild.HasProject != nil {
c := cases.Title(language.Und)
title = c.String(artifact.HasBuild.HasProject.Name)
if artifact.HasBuild.HasProject.HasOwner != nil {
subtitle = c.String(artifact.HasBuild.HasProject.HasOwner.Name)
}
}
pkgURL, err := signature.GetSignedURL("GET", url, "", svc.authSalt)
if err != nil {
httpError(w, err, codes.Internal)
return
}
// override with extracted data from artifact
if artifact.BundleID != "" {
bundleID = artifact.BundleID
}
if artifact.BundleName != "" {
title = artifact.BundleName
}
if artifact.BundleIcon != "" {
displayImageURL := "/api/artifact-icon/" + artifact.BundleIcon
signedURL, err := signature.GetSignedURL("GET", displayImageURL, "", svc.authSalt)
if err != nil {
httpError(w, err, codes.Internal)
return
}
displayImage = baseURL + signedURL
}
// append random emojis
title = strings.TrimSpace(title + " " + randEmoji())
subtitle = strings.TrimSpace(subtitle + " " + randEmoji())
plist := plistgen.Release(bundleID, baseURL+pkgURL)
plist.SetTitle(title)
plist.SetSubtitle(subtitle)
plist.SetDisplayImage(displayImage, false)
plist.SetFullSizeImage(fullSizeImage, false)
plist.SetVersion(version)
b, err := plist.Marshal()
if err != nil {
httpError(w, err, codes.Internal)
return
}
w.Header().Add("Content-Type", "application/x-plist")
_, _ = w.Write(b)
}
func randEmoji() string {
list := []string{"😱", "🤡", "🧚♀️", "🥰", "🙌"}
return list[rand.Intn(len(list))]
}