export.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. err := model.ExportDocx(id, savePath)
  116. if nil != err {
  117. ret.Code = 1
  118. ret.Msg = err.Error()
  119. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  120. return
  121. }
  122. }
  123. func exportMdHTML(c *gin.Context) {
  124. ret := gulu.Ret.NewResult()
  125. defer c.JSON(http.StatusOK, ret)
  126. arg, ok := util.JsonArg(c, ret)
  127. if !ok {
  128. return
  129. }
  130. id := arg["id"].(string)
  131. savePath := arg["savePath"].(string)
  132. name, content := model.ExportMarkdownHTML(id, savePath, false)
  133. ret.Data = map[string]interface{}{
  134. "id": id,
  135. "name": name,
  136. "content": content,
  137. }
  138. }
  139. func exportHTML(c *gin.Context) {
  140. ret := gulu.Ret.NewResult()
  141. defer c.JSON(http.StatusOK, ret)
  142. arg, ok := util.JsonArg(c, ret)
  143. if !ok {
  144. return
  145. }
  146. id := arg["id"].(string)
  147. pdf := arg["pdf"].(bool)
  148. savePath := arg["savePath"].(string)
  149. name, content := model.ExportHTML(id, savePath, pdf)
  150. ret.Data = map[string]interface{}{
  151. "id": id,
  152. "name": name,
  153. "content": content,
  154. }
  155. }
  156. func addPDFOutline(c *gin.Context) {
  157. ret := gulu.Ret.NewResult()
  158. defer c.JSON(http.StatusOK, ret)
  159. arg, ok := util.JsonArg(c, ret)
  160. if !ok {
  161. return
  162. }
  163. id := arg["id"].(string)
  164. path := arg["path"].(string)
  165. err := model.AddPDFOutline(id, path)
  166. if nil != err {
  167. ret.Code = -1
  168. ret.Msg = err.Error()
  169. return
  170. }
  171. }
  172. func exportPreview(c *gin.Context) {
  173. ret := gulu.Ret.NewResult()
  174. defer c.JSON(http.StatusOK, ret)
  175. arg, ok := util.JsonArg(c, ret)
  176. if !ok {
  177. return
  178. }
  179. id := arg["id"].(string)
  180. stdHTML := model.Preview(id)
  181. ret.Data = map[string]interface{}{
  182. "html": stdHTML,
  183. }
  184. }