plugin.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. return
  58. }
  59. savePetals(petals)
  60. loadCode(ret)
  61. return
  62. }
  63. func LoadPetals(frontend string) (ret []*Petal) {
  64. ret = []*Petal{}
  65. if Conf.Bazaar.PetalDisabled {
  66. return
  67. }
  68. petals := getPetals()
  69. for _, petal := range petals {
  70. _, petal.DisplayName, petal.Incompatible = bazaar.ParseInstalledPlugin(petal.Name, frontend)
  71. if !petal.Enabled || petal.Incompatible {
  72. continue
  73. }
  74. loadCode(petal)
  75. ret = append(ret, petal)
  76. }
  77. return
  78. }
  79. func loadCode(petal *Petal) {
  80. pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
  81. jsPath := filepath.Join(pluginDir, "index.js")
  82. if !gulu.File.IsExist(jsPath) {
  83. logging.LogErrorf("plugin [%s] js not found", petal.Name)
  84. return
  85. }
  86. data, err := filelock.ReadFile(jsPath)
  87. if nil != err {
  88. logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
  89. return
  90. }
  91. petal.JS = string(data)
  92. cssPath := filepath.Join(pluginDir, "index.css")
  93. if gulu.File.IsExist(cssPath) {
  94. data, err = filelock.ReadFile(cssPath)
  95. if nil != err {
  96. logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
  97. } else {
  98. petal.CSS = string(data)
  99. }
  100. }
  101. i18nDir := filepath.Join(pluginDir, "i18n")
  102. if gulu.File.IsDir(i18nDir) {
  103. langJSONs, readErr := os.ReadDir(i18nDir)
  104. if nil != readErr {
  105. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, readErr)
  106. } else {
  107. preferredLang := Conf.Lang + ".json"
  108. foundPreferredLang := false
  109. foundEnUS := false
  110. foundZhCN := false
  111. for _, langJSON := range langJSONs {
  112. if langJSON.Name() == preferredLang {
  113. foundPreferredLang = true
  114. break
  115. }
  116. if langJSON.Name() == "en_US.json" {
  117. foundEnUS = true
  118. }
  119. if langJSON.Name() == "zh_CN.json" {
  120. foundZhCN = true
  121. }
  122. }
  123. if !foundPreferredLang {
  124. if foundEnUS {
  125. preferredLang = "en_US.json"
  126. if "zh_CHT" == Conf.Lang && foundZhCN {
  127. // Improve marketplace package for traditional Chinese https://github.com/siyuan-note/siyuan/issues/8342
  128. preferredLang = "zh_CN.json"
  129. }
  130. } else if foundZhCN {
  131. preferredLang = "zh_CN.json"
  132. } else {
  133. preferredLang = langJSONs[0].Name()
  134. }
  135. }
  136. data, err = filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
  137. if nil != err {
  138. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
  139. } else {
  140. petal.I18n = map[string]interface{}{}
  141. if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
  142. logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
  143. }
  144. }
  145. }
  146. }
  147. }
  148. var petalsStoreLock = sync.Mutex{}
  149. func savePetals(petals []*Petal) {
  150. petalsStoreLock.Lock()
  151. defer petalsStoreLock.Unlock()
  152. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  153. confPath := filepath.Join(petalDir, "petals.json")
  154. data, err := gulu.JSON.MarshalIndentJSON(petals, "", "\t")
  155. if nil != err {
  156. logging.LogErrorf("marshal petals failed: %s", err)
  157. return
  158. }
  159. if err = filelock.WriteFile(confPath, data); nil != err {
  160. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  161. return
  162. }
  163. }
  164. func getPetals() (ret []*Petal) {
  165. petalsStoreLock.Lock()
  166. defer petalsStoreLock.Unlock()
  167. ret = []*Petal{}
  168. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  169. if err := os.MkdirAll(petalDir, 0755); nil != err {
  170. logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
  171. return
  172. }
  173. confPath := filepath.Join(petalDir, "petals.json")
  174. if !gulu.File.IsExist(confPath) {
  175. data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")
  176. if nil != err {
  177. logging.LogErrorf("marshal petals failed: %s", err)
  178. return
  179. }
  180. if err = filelock.WriteFile(confPath, data); nil != err {
  181. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  182. return
  183. }
  184. return
  185. }
  186. data, err := filelock.ReadFile(confPath)
  187. if nil != err {
  188. logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)
  189. return
  190. }
  191. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  192. logging.LogErrorf("unmarshal petals failed: %s", err)
  193. return
  194. }
  195. return
  196. }
  197. func getPetalByName(name string, petals []*Petal) (ret *Petal) {
  198. for _, p := range petals {
  199. if name == p.Name {
  200. ret = p
  201. break
  202. }
  203. }
  204. return
  205. }