export.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. "io"
  19. "net/http"
  20. "os"
  21. "path"
  22. "path/filepath"
  23. "strings"
  24. "github.com/88250/gulu"
  25. "github.com/gin-gonic/gin"
  26. "github.com/siyuan-note/logging"
  27. "github.com/siyuan-note/siyuan/kernel/model"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func exportEPUB(c *gin.Context) {
  31. ret := gulu.Ret.NewResult()
  32. defer c.JSON(http.StatusOK, ret)
  33. arg, ok := util.JsonArg(c, ret)
  34. if !ok {
  35. return
  36. }
  37. id := arg["id"].(string)
  38. name, zipPath := model.ExportPandocConvertZip(id, "epub", ".epub")
  39. ret.Data = map[string]interface{}{
  40. "name": name,
  41. "zip": zipPath,
  42. }
  43. }
  44. func exportRTF(c *gin.Context) {
  45. ret := gulu.Ret.NewResult()
  46. defer c.JSON(http.StatusOK, ret)
  47. arg, ok := util.JsonArg(c, ret)
  48. if !ok {
  49. return
  50. }
  51. id := arg["id"].(string)
  52. name, zipPath := model.ExportPandocConvertZip(id, "rtf", ".rtf")
  53. ret.Data = map[string]interface{}{
  54. "name": name,
  55. "zip": zipPath,
  56. }
  57. }
  58. func exportODT(c *gin.Context) {
  59. ret := gulu.Ret.NewResult()
  60. defer c.JSON(http.StatusOK, ret)
  61. arg, ok := util.JsonArg(c, ret)
  62. if !ok {
  63. return
  64. }
  65. id := arg["id"].(string)
  66. name, zipPath := model.ExportPandocConvertZip(id, "odt", ".odt")
  67. ret.Data = map[string]interface{}{
  68. "name": name,
  69. "zip": zipPath,
  70. }
  71. }
  72. func exportMediaWiki(c *gin.Context) {
  73. ret := gulu.Ret.NewResult()
  74. defer c.JSON(http.StatusOK, ret)
  75. arg, ok := util.JsonArg(c, ret)
  76. if !ok {
  77. return
  78. }
  79. id := arg["id"].(string)
  80. name, zipPath := model.ExportPandocConvertZip(id, "mediawiki", ".wiki")
  81. ret.Data = map[string]interface{}{
  82. "name": name,
  83. "zip": zipPath,
  84. }
  85. }
  86. func exportOrgMode(c *gin.Context) {
  87. ret := gulu.Ret.NewResult()
  88. defer c.JSON(http.StatusOK, ret)
  89. arg, ok := util.JsonArg(c, ret)
  90. if !ok {
  91. return
  92. }
  93. id := arg["id"].(string)
  94. name, zipPath := model.ExportPandocConvertZip(id, "org", ".org")
  95. ret.Data = map[string]interface{}{
  96. "name": name,
  97. "zip": zipPath,
  98. }
  99. }
  100. func exportOPML(c *gin.Context) {
  101. ret := gulu.Ret.NewResult()
  102. defer c.JSON(http.StatusOK, ret)
  103. arg, ok := util.JsonArg(c, ret)
  104. if !ok {
  105. return
  106. }
  107. id := arg["id"].(string)
  108. name, zipPath := model.ExportPandocConvertZip(id, "opml", ".opml")
  109. ret.Data = map[string]interface{}{
  110. "name": name,
  111. "zip": zipPath,
  112. }
  113. }
  114. func exportTextile(c *gin.Context) {
  115. ret := gulu.Ret.NewResult()
  116. defer c.JSON(http.StatusOK, ret)
  117. arg, ok := util.JsonArg(c, ret)
  118. if !ok {
  119. return
  120. }
  121. id := arg["id"].(string)
  122. name, zipPath := model.ExportPandocConvertZip(id, "textile", ".textile")
  123. ret.Data = map[string]interface{}{
  124. "name": name,
  125. "zip": zipPath,
  126. }
  127. }
  128. func exportAsciiDoc(c *gin.Context) {
  129. ret := gulu.Ret.NewResult()
  130. defer c.JSON(http.StatusOK, ret)
  131. arg, ok := util.JsonArg(c, ret)
  132. if !ok {
  133. return
  134. }
  135. id := arg["id"].(string)
  136. name, zipPath := model.ExportPandocConvertZip(id, "asciidoc", ".adoc")
  137. ret.Data = map[string]interface{}{
  138. "name": name,
  139. "zip": zipPath,
  140. }
  141. }
  142. func exportReStructuredText(c *gin.Context) {
  143. ret := gulu.Ret.NewResult()
  144. defer c.JSON(http.StatusOK, ret)
  145. arg, ok := util.JsonArg(c, ret)
  146. if !ok {
  147. return
  148. }
  149. id := arg["id"].(string)
  150. name, zipPath := model.ExportPandocConvertZip(id, "rst", ".rst")
  151. ret.Data = map[string]interface{}{
  152. "name": name,
  153. "zip": zipPath,
  154. }
  155. }
  156. func export2Liandi(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. err := model.Export2Liandi(id)
  165. if nil != err {
  166. ret.Code = -1
  167. ret.Msg = err.Error()
  168. return
  169. }
  170. }
  171. func exportDataInFolder(c *gin.Context) {
  172. ret := gulu.Ret.NewResult()
  173. defer c.JSON(http.StatusOK, ret)
  174. arg, ok := util.JsonArg(c, ret)
  175. if !ok {
  176. return
  177. }
  178. exportFolder := arg["folder"].(string)
  179. name, err := model.ExportDataInFolder(exportFolder)
  180. if nil != err {
  181. ret.Code = -1
  182. ret.Msg = err.Error()
  183. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  184. return
  185. }
  186. ret.Data = map[string]interface{}{
  187. "name": name,
  188. }
  189. }
  190. func exportData(c *gin.Context) {
  191. ret := gulu.Ret.NewResult()
  192. defer c.JSON(http.StatusOK, ret)
  193. zipPath, err := model.ExportData()
  194. if nil != err {
  195. ret.Code = 1
  196. ret.Msg = err.Error()
  197. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  198. return
  199. }
  200. ret.Data = map[string]interface{}{
  201. "zip": zipPath,
  202. }
  203. }
  204. func batchExportMd(c *gin.Context) {
  205. ret := gulu.Ret.NewResult()
  206. defer c.JSON(http.StatusOK, ret)
  207. arg, ok := util.JsonArg(c, ret)
  208. if !ok {
  209. return
  210. }
  211. notebook := arg["notebook"].(string)
  212. p := arg["path"].(string)
  213. zipPath := model.BatchExportMarkdown(notebook, p)
  214. ret.Data = map[string]interface{}{
  215. "name": path.Base(zipPath),
  216. "zip": zipPath,
  217. }
  218. }
  219. func exportMd(c *gin.Context) {
  220. ret := gulu.Ret.NewResult()
  221. defer c.JSON(http.StatusOK, ret)
  222. arg, ok := util.JsonArg(c, ret)
  223. if !ok {
  224. return
  225. }
  226. id := arg["id"].(string)
  227. name, zipPath := model.ExportPandocConvertZip(id, "", ".md")
  228. ret.Data = map[string]interface{}{
  229. "name": name,
  230. "zip": zipPath,
  231. }
  232. }
  233. func exportNotebookSY(c *gin.Context) {
  234. ret := gulu.Ret.NewResult()
  235. defer c.JSON(http.StatusOK, ret)
  236. arg, ok := util.JsonArg(c, ret)
  237. if !ok {
  238. return
  239. }
  240. id := arg["id"].(string)
  241. zipPath := model.ExportNotebookSY(id)
  242. ret.Data = map[string]interface{}{
  243. "zip": zipPath,
  244. }
  245. }
  246. func exportSY(c *gin.Context) {
  247. ret := gulu.Ret.NewResult()
  248. defer c.JSON(http.StatusOK, ret)
  249. arg, ok := util.JsonArg(c, ret)
  250. if !ok {
  251. return
  252. }
  253. id := arg["id"].(string)
  254. name, zipPath := model.ExportSY(id)
  255. ret.Data = map[string]interface{}{
  256. "name": name,
  257. "zip": zipPath,
  258. }
  259. }
  260. func exportMdContent(c *gin.Context) {
  261. ret := gulu.Ret.NewResult()
  262. defer c.JSON(http.StatusOK, ret)
  263. arg, ok := util.JsonArg(c, ret)
  264. if !ok {
  265. return
  266. }
  267. id := arg["id"].(string)
  268. if util.InvalidIDPattern(id, ret) {
  269. return
  270. }
  271. hPath, content := model.ExportMarkdownContent(id)
  272. ret.Data = map[string]interface{}{
  273. "hPath": hPath,
  274. "content": content,
  275. }
  276. }
  277. func exportDocx(c *gin.Context) {
  278. ret := gulu.Ret.NewResult()
  279. defer c.JSON(http.StatusOK, ret)
  280. arg, ok := util.JsonArg(c, ret)
  281. if !ok {
  282. return
  283. }
  284. id := arg["id"].(string)
  285. savePath := arg["savePath"].(string)
  286. removeAssets := arg["removeAssets"].(bool)
  287. merge := false
  288. if nil != arg["merge"] {
  289. merge = arg["merge"].(bool)
  290. }
  291. err := model.ExportDocx(id, savePath, removeAssets, merge)
  292. if nil != err {
  293. ret.Code = 1
  294. ret.Msg = err.Error()
  295. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  296. return
  297. }
  298. }
  299. func exportMdHTML(c *gin.Context) {
  300. ret := gulu.Ret.NewResult()
  301. defer c.JSON(http.StatusOK, ret)
  302. arg, ok := util.JsonArg(c, ret)
  303. if !ok {
  304. return
  305. }
  306. id := arg["id"].(string)
  307. savePath := arg["savePath"].(string)
  308. name, content := model.ExportMarkdownHTML(id, savePath, false, false)
  309. ret.Data = map[string]interface{}{
  310. "id": id,
  311. "name": name,
  312. "content": content,
  313. }
  314. }
  315. func exportTempContent(c *gin.Context) {
  316. ret := gulu.Ret.NewResult()
  317. defer c.JSON(http.StatusOK, ret)
  318. arg, ok := util.JsonArg(c, ret)
  319. if !ok {
  320. return
  321. }
  322. content := arg["content"].(string)
  323. tmpExport := filepath.Join(util.TempDir, "export", "temp")
  324. if err := os.MkdirAll(tmpExport, 0755); nil != err {
  325. ret.Code = 1
  326. ret.Msg = err.Error()
  327. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  328. return
  329. }
  330. p := filepath.Join(tmpExport, gulu.Rand.String(7))
  331. if err := os.WriteFile(p, []byte(content), 0644); nil != err {
  332. ret.Code = 1
  333. ret.Msg = err.Error()
  334. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  335. return
  336. }
  337. url := path.Join("/export/temp/", filepath.Base(p))
  338. ret.Data = map[string]interface{}{
  339. "url": "http://" + util.LocalHost + ":" + util.ServerPort + url,
  340. }
  341. }
  342. func exportPreviewHTML(c *gin.Context) {
  343. ret := gulu.Ret.NewResult()
  344. defer c.JSON(http.StatusOK, ret)
  345. arg, ok := util.JsonArg(c, ret)
  346. if !ok {
  347. return
  348. }
  349. id := arg["id"].(string)
  350. keepFold := false
  351. if nil != arg["keepFold"] {
  352. keepFold = arg["keepFold"].(bool)
  353. }
  354. merge := false
  355. if nil != arg["merge"] {
  356. merge = arg["merge"].(bool)
  357. }
  358. image := false
  359. if nil != arg["image"] {
  360. image = arg["image"].(bool)
  361. }
  362. name, content := model.ExportHTML(id, "", true, image, keepFold, merge)
  363. // 导出 PDF 预览时点击块引转换后的脚注跳转不正确 https://github.com/siyuan-note/siyuan/issues/5894
  364. content = strings.ReplaceAll(content, "http://"+util.LocalHost+":"+util.ServerPort+"/#", "#")
  365. ret.Data = map[string]interface{}{
  366. "id": id,
  367. "name": name,
  368. "content": content,
  369. }
  370. }
  371. func exportHTML(c *gin.Context) {
  372. ret := gulu.Ret.NewResult()
  373. defer c.JSON(http.StatusOK, ret)
  374. arg, ok := util.JsonArg(c, ret)
  375. if !ok {
  376. return
  377. }
  378. id := arg["id"].(string)
  379. pdf := arg["pdf"].(bool)
  380. savePath := arg["savePath"].(string)
  381. keepFold := false
  382. if nil != arg["keepFold"] {
  383. keepFold = arg["keepFold"].(bool)
  384. }
  385. merge := false
  386. if nil != arg["merge"] {
  387. merge = arg["merge"].(bool)
  388. }
  389. name, content := model.ExportHTML(id, savePath, pdf, false, keepFold, merge)
  390. ret.Data = map[string]interface{}{
  391. "id": id,
  392. "name": name,
  393. "content": content,
  394. }
  395. }
  396. func processPDF(c *gin.Context) {
  397. ret := gulu.Ret.NewResult()
  398. defer c.JSON(http.StatusOK, ret)
  399. arg, ok := util.JsonArg(c, ret)
  400. if !ok {
  401. return
  402. }
  403. id := arg["id"].(string)
  404. path := arg["path"].(string)
  405. merge := false
  406. if nil != arg["merge"] {
  407. merge = arg["merge"].(bool)
  408. }
  409. removeAssets := arg["removeAssets"].(bool)
  410. err := model.ProcessPDF(id, path, merge, removeAssets)
  411. if nil != err {
  412. ret.Code = -1
  413. ret.Msg = err.Error()
  414. return
  415. }
  416. }
  417. func exportPreview(c *gin.Context) {
  418. ret := gulu.Ret.NewResult()
  419. defer c.JSON(http.StatusOK, ret)
  420. arg, ok := util.JsonArg(c, ret)
  421. if !ok {
  422. return
  423. }
  424. id := arg["id"].(string)
  425. stdHTML := model.Preview(id)
  426. ret.Data = map[string]interface{}{
  427. "html": stdHTML,
  428. }
  429. }
  430. func exportAsFile(c *gin.Context) {
  431. ret := gulu.Ret.NewResult()
  432. defer c.JSON(http.StatusOK, ret)
  433. form, err := c.MultipartForm()
  434. if nil != err {
  435. logging.LogErrorf("export as file failed: %s", err)
  436. ret.Code = -1
  437. ret.Msg = err.Error()
  438. return
  439. }
  440. file := form.File["file"][0]
  441. reader, err := file.Open()
  442. if nil != err {
  443. logging.LogErrorf("export as file failed: %s", err)
  444. ret.Code = -1
  445. ret.Msg = err.Error()
  446. return
  447. }
  448. defer reader.Close()
  449. data, err := io.ReadAll(reader)
  450. if nil != err {
  451. logging.LogErrorf("export as file failed: %s", err)
  452. ret.Code = -1
  453. ret.Msg = err.Error()
  454. return
  455. }
  456. name := "file-" + file.Filename
  457. name = util.FilterFileName(name)
  458. tmpDir := filepath.Join(util.TempDir, "export")
  459. if err = os.MkdirAll(tmpDir, 0755); nil != err {
  460. logging.LogErrorf("export as file failed: %s", err)
  461. ret.Code = -1
  462. ret.Msg = err.Error()
  463. return
  464. }
  465. tmp := filepath.Join(tmpDir, name)
  466. err = os.WriteFile(tmp, data, 0644)
  467. if nil != err {
  468. logging.LogErrorf("export as file failed: %s", err)
  469. ret.Code = -1
  470. ret.Msg = err.Error()
  471. return
  472. }
  473. ret.Data = map[string]interface{}{
  474. "name": name,
  475. "file": path.Join("/export/", name),
  476. }
  477. }