frontend_plugin.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. StaticRoot string `json:"staticRoot"`
  11. StaticRootAbs string `json:"-"`
  12. }
  13. func (fp *FrontendPluginBase) initFrontendPlugin() {
  14. if fp.StaticRoot != "" {
  15. fp.StaticRootAbs = filepath.Join(fp.PluginDir, fp.StaticRoot)
  16. StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
  17. Directory: fp.StaticRootAbs,
  18. PluginId: fp.Id,
  19. })
  20. }
  21. fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.Id)
  22. fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.Id)
  23. for i := -0; i < len(fp.Info.Screenshots); i++ {
  24. fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.Id)
  25. }
  26. fp.handleModuleDefaults()
  27. }
  28. func (fp *FrontendPluginBase) handleModuleDefaults() {
  29. if fp.Module != "" {
  30. return
  31. }
  32. if fp.StaticRoot != "" {
  33. fp.Module = path.Join("plugins", fp.Id, "module")
  34. return
  35. }
  36. fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
  37. }
  38. func evalRelativePluginUrlPath(pathStr string, pluginId string) string {
  39. u, _ := url.Parse(pathStr)
  40. if u.IsAbs() {
  41. return pathStr
  42. }
  43. return path.Join("public/plugins", pluginId, pathStr)
  44. }