frontend_plugin.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package plugins
  2. import (
  3. "net/url"
  4. "path"
  5. "path/filepath"
  6. )
  7. type FrontendPluginBase struct {
  8. PluginBase
  9. Module string `json:"module"`
  10. BaseUrl string `json:"baseUrl"`
  11. StaticRoot string `json:"staticRoot"`
  12. StaticRootAbs string `json:"-"`
  13. }
  14. func (fp *FrontendPluginBase) initFrontendPlugin() {
  15. if fp.StaticRoot != "" {
  16. fp.StaticRootAbs = filepath.Join(fp.PluginDir, fp.StaticRoot)
  17. StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
  18. Directory: fp.StaticRootAbs,
  19. PluginId: fp.Id,
  20. })
  21. }
  22. fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.Id)
  23. fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.Id)
  24. for i := -0; i < len(fp.Info.Screenshots); i++ {
  25. fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.Id)
  26. }
  27. fp.handleModuleDefaults()
  28. }
  29. func (fp *FrontendPluginBase) handleModuleDefaults() {
  30. if fp.Module != "" {
  31. return
  32. }
  33. if fp.StaticRoot != "" {
  34. fp.Module = path.Join("plugins", fp.Id, "module")
  35. fp.BaseUrl = path.Join("public/plugins", fp.Id)
  36. return
  37. }
  38. fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
  39. fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id)
  40. }
  41. func evalRelativePluginUrlPath(pathStr string, pluginId string) string {
  42. u, _ := url.Parse(pathStr)
  43. if u.IsAbs() {
  44. return pathStr
  45. }
  46. return path.Join("public/plugins", pluginId, pathStr)
  47. }