frontend_plugin.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package plugins
  2. import (
  3. "net/url"
  4. "path"
  5. "path/filepath"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. type FrontendPluginBase struct {
  10. PluginBase
  11. }
  12. func (fp *FrontendPluginBase) initFrontendPlugin() {
  13. if fp.StaticRoot != "" {
  14. fp.StaticRootAbs = filepath.Join(fp.PluginDir, fp.StaticRoot)
  15. StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
  16. Directory: fp.StaticRootAbs,
  17. PluginId: fp.Id,
  18. })
  19. }
  20. fp.handleModuleDefaults()
  21. fp.Info.Logos.Small = getPluginLogoUrl(fp.Info.Logos.Small, fp.BaseUrl)
  22. fp.Info.Logos.Large = getPluginLogoUrl(fp.Info.Logos.Large, fp.BaseUrl)
  23. for i := 0; i < len(fp.Info.Screenshots); i++ {
  24. fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.BaseUrl)
  25. }
  26. }
  27. func getPluginLogoUrl(path, baseUrl string) string {
  28. if path == "" {
  29. return "public/img/plugin-default-logo_dark.svg"
  30. }
  31. return evalRelativePluginUrlPath(path, baseUrl)
  32. }
  33. func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
  34. appSubPath := strings.Replace(fp.PluginDir, app.StaticRootAbs, "", 1)
  35. fp.IncludedInAppId = app.Id
  36. fp.BaseUrl = app.BaseUrl
  37. fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
  38. }
  39. func (fp *FrontendPluginBase) handleModuleDefaults() {
  40. if fp.StaticRoot != "" {
  41. fp.Module = path.Join("plugins", fp.Id, "module")
  42. fp.BaseUrl = path.Join("public/plugins", fp.Id)
  43. return
  44. }
  45. fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
  46. fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id)
  47. }
  48. func evalRelativePluginUrlPath(pathStr string, baseUrl string) string {
  49. if pathStr == "" {
  50. return ""
  51. }
  52. u, _ := url.Parse(pathStr)
  53. if u.IsAbs() {
  54. return pathStr
  55. }
  56. return path.Join(baseUrl, pathStr)
  57. }