extension.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "bytes"
  19. "io"
  20. "net/url"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "strconv"
  25. "strings"
  26. "github.com/88250/gulu"
  27. "github.com/88250/lute/ast"
  28. "github.com/gin-gonic/gin"
  29. "github.com/siyuan-note/filelock"
  30. "github.com/siyuan-note/logging"
  31. "github.com/siyuan-note/siyuan/kernel/model"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func extensionCopy(c *gin.Context) {
  35. ret := gulu.Ret.NewResult()
  36. defer c.JSON(200, ret)
  37. form, _ := c.MultipartForm()
  38. dom := form.Value["dom"][0]
  39. assets := filepath.Join(util.DataDir, "assets")
  40. if notebookVal := form.Value["notebook"]; 0 < len(notebookVal) {
  41. assets = filepath.Join(util.DataDir, notebookVal[0], "assets")
  42. if !gulu.File.IsDir(assets) {
  43. assets = filepath.Join(util.DataDir, "assets")
  44. }
  45. }
  46. if err := os.MkdirAll(assets, 0755); nil != err {
  47. logging.LogErrorf("create assets folder [%s] failed: %s", assets, err)
  48. ret.Msg = err.Error()
  49. return
  50. }
  51. luteEngine := model.NewLute()
  52. md := luteEngine.HTML2Md(dom)
  53. md = strings.TrimSpace(md)
  54. ret.Data = map[string]interface{}{
  55. "md": md,
  56. }
  57. uploaded := map[string]string{}
  58. for originalName, file := range form.File {
  59. oName, err := url.PathUnescape(originalName)
  60. if nil != err {
  61. if strings.Contains(originalName, "%u") {
  62. originalName = strings.ReplaceAll(originalName, "%u", "\\u")
  63. originalName, err = strconv.Unquote("\"" + originalName + "\"")
  64. if nil != err {
  65. continue
  66. }
  67. oName, err = url.PathUnescape(originalName)
  68. if nil != err {
  69. continue
  70. }
  71. } else {
  72. continue
  73. }
  74. }
  75. u, _ := url.Parse(oName)
  76. if "" == u.Path {
  77. continue
  78. }
  79. fName := path.Base(u.Path)
  80. fName = util.FilterUploadFileName(fName)
  81. f, err := file[0].Open()
  82. if nil != err {
  83. ret.Code = -1
  84. ret.Msg = err.Error()
  85. break
  86. }
  87. data, err := io.ReadAll(f)
  88. if nil != err {
  89. ret.Code = -1
  90. ret.Msg = err.Error()
  91. break
  92. }
  93. ext := path.Ext(fName)
  94. fName = fName[0 : len(fName)-len(ext)]
  95. if "" == ext && bytes.HasPrefix(data, []byte("<svg ")) && bytes.HasSuffix(data, []byte("</svg>")) {
  96. ext = ".svg"
  97. }
  98. fName = fName + "-" + ast.NewNodeID() + ext
  99. writePath := filepath.Join(assets, fName)
  100. if err = filelock.WriteFile(writePath, data); nil != err {
  101. ret.Code = -1
  102. ret.Msg = err.Error()
  103. break
  104. }
  105. uploaded[oName] = "assets/" + fName
  106. }
  107. for k, v := range uploaded {
  108. if "" == md {
  109. // 复制单个图片的情况
  110. md = "![](" + v + ")"
  111. break
  112. }
  113. md = strings.ReplaceAll(md, "]("+k+")", "]("+v+")")
  114. p, err := url.Parse(k)
  115. if nil != err {
  116. continue
  117. }
  118. md = strings.ReplaceAll(md, "]("+p.Path+")", "]("+v+")")
  119. }
  120. ret.Data = map[string]interface{}{
  121. "md": md,
  122. }
  123. ret.Msg = model.Conf.Language(72)
  124. }