|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "io/fs" |
| 8 | + "net/http" |
| 9 | + "path" |
| 10 | + "strings" |
| 11 | + "testing/fstest" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/replicatedhq/embedded-cluster/pkg/release" |
| 15 | + kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" |
| 16 | +) |
| 17 | + |
| 18 | +var appConstantsJsContent = `<script> |
| 19 | + var APP_TITLE = "%s"; |
| 20 | + var APP_ICON_PATH = "%s"; |
| 21 | + var APP_UPSTREAM_ICON_URI = "%s"; |
| 22 | +</script>` |
| 23 | + |
| 24 | +var assetHTTPClient = &http.Client{ |
| 25 | + Timeout: 10 * time.Second, |
| 26 | +} |
| 27 | + |
| 28 | +// AssetsFs returns a fs.FS that contains a javascript file and the icon file itself. |
| 29 | +// It is used to serve the application title, icon, and upstream icon URI to the web UI. |
| 30 | +func AssetsFs(pathPrefix string) fs.FS { |
| 31 | + app := release.GetApplication() |
| 32 | + |
| 33 | + appTitle := getApplicationTitle(app) |
| 34 | + appUpstreamIconURI := getApplicationUpstreamIconURI(app) |
| 35 | + appIconData := getApplicationIconData(app) |
| 36 | + appIconFileName := getApplicationIconFileName(app) |
| 37 | + appIconPath := path.Join(pathPrefix, appIconFileName) |
| 38 | + |
| 39 | + return fstest.MapFS{ |
| 40 | + path.Join(pathPrefix, "app-constants.js"): { |
| 41 | + Data: fmt.Appendf([]byte{}, appConstantsJsContent, appTitle, appIconPath, appUpstreamIconURI), |
| 42 | + Mode: 0444, |
| 43 | + ModTime: time.Now(), |
| 44 | + }, |
| 45 | + appIconPath: { |
| 46 | + Data: appIconData, |
| 47 | + Mode: 0444, |
| 48 | + ModTime: time.Now(), |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func getApplicationTitle(app *kotsv1beta1.Application) string { |
| 54 | + if app != nil { |
| 55 | + return app.Spec.Title |
| 56 | + } |
| 57 | + return "App" |
| 58 | +} |
| 59 | + |
| 60 | +func getApplicationUpstreamIconURI(app *kotsv1beta1.Application) string { |
| 61 | + if app != nil && app.Spec.Icon != "" { |
| 62 | + return app.Spec.Icon |
| 63 | + } |
| 64 | + return defaultIcon |
| 65 | +} |
| 66 | + |
| 67 | +func getApplicationIconData(app *kotsv1beta1.Application) []byte { |
| 68 | + if app != nil && app.Spec.Icon != "" { |
| 69 | + if data := getIconData(app.Spec.Icon); len(data) > 0 { |
| 70 | + return data |
| 71 | + } |
| 72 | + } |
| 73 | + return getIconData(defaultIcon) |
| 74 | +} |
| 75 | + |
| 76 | +func getApplicationIconFileName(app *kotsv1beta1.Application) string { |
| 77 | + if app != nil && app.Spec.Icon != "" { |
| 78 | + if icon := getIconFileName(app.Spec.Icon); icon != "" { |
| 79 | + return icon |
| 80 | + } |
| 81 | + } |
| 82 | + return getDefaultIconFileName() |
| 83 | +} |
| 84 | + |
| 85 | +func getDefaultIconFileName() string { |
| 86 | + return getIconFileName(defaultIcon) |
| 87 | +} |
| 88 | + |
| 89 | +func getIconData(icon string) []byte { |
| 90 | + // Check if it's a data URI (starts with data:) |
| 91 | + if strings.HasPrefix(icon, "data:") { |
| 92 | + // Extract base64 data |
| 93 | + if strings.Contains(icon, ";base64,") { |
| 94 | + dataParts := strings.Split(icon, ";base64,") |
| 95 | + if len(dataParts) > 1 { |
| 96 | + decoded, err := base64.StdEncoding.DecodeString(dataParts[1]) |
| 97 | + if err == nil { |
| 98 | + return decoded |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } else if strings.HasPrefix(icon, "http://") || strings.HasPrefix(icon, "https://") { |
| 103 | + // For URLs, try to fetch the icon |
| 104 | + resp, err := assetHTTPClient.Get(icon) |
| 105 | + if err == nil && resp.StatusCode == http.StatusOK { |
| 106 | + defer resp.Body.Close() |
| 107 | + data, err := io.ReadAll(resp.Body) |
| 108 | + if err == nil { |
| 109 | + return data |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func getIconFileName(icon string) string { |
| 117 | + // Check if it's a data URI (starts with data:) |
| 118 | + if strings.HasPrefix(icon, "data:") { |
| 119 | + // Extract content type to determine file extension |
| 120 | + parts := strings.Split(icon, ";") |
| 121 | + if len(parts) > 0 { |
| 122 | + contentTypeParts := strings.Split(parts[0], ":") |
| 123 | + if len(contentTypeParts) > 1 { |
| 124 | + contentType := contentTypeParts[1] |
| 125 | + switch contentType { |
| 126 | + case "image/png": |
| 127 | + return "icon.png" |
| 128 | + case "image/jpeg", "image/jpg": |
| 129 | + return "icon.jpg" |
| 130 | + case "image/svg+xml": |
| 131 | + return "icon.svg" |
| 132 | + default: |
| 133 | + return "" |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } else if strings.HasPrefix(icon, "http://") || strings.HasPrefix(icon, "https://") { |
| 138 | + // Try to determine file extension from URL |
| 139 | + urlParts := strings.Split(icon, ".") |
| 140 | + if len(urlParts) > 1 { |
| 141 | + ext := urlParts[len(urlParts)-1] |
| 142 | + if ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "svg" { |
| 143 | + return "icon." + ext |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return "" |
| 148 | +} |
0 commit comments