|
| 1 | +package branding |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "mime/multipart" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/utmstack/UTMStack/installer/utils" |
| 17 | +) |
| 18 | + |
| 19 | +func InstallLicense(updatesFolder string) (bool, error) { |
| 20 | + src := LicensePath() |
| 21 | + if !utils.CheckIfPathExist(src) { |
| 22 | + return false, nil |
| 23 | + } |
| 24 | + dst := filepath.Join(updatesFolder, "LICENSE") |
| 25 | + if utils.CheckIfPathExist(dst) { |
| 26 | + return false, nil |
| 27 | + } |
| 28 | + data, err := os.ReadFile(src) |
| 29 | + if err != nil { |
| 30 | + return false, err |
| 31 | + } |
| 32 | + if err := os.WriteFile(dst, data, 0644); err != nil { |
| 33 | + return false, err |
| 34 | + } |
| 35 | + return true, nil |
| 36 | +} |
| 37 | + |
| 38 | +var assetFiles = map[string]string{ |
| 39 | + "logo": "logo", |
| 40 | + "logoDark": "logo-dark", |
| 41 | + "favicon": "favicon", |
| 42 | + "reportLogo": "report-logo", |
| 43 | + "reportCover": "report-cover", |
| 44 | +} |
| 45 | + |
| 46 | +var imageExts = []string{".svg", ".png", ".jpg", ".jpeg", ".webp", ".gif", ".ico"} |
| 47 | + |
| 48 | +func findAsset(base string) string { |
| 49 | + for _, ext := range imageExts { |
| 50 | + p := filepath.Join(Dir(), base+ext) |
| 51 | + if utils.CheckIfPathExist(p) { |
| 52 | + return p |
| 53 | + } |
| 54 | + } |
| 55 | + return "" |
| 56 | +} |
| 57 | + |
| 58 | +func Seed(baseURL, internalKey string) error { |
| 59 | + if !IsCustom() { |
| 60 | + return nil |
| 61 | + } |
| 62 | + b := Get() |
| 63 | + |
| 64 | + var buf bytes.Buffer |
| 65 | + w := multipart.NewWriter(&buf) |
| 66 | + |
| 67 | + fields := map[string]string{ |
| 68 | + "enabled": "true", |
| 69 | + "productName": b.ProductName, |
| 70 | + } |
| 71 | + for k, v := range fields { |
| 72 | + if err := w.WriteField(k, v); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + for slot, base := range assetFiles { |
| 78 | + p := findAsset(base) |
| 79 | + if p == "" { |
| 80 | + continue |
| 81 | + } |
| 82 | + if err := addFile(w, slot, p); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + if err := w.Close(); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + req, err := http.NewRequest(http.MethodPost, strings.TrimRight(baseURL, "/")+"/api/v1/branding/seed", &buf) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + req.Header.Set("Content-Type", w.FormDataContentType()) |
| 95 | + req.Header.Set("X-Internal-Key", internalKey) |
| 96 | + |
| 97 | + client := &http.Client{ |
| 98 | + Timeout: 30 * time.Second, |
| 99 | + Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, |
| 100 | + } |
| 101 | + resp, err := client.Do(req) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + defer func() { _ = resp.Body.Close() }() |
| 106 | + if resp.StatusCode != http.StatusOK { |
| 107 | + body, _ := io.ReadAll(resp.Body) |
| 108 | + return fmt.Errorf("status %d: %s", resp.StatusCode, string(body)) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// mimeForExt maps an image extension to its MIME type (for data URIs). |
| 114 | +func mimeForExt(ext string) string { |
| 115 | + switch strings.ToLower(ext) { |
| 116 | + case ".svg": |
| 117 | + return "image/svg+xml" |
| 118 | + case ".png": |
| 119 | + return "image/png" |
| 120 | + case ".jpg", ".jpeg": |
| 121 | + return "image/jpeg" |
| 122 | + case ".webp": |
| 123 | + return "image/webp" |
| 124 | + case ".gif": |
| 125 | + return "image/gif" |
| 126 | + case ".ico": |
| 127 | + return "image/x-icon" |
| 128 | + default: |
| 129 | + return "application/octet-stream" |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// LogoDataURI returns the shipped logo encoded as a base64 data URI, so it can |
| 134 | +// be embedded directly in the nginx maintenance page (which is served by the |
| 135 | +// host nginx while the backend — and its /uploads — is down). Returns false |
| 136 | +// when no logo was shipped. |
| 137 | +func LogoDataURI() (string, bool) { |
| 138 | + p := findAsset("logo") |
| 139 | + if p == "" { |
| 140 | + return "", false |
| 141 | + } |
| 142 | + data, err := os.ReadFile(p) |
| 143 | + if err != nil { |
| 144 | + return "", false |
| 145 | + } |
| 146 | + return "data:" + mimeForExt(filepath.Ext(p)) + ";base64," + base64.StdEncoding.EncodeToString(data), true |
| 147 | +} |
| 148 | + |
| 149 | +func addFile(w *multipart.Writer, field, path string) error { |
| 150 | + f, err := os.Open(path) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + defer func() { _ = f.Close() }() |
| 155 | + fw, err := w.CreateFormFile(field, filepath.Base(path)) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + _, err = io.Copy(fw, f) |
| 160 | + return err |
| 161 | +} |
0 commit comments