plugin.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SiYuan - Build Your Eternal Digital Garden
  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. "crypto/sha1"
  19. "fmt"
  20. "os"
  21. "path/filepath"
  22. "sync"
  23. "github.com/88250/gulu"
  24. "github.com/siyuan-note/filelock"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/bazaar"
  27. "github.com/siyuan-note/siyuan/kernel/util"
  28. )
  29. // Petal represents a plugin's management status.
  30. type Petal struct {
  31. ID string `json:"id"` // Plugin ID
  32. Name string `json:"name"` // Plugin name
  33. Enabled bool `json:"enabled"` // Whether enabled
  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) {
  39. petals := getPetals()
  40. plugins := bazaar.InstalledPlugins()
  41. var plugin *bazaar.Plugin
  42. for _, p := range plugins {
  43. if p.Name == name {
  44. plugin = p
  45. break
  46. }
  47. }
  48. if nil == plugin {
  49. logging.LogErrorf("plugin [%s] not found", name)
  50. return
  51. }
  52. petal := getPetalByName(plugin.Name, petals)
  53. if nil == petal {
  54. petal = &Petal{
  55. ID: hash(plugin.Name),
  56. Name: plugin.Name,
  57. Enabled: enabled,
  58. }
  59. petals = append(petals, petal)
  60. } else {
  61. petal.Enabled = enabled
  62. }
  63. savePetals(petals)
  64. }
  65. func LoadPetals() (ret []*Petal) {
  66. ret = []*Petal{}
  67. petals := getPetals()
  68. for _, petal := range petals {
  69. if !petal.Enabled {
  70. continue
  71. }
  72. pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name)
  73. jsPath := filepath.Join(pluginDir, "index.js")
  74. if !gulu.File.IsExist(jsPath) {
  75. logging.LogErrorf("plugin [%s] js not found", petal.Name)
  76. continue
  77. }
  78. data, err := filelock.ReadFile(jsPath)
  79. if nil != err {
  80. logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err)
  81. continue
  82. }
  83. petal.JS = string(data)
  84. cssPath := filepath.Join(pluginDir, "index.css")
  85. if gulu.File.IsExist(cssPath) {
  86. data, err := filelock.ReadFile(cssPath)
  87. if nil != err {
  88. logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err)
  89. } else {
  90. petal.CSS = string(data)
  91. }
  92. }
  93. i18nDir := filepath.Join(pluginDir, "i18n")
  94. if gulu.File.IsDir(i18nDir) {
  95. langJSONs, err := os.ReadDir(i18nDir)
  96. if nil != err {
  97. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
  98. } else {
  99. preferredLang := Conf.Lang + ".json"
  100. foundPreferredLang := false
  101. foundEnUS := false
  102. foundZhCN := false
  103. for _, langJSON := range langJSONs {
  104. if langJSON.Name() == preferredLang {
  105. foundPreferredLang = true
  106. break
  107. }
  108. if langJSON.Name() == "en_US.json" {
  109. foundEnUS = true
  110. }
  111. if langJSON.Name() == "zh_CN.json" {
  112. foundZhCN = true
  113. }
  114. }
  115. if !foundPreferredLang {
  116. if foundEnUS {
  117. preferredLang = "en_US.json"
  118. } else if foundZhCN {
  119. preferredLang = "zh_CN.json"
  120. } else {
  121. preferredLang = langJSONs[0].Name()
  122. }
  123. }
  124. data, err := filelock.ReadFile(filepath.Join(i18nDir, preferredLang))
  125. if nil != err {
  126. logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err)
  127. } else {
  128. petal.I18n = map[string]interface{}{}
  129. if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); nil != err {
  130. logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err)
  131. }
  132. }
  133. }
  134. }
  135. ret = append(ret, petal)
  136. }
  137. return
  138. }
  139. var petalsStoreLock = sync.Mutex{}
  140. func savePetals(petals []*Petal) {
  141. petalsStoreLock.Lock()
  142. defer petalsStoreLock.Unlock()
  143. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  144. confPath := filepath.Join(petalDir, "petals.json")
  145. data, err := gulu.JSON.MarshalIndentJSON(petals, "", "\t")
  146. if nil != err {
  147. logging.LogErrorf("marshal petals failed: %s", err)
  148. return
  149. }
  150. if err = filelock.WriteFile(confPath, data); nil != err {
  151. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  152. return
  153. }
  154. }
  155. func getPetals() (ret []*Petal) {
  156. petalsStoreLock.Lock()
  157. defer petalsStoreLock.Unlock()
  158. ret = []*Petal{}
  159. petalDir := filepath.Join(util.DataDir, "storage", "petal")
  160. if err := os.MkdirAll(petalDir, 0755); nil != err {
  161. logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
  162. return
  163. }
  164. confPath := filepath.Join(petalDir, "petals.json")
  165. if !gulu.File.IsExist(confPath) {
  166. data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")
  167. if nil != err {
  168. logging.LogErrorf("marshal petals failed: %s", err)
  169. return
  170. }
  171. if err = filelock.WriteFile(confPath, data); nil != err {
  172. logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
  173. return
  174. }
  175. return
  176. }
  177. data, err := filelock.ReadFile(confPath)
  178. if nil != err {
  179. logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)
  180. return
  181. }
  182. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  183. logging.LogErrorf("unmarshal petals failed: %s", err)
  184. return
  185. }
  186. return
  187. }
  188. func getPetalByName(name string, petals []*Petal) (ret *Petal) {
  189. for _, p := range petals {
  190. if name == p.Name {
  191. ret = p
  192. break
  193. }
  194. }
  195. return
  196. }
  197. func hash(str string) string {
  198. return fmt.Sprintf("%x", sha1.Sum([]byte(str)))
  199. }