plugin.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package model
  17. import (
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "sync"
  22. "github.com/88250/gulu"
  23. "github.com/siyuan-note/filelock"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/bazaar"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. // Petal represents a plugin's management status.
  29. type Petal struct {
  30. Name string `json:"name"` // Plugin name
  31. DisplayName string `json:"displayName"` // Plugin display name
  32. Enabled bool `json:"enabled"` // Whether enabled
  33. Incompatible bool `json:"incompatible"` // Whether incompatible
  34. JS string `json:"js"` // JS code
  35. CSS string `json:"css"` // CSS code
  36. I18n map[string]interface{} `json:"i18n"` // i18n text
  37. }
  38. func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, err error) {
  39. petals := getPetals()
  40. found, displayName, incompatible := bazaar.ParseInstalledPlugin(name, frontend)
  41. if !found {
  42. logging.LogErrorf("plugin [%s] not found", name)
  43. return
  44. }
  45. ret = getPetalByName(name, petals)
  46. if nil == ret {
  47. ret = &Petal{
  48. Name: name,
  49. }
  50. petals = append(petals, ret)
  51. }
  52. ret.DisplayName = displayName
  53. ret.Enabled = enabled
  54. ret.Incompatible = incompatible
  55. if incompatible {
  56. err = fmt.Errorf(Conf.Language(205))
  57. logging.LogInfof("plugin [%s] is incompatible [%s]", name, frontend)
  58. return
  59. }
  60. savePetals(petals)
  61. loadCode(ret)
  62. return
  63. }
  64. func LoadPetals(frontend string) (ret []*Petal) {
  65. ret = []*Petal{}
  66. if Conf.Bazaar.PetalDisabled {
  67. return
  68. }
  69. if !Conf.Bazaar.Trust {
  70. // 移动端没有集市模块,所以要默认开启,桌面端和 Docker 容器需要用户手动确认过信任后才能开启
  71. if util.ContainerStd == util.Container || util.ContainerDocker == util.Container {
  72. return
  73. }
  74. }
  75. petals := getPetals()
  76. for _, petal := range petals {
  77. installPath := filepath.Join(util.DataDir, "plugins", petal.Name)
  78. if !filelock.IsExist(installPath) {
  79. continue
  80. }
  81. _, petal.DisplayName, petal.Incompatible = bazaar.ParseInstalledPlugin(petal.Name, frontend)
  82. if !petal.Enabled || petal.Incompatible {
  83. continue
  84. }
  85. loadCode(petal)
  86. ret = append(ret, petal)
  87. }
  88. return
  89. }
  90. func loadCode(petal *Petal) {
  91. pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
  92. jsPath := filepath.Join(pluginDir, "index.js")
  93. if !filelock.IsExist(jsPath) {
  94. logging.LogErrorf("plugin [%s] js not found", petal.Name)
  95. return
  96. }
  97. data, err := filelock.ReadFile(jsPath)
  98. if nil != err {
  99. logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
  100. return
  101. }
  102. petal.JS = string(data)
  103. cssPath := filepath.Join(pluginDir, "index.css")
  104. if filelock.IsExist(cssPath) {
  105. data, err = filelock.ReadFile(cssPath)
  106. if nil != err {
  107. logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
  108. } else {
  109. petal.CSS = string(data)
  110. }
  111. }
  112. i18nDir := filepath.Join(pluginDir, "i18n")
  113. if gulu.File.IsDir(i18nDir) {
  114. langJSONs, readErr := os.ReadDir(i18nDir)
  115. if nil != readErr {
  116. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, readErr)
  117. } else {
  118. preferredLang := Conf.Lang + ".json"
  119. foundPreferredLang := false
  120. foundEnUS := false
  121. foundZhCN := false
  122. for _, langJSON := range langJSONs {
  123. if langJSON.Name() == preferredLang {
  124. foundPreferredLang = true
  125. break
  126. }
  127. if langJSON.Name() == "en_US.json" {
  128. foundEnUS = true
  129. }
  130. if langJSON.Name() == "zh_CN.json" {
  131. foundZhCN = true
  132. }
  133. }
  134. if !foundPreferredLang {
  135. if foundEnUS {
  136. preferredLang = "en_US.json"
  137. if "zh_CHT" == Conf.Lang && foundZhCN {
  138. // Improve marketplace package for traditional Chinese https://github.com/siyuan-note/siyuan/issues/8342
  139. preferredLang = "zh_CN.json"
  140. }
  141. } else if foundZhCN {
  142. preferredLang = "zh_CN.json"
  143. } else {
  144. preferredLang = langJSONs[0].Name()
  145. }
  146. }
  147. data, err = filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
  148. if nil != err {
  149. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
  150. } else {
  151. petal.I18n = map[string]interface{}{}
  152. if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
  153. logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
  154. }
  155. }
  156. }
  157. }
  158. }
  159. var petalsStoreLock = sync.Mutex{}
  160. func savePetals(petals []*Petal) {
  161. petalsStoreLock.Lock()
  162. defer petalsStoreLock.Unlock()
  163. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  164. confPath := filepath.Join(petalDir, "petals.json")
  165. data, err := gulu.JSON.MarshalIndentJSON(petals, "", "\t")
  166. if nil != err {
  167. logging.LogErrorf("marshal petals failed: %s", err)
  168. return
  169. }
  170. if err = filelock.WriteFile(confPath, data); nil != err {
  171. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  172. return
  173. }
  174. }
  175. func getPetals() (ret []*Petal) {
  176. petalsStoreLock.Lock()
  177. defer petalsStoreLock.Unlock()
  178. ret = []*Petal{}
  179. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  180. if err := os.MkdirAll(petalDir, 0755); nil != err {
  181. logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
  182. return
  183. }
  184. confPath := filepath.Join(petalDir, "petals.json")
  185. if !filelock.IsExist(confPath) {
  186. data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")
  187. if nil != err {
  188. logging.LogErrorf("marshal petals failed: %s", err)
  189. return
  190. }
  191. if err = filelock.WriteFile(confPath, data); nil != err {
  192. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  193. return
  194. }
  195. return
  196. }
  197. data, err := filelock.ReadFile(confPath)
  198. if nil != err {
  199. logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)
  200. return
  201. }
  202. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  203. logging.LogErrorf("unmarshal petals failed: %s", err)
  204. return
  205. }
  206. return
  207. }
  208. func getPetalByName(name string, petals []*Petal) (ret *Petal) {
  209. for _, p := range petals {
  210. if name == p.Name {
  211. ret = p
  212. break
  213. }
  214. }
  215. return
  216. }