snippet.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 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. filePath = filepath.Join(util.SnippetsPath, filePath)
  48. c.File(filePath)
  49. }
  50. func getSnippet(c *gin.Context) {
  51. ret := gulu.Ret.NewResult()
  52. defer c.JSON(http.StatusOK, ret)
  53. arg, ok := util.JsonArg(c, ret)
  54. if !ok {
  55. return
  56. }
  57. typ := arg["type"].(string) // js/css/all
  58. enabledArg := int(arg["enabled"].(float64)) // 0:禁用,1:启用,2:全部
  59. enabled := true
  60. if 0 == enabledArg {
  61. enabled = false
  62. }
  63. confSnippets, err := model.LoadSnippets()
  64. if nil != err {
  65. ret.Code = -1
  66. ret.Msg = "load snippets failed: " + err.Error()
  67. return
  68. }
  69. var snippets []*conf.Snippet
  70. for _, s := range confSnippets {
  71. if ("all" == typ || s.Type == typ) && (2 == enabledArg || s.Enabled == enabled) {
  72. snippets = append(snippets, s)
  73. }
  74. }
  75. if 1 > len(snippets) {
  76. snippets = []*conf.Snippet{}
  77. }
  78. ret.Data = map[string]interface{}{
  79. "snippets": snippets,
  80. }
  81. }
  82. func setSnippet(c *gin.Context) {
  83. ret := gulu.Ret.NewResult()
  84. defer c.JSON(http.StatusOK, ret)
  85. arg, ok := util.JsonArg(c, ret)
  86. if !ok {
  87. return
  88. }
  89. snippetsArg := arg["snippets"].([]interface{})
  90. var snippets []*conf.Snippet
  91. for _, s := range snippetsArg {
  92. m := s.(map[string]interface{})
  93. snippet := &conf.Snippet{
  94. ID: m["id"].(string),
  95. Name: m["name"].(string),
  96. Type: m["type"].(string),
  97. Content: m["content"].(string),
  98. Enabled: m["enabled"].(bool),
  99. }
  100. if "" == snippet.ID {
  101. snippet.ID = ast.NewNodeID()
  102. }
  103. snippets = append(snippets, snippet)
  104. }
  105. err := model.SetSnippet(snippets)
  106. if nil != err {
  107. ret.Code = -1
  108. ret.Msg = "set snippet failed: " + err.Error()
  109. return
  110. }
  111. }
  112. func removeSnippet(c *gin.Context) {
  113. ret := gulu.Ret.NewResult()
  114. defer c.JSON(http.StatusOK, ret)
  115. arg, ok := util.JsonArg(c, ret)
  116. if !ok {
  117. return
  118. }
  119. id := arg["id"].(string)
  120. snippet, err := model.RemoveSnippet(id)
  121. if nil != err {
  122. ret.Code = -1
  123. ret.Msg = "remove snippet failed: " + err.Error()
  124. return
  125. }
  126. ret.Data = snippet
  127. }