import.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "io"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "github.com/88250/gulu"
  23. "github.com/gin-gonic/gin"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/model"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. func importSY(c *gin.Context) {
  29. ret := gulu.Ret.NewResult()
  30. defer c.JSON(200, ret)
  31. util.PushEndlessProgress(model.Conf.Language(73))
  32. defer util.ClearPushProgress(100)
  33. form, err := c.MultipartForm()
  34. if nil != err {
  35. logging.LogErrorf("parse import .sy.zip failed: %s", err)
  36. ret.Code = -1
  37. ret.Msg = err.Error()
  38. return
  39. }
  40. files := form.File["file"]
  41. if 1 > len(files) {
  42. logging.LogErrorf("parse import .sy.zip failed, no file found")
  43. ret.Code = -1
  44. ret.Msg = "no file found"
  45. return
  46. }
  47. file := files[0]
  48. reader, err := file.Open()
  49. if nil != err {
  50. logging.LogErrorf("read import .sy.zip failed: %s", err)
  51. ret.Code = -1
  52. ret.Msg = err.Error()
  53. return
  54. }
  55. importDir := filepath.Join(util.TempDir, "import")
  56. if err = os.MkdirAll(importDir, 0755); nil != err {
  57. logging.LogErrorf("make import dir [%s] failed: %s", importDir, err)
  58. ret.Code = -1
  59. ret.Msg = err.Error()
  60. return
  61. }
  62. writePath := filepath.Join(util.TempDir, "import", file.Filename)
  63. defer os.RemoveAll(writePath)
  64. writer, err := os.OpenFile(writePath, os.O_RDWR|os.O_CREATE, 0644)
  65. if nil != err {
  66. logging.LogErrorf("open import .sy.zip [%s] failed: %s", writePath, err)
  67. ret.Code = -1
  68. ret.Msg = err.Error()
  69. return
  70. }
  71. if _, err = io.Copy(writer, reader); nil != err {
  72. logging.LogErrorf("write import .sy.zip failed: %s", err)
  73. ret.Code = -1
  74. ret.Msg = err.Error()
  75. return
  76. }
  77. writer.Close()
  78. reader.Close()
  79. notebook := form.Value["notebook"][0]
  80. toPath := form.Value["toPath"][0]
  81. err = model.ImportSY(writePath, notebook, toPath)
  82. if nil != err {
  83. ret.Code = -1
  84. ret.Msg = err.Error()
  85. return
  86. }
  87. }
  88. func importData(c *gin.Context) {
  89. ret := gulu.Ret.NewResult()
  90. defer c.JSON(http.StatusOK, ret)
  91. util.PushEndlessProgress(model.Conf.Language(73))
  92. defer util.ClearPushProgress(100)
  93. form, err := c.MultipartForm()
  94. if nil != err {
  95. logging.LogErrorf("import data failed: %s", err)
  96. ret.Code = -1
  97. ret.Msg = err.Error()
  98. return
  99. }
  100. if 1 > len(form.File["file"]) {
  101. logging.LogErrorf("import data failed: %s", err)
  102. ret.Code = -1
  103. ret.Msg = "file not found"
  104. return
  105. }
  106. tmpImport := filepath.Join(util.TempDir, "import")
  107. err = os.MkdirAll(tmpImport, 0755)
  108. if nil != err {
  109. ret.Code = -1
  110. ret.Msg = "create temp import dir failed"
  111. return
  112. }
  113. dataZipPath := filepath.Join(tmpImport, util.CurrentTimeSecondsStr()+".zip")
  114. defer os.RemoveAll(dataZipPath)
  115. dataZipFile, err := os.Create(dataZipPath)
  116. if nil != err {
  117. logging.LogErrorf("create temp file failed: %s", err)
  118. ret.Code = -1
  119. ret.Msg = "create temp file failed"
  120. return
  121. }
  122. file := form.File["file"][0]
  123. fileReader, err := file.Open()
  124. if nil != err {
  125. logging.LogErrorf("open upload file failed: %s", err)
  126. ret.Code = -1
  127. ret.Msg = "open file failed"
  128. return
  129. }
  130. _, err = io.Copy(dataZipFile, fileReader)
  131. if nil != err {
  132. logging.LogErrorf("read upload file failed: %s", err)
  133. ret.Code = -1
  134. ret.Msg = "read file failed"
  135. return
  136. }
  137. if err = dataZipFile.Close(); nil != err {
  138. logging.LogErrorf("close file failed: %s", err)
  139. ret.Code = -1
  140. ret.Msg = "close file failed"
  141. return
  142. }
  143. fileReader.Close()
  144. err = model.ImportData(dataZipPath)
  145. if nil != err {
  146. ret.Code = -1
  147. ret.Msg = err.Error()
  148. return
  149. }
  150. }
  151. func importStdMd(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. arg, ok := util.JsonArg(c, ret)
  155. if !ok {
  156. return
  157. }
  158. notebook := arg["notebook"].(string)
  159. localPath := arg["localPath"].(string)
  160. toPath := arg["toPath"].(string)
  161. err := model.ImportFromLocalPath(notebook, localPath, toPath)
  162. if nil != err {
  163. ret.Code = -1
  164. ret.Msg = err.Error()
  165. return
  166. }
  167. }