import.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 err != nil {
  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 err != nil {
  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); err != nil {
  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 err != nil {
  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); err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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. logging.LogInfof("import data [name=%s, size=%d]", file.Filename, file.Size)
  124. fileReader, err := file.Open()
  125. if err != nil {
  126. logging.LogErrorf("open upload file failed: %s", err)
  127. ret.Code = -1
  128. ret.Msg = "open file failed"
  129. return
  130. }
  131. _, err = io.Copy(dataZipFile, fileReader)
  132. if err != nil {
  133. logging.LogErrorf("read upload file failed: %s", err)
  134. ret.Code = -1
  135. ret.Msg = "read file failed"
  136. return
  137. }
  138. if err = dataZipFile.Close(); err != nil {
  139. logging.LogErrorf("close file failed: %s", err)
  140. ret.Code = -1
  141. ret.Msg = "close file failed"
  142. return
  143. }
  144. fileReader.Close()
  145. err = model.ImportData(dataZipPath)
  146. if err != nil {
  147. ret.Code = -1
  148. ret.Msg = err.Error()
  149. return
  150. }
  151. }
  152. func importStdMd(c *gin.Context) {
  153. ret := gulu.Ret.NewResult()
  154. defer c.JSON(http.StatusOK, ret)
  155. arg, ok := util.JsonArg(c, ret)
  156. if !ok {
  157. return
  158. }
  159. notebook := arg["notebook"].(string)
  160. localPath := arg["localPath"].(string)
  161. toPath := arg["toPath"].(string)
  162. err := model.ImportFromLocalPath(notebook, localPath, toPath)
  163. if err != nil {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. return
  167. }
  168. }