export.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "net/http"
  19. "path"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func exportDataInFolder(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. exportFolder := arg["folder"].(string)
  33. err := model.ExportDataInFolder(exportFolder)
  34. if nil != err {
  35. ret.Code = -1
  36. ret.Msg = err.Error()
  37. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  38. return
  39. }
  40. }
  41. func exportData(c *gin.Context) {
  42. ret := gulu.Ret.NewResult()
  43. defer c.JSON(http.StatusOK, ret)
  44. zipPath := model.ExportData()
  45. ret.Data = map[string]interface{}{
  46. "zip": zipPath,
  47. }
  48. }
  49. func batchExportMd(c *gin.Context) {
  50. ret := gulu.Ret.NewResult()
  51. defer c.JSON(http.StatusOK, ret)
  52. arg, ok := util.JsonArg(c, ret)
  53. if !ok {
  54. return
  55. }
  56. notebook := arg["notebook"].(string)
  57. p := arg["path"].(string)
  58. zipPath := model.BatchExportMarkdown(notebook, p)
  59. ret.Data = map[string]interface{}{
  60. "name": path.Base(zipPath),
  61. "zip": zipPath,
  62. }
  63. }
  64. func exportMd(c *gin.Context) {
  65. ret := gulu.Ret.NewResult()
  66. defer c.JSON(http.StatusOK, ret)
  67. arg, ok := util.JsonArg(c, ret)
  68. if !ok {
  69. return
  70. }
  71. id := arg["id"].(string)
  72. name, zipPath := model.ExportMarkdown(id)
  73. ret.Data = map[string]interface{}{
  74. "name": name,
  75. "zip": zipPath,
  76. }
  77. }
  78. func exportSY(c *gin.Context) {
  79. ret := gulu.Ret.NewResult()
  80. defer c.JSON(http.StatusOK, ret)
  81. arg, ok := util.JsonArg(c, ret)
  82. if !ok {
  83. return
  84. }
  85. id := arg["id"].(string)
  86. name, zipPath := model.ExportSY(id)
  87. ret.Data = map[string]interface{}{
  88. "name": name,
  89. "zip": zipPath,
  90. }
  91. }
  92. func exportMdContent(c *gin.Context) {
  93. ret := gulu.Ret.NewResult()
  94. defer c.JSON(http.StatusOK, ret)
  95. arg, ok := util.JsonArg(c, ret)
  96. if !ok {
  97. return
  98. }
  99. id := arg["id"].(string)
  100. hPath, content := model.ExportMarkdownContent(id)
  101. ret.Data = map[string]interface{}{
  102. "hPath": hPath,
  103. "content": content,
  104. }
  105. }
  106. func exportDocx(c *gin.Context) {
  107. ret := gulu.Ret.NewResult()
  108. defer c.JSON(http.StatusOK, ret)
  109. arg, ok := util.JsonArg(c, ret)
  110. if !ok {
  111. return
  112. }
  113. id := arg["id"].(string)
  114. savePath := arg["savePath"].(string)
  115. removeAssets := arg["removeAssets"].(bool)
  116. err := model.ExportDocx(id, savePath, removeAssets)
  117. if nil != err {
  118. ret.Code = 1
  119. ret.Msg = err.Error()
  120. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  121. return
  122. }
  123. }
  124. func exportMdHTML(c *gin.Context) {
  125. ret := gulu.Ret.NewResult()
  126. defer c.JSON(http.StatusOK, ret)
  127. arg, ok := util.JsonArg(c, ret)
  128. if !ok {
  129. return
  130. }
  131. id := arg["id"].(string)
  132. savePath := arg["savePath"].(string)
  133. name, content := model.ExportMarkdownHTML(id, savePath, false)
  134. ret.Data = map[string]interface{}{
  135. "id": id,
  136. "name": name,
  137. "content": content,
  138. }
  139. }
  140. func exportHTML(c *gin.Context) {
  141. ret := gulu.Ret.NewResult()
  142. defer c.JSON(http.StatusOK, ret)
  143. arg, ok := util.JsonArg(c, ret)
  144. if !ok {
  145. return
  146. }
  147. id := arg["id"].(string)
  148. pdf := arg["pdf"].(bool)
  149. savePath := arg["savePath"].(string)
  150. name, content := model.ExportHTML(id, savePath, pdf)
  151. ret.Data = map[string]interface{}{
  152. "id": id,
  153. "name": name,
  154. "content": content,
  155. }
  156. }
  157. func addPDFOutline(c *gin.Context) {
  158. ret := gulu.Ret.NewResult()
  159. defer c.JSON(http.StatusOK, ret)
  160. arg, ok := util.JsonArg(c, ret)
  161. if !ok {
  162. return
  163. }
  164. id := arg["id"].(string)
  165. path := arg["path"].(string)
  166. err := model.AddPDFOutline(id, path)
  167. if nil != err {
  168. ret.Code = -1
  169. ret.Msg = err.Error()
  170. return
  171. }
  172. }
  173. func exportPreview(c *gin.Context) {
  174. ret := gulu.Ret.NewResult()
  175. defer c.JSON(http.StatusOK, ret)
  176. arg, ok := util.JsonArg(c, ret)
  177. if !ok {
  178. return
  179. }
  180. id := arg["id"].(string)
  181. stdHTML := model.Preview(id)
  182. ret.Data = map[string]interface{}{
  183. "html": stdHTML,
  184. }
  185. }