snippet.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "path/filepath"
  19. "sync"
  20. "github.com/88250/gulu"
  21. "github.com/siyuan-note/filelock"
  22. "github.com/siyuan-note/logging"
  23. "github.com/siyuan-note/siyuan/kernel/conf"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. var snippetsLock = sync.Mutex{}
  27. func RemoveSnippet(id string) (ret *conf.Snippet, err error) {
  28. snippetsLock.Lock()
  29. defer snippetsLock.Unlock()
  30. snippets, err := loadSnippets()
  31. if nil != err {
  32. return
  33. }
  34. for i, s := range snippets {
  35. if s.ID == id {
  36. ret = s
  37. snippets = append(snippets[:i], snippets[i+1:]...)
  38. break
  39. }
  40. }
  41. err = writeSnippetsConf(snippets)
  42. return
  43. }
  44. func SetSnippet(id, name, typ, content string, enabled bool) (ret *conf.Snippet, err error) {
  45. snippetsLock.Lock()
  46. defer snippetsLock.Unlock()
  47. snippets, err := loadSnippets()
  48. if nil != err {
  49. return
  50. }
  51. isUpdate := false
  52. for _, s := range snippets {
  53. if s.ID == id {
  54. s.Name = name
  55. s.Type = typ
  56. s.Content = content
  57. s.Enabled = enabled
  58. ret = s
  59. isUpdate = true
  60. break
  61. }
  62. }
  63. if !isUpdate {
  64. ret = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled}
  65. snippets = append(snippets, ret)
  66. }
  67. err = writeSnippetsConf(snippets)
  68. return
  69. }
  70. func LoadSnippets() (ret []*conf.Snippet, err error) {
  71. snippetsLock.Lock()
  72. defer snippetsLock.Unlock()
  73. return loadSnippets()
  74. }
  75. func loadSnippets() (ret []*conf.Snippet, err error) {
  76. ret = []*conf.Snippet{}
  77. confPath := filepath.Join(util.DataDir, "snippets/conf.json")
  78. if !gulu.File.IsExist(confPath) {
  79. return
  80. }
  81. data, err := filelock.ReadFile(confPath)
  82. if nil != err {
  83. logging.LogErrorf("load js snippets failed: %s", err)
  84. return
  85. }
  86. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  87. logging.LogErrorf("unmarshal js snippets failed: %s", err)
  88. return
  89. }
  90. needRewrite := false
  91. for _, snippet := range ret {
  92. if "" == snippet.ID {
  93. snippet.ID = gulu.Rand.String(12)
  94. needRewrite = true
  95. }
  96. }
  97. if needRewrite {
  98. writeSnippetsConf(ret)
  99. }
  100. return
  101. }
  102. func writeSnippetsConf(snippets []*conf.Snippet) (err error) {
  103. data, err := gulu.JSON.MarshalIndentJSON(snippets, "", " ")
  104. if nil != err {
  105. logging.LogErrorf("marshal snippets failed: %s", err)
  106. return
  107. }
  108. confPath := filepath.Join(util.DataDir, "snippets/conf.json")
  109. err = filelock.WriteFile(confPath, data)
  110. return
  111. }