snippet.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 api
  17. import (
  18. "mime"
  19. "net/http"
  20. "path/filepath"
  21. "strings"
  22. "github.com/88250/gulu"
  23. "github.com/88250/lute/ast"
  24. "github.com/gin-gonic/gin"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/conf"
  27. "github.com/siyuan-note/siyuan/kernel/model"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func serveSnippets(c *gin.Context) {
  31. filePath := strings.TrimPrefix(c.Request.URL.Path, "/snippets/")
  32. ext := filepath.Ext(filePath)
  33. name := strings.TrimSuffix(filePath, ext)
  34. confSnippets, err := model.LoadSnippets()
  35. if nil != err {
  36. logging.LogErrorf("load snippets failed: %s", name, err)
  37. c.Status(404)
  38. return
  39. }
  40. for _, s := range confSnippets {
  41. if s.Name == name && ("" != ext && s.Type == ext[1:]) {
  42. c.Header("Content-Type", mime.TypeByExtension(ext))
  43. c.String(http.StatusOK, s.Content)
  44. return
  45. }
  46. }
  47. // 没有在配置文件中命中时在文件系统上查找
  48. filePath = filepath.Join(util.SnippetsPath, filePath)
  49. c.File(filePath)
  50. }
  51. func getSnippet(c *gin.Context) {
  52. ret := gulu.Ret.NewResult()
  53. defer c.JSON(http.StatusOK, ret)
  54. arg, ok := util.JsonArg(c, ret)
  55. if !ok {
  56. return
  57. }
  58. typ := arg["type"].(string) // js/css/all
  59. enabledArg := int(arg["enabled"].(float64)) // 0:禁用,1:启用,2:全部
  60. enabled := true
  61. if 0 == enabledArg {
  62. enabled = false
  63. }
  64. confSnippets, err := model.LoadSnippets()
  65. if nil != err {
  66. ret.Code = -1
  67. ret.Msg = "load snippets failed: " + err.Error()
  68. return
  69. }
  70. var snippets []*conf.Snippet
  71. for _, s := range confSnippets {
  72. if ("all" == typ || s.Type == typ) && (2 == enabledArg || s.Enabled == enabled) {
  73. snippets = append(snippets, s)
  74. }
  75. }
  76. if 1 > len(snippets) {
  77. snippets = []*conf.Snippet{}
  78. }
  79. ret.Data = map[string]interface{}{
  80. "snippets": snippets,
  81. }
  82. }
  83. func setSnippet(c *gin.Context) {
  84. ret := gulu.Ret.NewResult()
  85. defer c.JSON(http.StatusOK, ret)
  86. arg, ok := util.JsonArg(c, ret)
  87. if !ok {
  88. return
  89. }
  90. snippetsArg := arg["snippets"].([]interface{})
  91. var snippets []*conf.Snippet
  92. for _, s := range snippetsArg {
  93. m := s.(map[string]interface{})
  94. snippet := &conf.Snippet{
  95. ID: m["id"].(string),
  96. Name: m["name"].(string),
  97. Type: m["type"].(string),
  98. Content: m["content"].(string),
  99. Enabled: m["enabled"].(bool),
  100. }
  101. if "" == snippet.ID {
  102. snippet.ID = ast.NewNodeID()
  103. }
  104. snippets = append(snippets, snippet)
  105. }
  106. err := model.SetSnippet(snippets)
  107. if nil != err {
  108. ret.Code = -1
  109. ret.Msg = "set snippet failed: " + err.Error()
  110. return
  111. }
  112. }
  113. func removeSnippet(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. id := arg["id"].(string)
  121. snippet, err := model.RemoveSnippet(id)
  122. if nil != err {
  123. ret.Code = -1
  124. ret.Msg = "remove snippet failed: " + err.Error()
  125. return
  126. }
  127. ret.Data = snippet
  128. }