BookResult.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. package models
  2. import (
  3. "bytes"
  4. "github.com/juju/errors"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "encoding/json"
  12. "github.com/PuerkitoBio/goquery"
  13. "github.com/astaxie/beego"
  14. "github.com/astaxie/beego/logs"
  15. "github.com/astaxie/beego/orm"
  16. "github.com/lifei6671/mindoc/conf"
  17. "github.com/lifei6671/mindoc/converter"
  18. "github.com/lifei6671/mindoc/utils/cryptil"
  19. "github.com/lifei6671/mindoc/utils/filetil"
  20. "github.com/lifei6671/mindoc/utils/gopool"
  21. "github.com/lifei6671/mindoc/utils/requests"
  22. "github.com/lifei6671/mindoc/utils/ziptil"
  23. "gopkg.in/russross/blackfriday.v2"
  24. "net/http"
  25. "regexp"
  26. )
  27. var (
  28. exportLimitWorkerChannel = gopool.NewChannelPool(conf.GetExportLimitNum(), conf.GetExportQueueLimitNum())
  29. )
  30. type BookResult struct {
  31. BookId int `json:"book_id"`
  32. BookName string `json:"book_name"`
  33. ItemId int `json:"item_id"`
  34. ItemName string `json:"item_name"`
  35. Identify string `json:"identify"`
  36. OrderIndex int `json:"order_index"`
  37. Description string `json:"description"`
  38. Publisher string `json:"publisher"`
  39. PrivatelyOwned int `json:"privately_owned"`
  40. PrivateToken string `json:"private_token"`
  41. BookPassword string `json:"book_password"`
  42. DocCount int `json:"doc_count"`
  43. CommentStatus string `json:"comment_status"`
  44. CommentCount int `json:"comment_count"`
  45. CreateTime time.Time `json:"create_time"`
  46. CreateName string `json:"create_name"`
  47. RealName string `json:"real_name"`
  48. ModifyTime time.Time `json:"modify_time"`
  49. Cover string `json:"cover"`
  50. Theme string `json:"theme"`
  51. Label string `json:"label"`
  52. MemberId int `json:"member_id"`
  53. Editor string `json:"editor"`
  54. AutoRelease bool `json:"auto_release"`
  55. HistoryCount int `json:"history_count"`
  56. //RelationshipId int `json:"relationship_id"`
  57. //TeamRelationshipId int `json:"team_relationship_id"`
  58. RoleId conf.BookRole `json:"role_id"`
  59. RoleName string `json:"role_name"`
  60. Status int `json:"status"`
  61. IsEnableShare bool `json:"is_enable_share"`
  62. IsUseFirstDocument bool `json:"is_use_first_document"`
  63. LastModifyText string `json:"last_modify_text"`
  64. IsDisplayComment bool `json:"is_display_comment"`
  65. IsDownload bool `json:"is_download"`
  66. AutoSave bool `json:"auto_save"`
  67. }
  68. func NewBookResult() *BookResult {
  69. return &BookResult{}
  70. }
  71. func (m *BookResult) String() string {
  72. ret, err := json.Marshal(*m)
  73. if err != nil {
  74. return ""
  75. }
  76. return string(ret)
  77. }
  78. // 根据项目标识查询项目以及指定用户权限的信息.
  79. func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult, error) {
  80. if identify == "" || memberId <= 0 {
  81. return m, ErrInvalidParameter
  82. }
  83. o := orm.NewOrm()
  84. var book Book
  85. err := NewBook().QueryTable().Filter("identify", identify).One(&book)
  86. if err != nil {
  87. beego.Error("获取项目失败 ->", errors.Details(err))
  88. return m, errors.Trace(err)
  89. }
  90. roleId, err := NewBook().FindForRoleId(book.BookId, memberId)
  91. if err != nil {
  92. return m, ErrPermissionDenied
  93. }
  94. var relationship2 Relationship
  95. //查找项目创始人
  96. err = NewRelationship().QueryTable().Filter("book_id", book.BookId).Filter("role_id", 0).One(&relationship2)
  97. if err != nil {
  98. logs.Error("根据项目标识查询项目以及指定用户权限的信息 -> ", errors.Details(err))
  99. return m, ErrPermissionDenied
  100. }
  101. member, err := NewMember().Find(relationship2.MemberId)
  102. if err != nil {
  103. return m, errors.Trace(err)
  104. }
  105. m.ToBookResult(book)
  106. m.RoleId = roleId
  107. m.MemberId = memberId
  108. m.CreateName = member.Account
  109. if member.RealName != "" {
  110. m.RealName = member.RealName
  111. }
  112. if m.RoleId == conf.BookFounder {
  113. m.RoleName = "创始人"
  114. } else if m.RoleId == conf.BookAdmin {
  115. m.RoleName = "管理员"
  116. } else if m.RoleId == conf.BookEditor {
  117. m.RoleName = "编辑者"
  118. } else if m.RoleId == conf.BookObserver {
  119. m.RoleName = "观察者"
  120. }
  121. doc := NewDocument()
  122. err = o.QueryTable(doc.TableNameWithPrefix()).Filter("book_id", book.BookId).OrderBy("modify_time").One(doc)
  123. if err == nil {
  124. member2 := NewMember()
  125. member2.Find(doc.ModifyAt)
  126. m.LastModifyText = member2.Account + " 于 " + doc.ModifyTime.Local().Format("2006-01-02 15:04:05")
  127. }
  128. return m, nil
  129. }
  130. func (m *BookResult) FindToPager(pageIndex, pageSize int) (books []*BookResult, totalCount int, err error) {
  131. o := orm.NewOrm()
  132. count, err := o.QueryTable(NewBook().TableNameWithPrefix()).Count()
  133. if err != nil {
  134. return
  135. }
  136. totalCount = int(count)
  137. sql := `SELECT
  138. book.*,rel.relationship_id,rel.role_id,m.account AS create_name,m.real_name
  139. FROM md_books AS book
  140. LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
  141. LEFT JOIN md_members AS m ON rel.member_id = m.member_id
  142. ORDER BY book.order_index DESC ,book.book_id DESC LIMIT ?,?`
  143. offset := (pageIndex - 1) * pageSize
  144. _, err = o.Raw(sql, offset, pageSize).QueryRows(&books)
  145. return
  146. }
  147. //实体转换
  148. func (m *BookResult) ToBookResult(book Book) *BookResult {
  149. m.BookId = book.BookId
  150. m.BookName = book.BookName
  151. m.Identify = book.Identify
  152. m.OrderIndex = book.OrderIndex
  153. m.Description = strings.Replace(book.Description, "\r\n", "<br/>", -1)
  154. m.PrivatelyOwned = book.PrivatelyOwned
  155. m.PrivateToken = book.PrivateToken
  156. m.BookPassword = book.BookPassword
  157. m.DocCount = book.DocCount
  158. m.CommentStatus = book.CommentStatus
  159. m.CommentCount = book.CommentCount
  160. m.CreateTime = book.CreateTime
  161. m.ModifyTime = book.ModifyTime
  162. m.Cover = book.Cover
  163. m.Label = book.Label
  164. m.Status = book.Status
  165. m.Editor = book.Editor
  166. m.Theme = book.Theme
  167. m.AutoRelease = book.AutoRelease == 1
  168. m.IsEnableShare = book.IsEnableShare == 0
  169. m.IsUseFirstDocument = book.IsUseFirstDocument == 1
  170. m.Publisher = book.Publisher
  171. m.HistoryCount = book.HistoryCount
  172. m.IsDownload = book.IsDownload == 0
  173. m.AutoSave = book.AutoSave == 1
  174. m.ItemId = book.ItemId
  175. if book.Theme == "" {
  176. m.Theme = "default"
  177. }
  178. if book.Editor == "" {
  179. m.Editor = "markdown"
  180. }
  181. doc := NewDocument()
  182. o := orm.NewOrm()
  183. err := o.QueryTable(doc.TableNameWithPrefix()).Filter("book_id", book.BookId).OrderBy("modify_time").One(doc)
  184. if err == nil {
  185. member2 := NewMember()
  186. member2.Find(doc.ModifyAt)
  187. m.LastModifyText = member2.Account + " 于 " + doc.ModifyTime.Local().Format("2006-01-02 15:04:05")
  188. }
  189. if m.ItemId > 0 {
  190. if item, err := NewItemsets().First(m.ItemId); err == nil {
  191. m.ItemName = item.ItemName
  192. }
  193. }
  194. return m
  195. }
  196. //后台转换
  197. func BackgroundConvert(sessionId string, bookResult *BookResult) error {
  198. if err := converter.CheckConvertCommand(); err != nil {
  199. beego.Error("检查转换程序失败 -> ", errors.Details(err))
  200. return errors.Trace(err)
  201. }
  202. err := exportLimitWorkerChannel.LoadOrStore(bookResult.Identify, func() {
  203. if _, err := bookResult.Converter(sessionId); err != nil {
  204. beego.Error("转换文档失败 -> ", errors.Details(err))
  205. }
  206. })
  207. if err != nil {
  208. beego.Error("将导出任务加入任务队列失败 -> ", errors.Details(err))
  209. return errors.Trace(err)
  210. }
  211. exportLimitWorkerChannel.Start()
  212. return nil
  213. }
  214. //导出PDF、word等格式
  215. func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
  216. convertBookResult := ConvertBookResult{}
  217. outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(m.BookId))
  218. viewPath := beego.BConfig.WebConfig.ViewsPath
  219. pdfpath := filepath.Join(outputPath, "book.pdf")
  220. epubpath := filepath.Join(outputPath, "book.epub")
  221. mobipath := filepath.Join(outputPath, "book.mobi")
  222. docxpath := filepath.Join(outputPath, "book.docx")
  223. //先将转换的文件储存到临时目录
  224. tempOutputPath := filepath.Join(os.TempDir(), sessionId, m.Identify, "source") //filepath.Abs(filepath.Join("cache", sessionId))
  225. sourceDir := strings.TrimSuffix(tempOutputPath, "source")
  226. if filetil.FileExists(sourceDir) {
  227. if err := os.RemoveAll(sourceDir); err != nil {
  228. beego.Error("删除临时目录失败 ->", sourceDir, errors.Details(err))
  229. }
  230. }
  231. if err := filetil.MkdirAll(outputPath, 0755); err != nil {
  232. beego.Error("创建目录失败 -> ", outputPath, errors.Details(err))
  233. }
  234. if err := filetil.MkdirAll(tempOutputPath, 0755); err != nil {
  235. beego.Error("创建目录失败 -> ", tempOutputPath, errors.Details(err))
  236. }
  237. if err := filetil.MkdirAll(filepath.Join(tempOutputPath, "Images"), 0755); err != nil {
  238. return convertBookResult, errors.Trace(err)
  239. }
  240. //defer os.RemoveAll(strings.TrimSuffix(tempOutputPath,"source"))
  241. if filetil.FileExists(pdfpath) && filetil.FileExists(epubpath) && filetil.FileExists(mobipath) && filetil.FileExists(docxpath) {
  242. convertBookResult.EpubPath = epubpath
  243. convertBookResult.MobiPath = mobipath
  244. convertBookResult.PDFPath = pdfpath
  245. convertBookResult.WordPath = docxpath
  246. return convertBookResult, nil
  247. }
  248. docs, err := NewDocument().FindListByBookId(m.BookId)
  249. if err != nil {
  250. return convertBookResult, errors.Trace(err)
  251. }
  252. tocList := make([]converter.Toc, 0)
  253. for _, item := range docs {
  254. if item.ParentId == 0 {
  255. toc := converter.Toc{
  256. Id: item.DocumentId,
  257. Link: strconv.Itoa(item.DocumentId) + ".html",
  258. Pid: item.ParentId,
  259. Title: item.DocumentName,
  260. }
  261. tocList = append(tocList, toc)
  262. }
  263. }
  264. for _, item := range docs {
  265. if item.ParentId != 0 {
  266. toc := converter.Toc{
  267. Id: item.DocumentId,
  268. Link: strconv.Itoa(item.DocumentId) + ".html",
  269. Pid: item.ParentId,
  270. Title: item.DocumentName,
  271. }
  272. tocList = append(tocList, toc)
  273. }
  274. }
  275. ebookConfig := converter.Config{
  276. Charset: "utf-8",
  277. Cover: m.Cover,
  278. Timestamp: time.Now().Format("2006-01-02 15:04:05"),
  279. Description: string(blackfriday.Run([]byte(m.Description))),
  280. Footer: "<p style='color:#8E8E8E;font-size:12px;'>本文档使用 <a href='https://www.iminho.me' style='text-decoration:none;color:#1abc9c;font-weight:bold;'>MinDoc</a> 构建 <span style='float:right'>- _PAGENUM_ -</span></p>",
  281. Header: "<p style='color:#8E8E8E;font-size:12px;'>_SECTION_</p>",
  282. Identifier: "",
  283. Language: "zh-CN",
  284. Creator: m.CreateName,
  285. Publisher: m.Publisher,
  286. Contributor: m.Publisher,
  287. Title: m.BookName,
  288. Format: []string{"epub", "mobi", "pdf", "docx"},
  289. FontSize: "14",
  290. PaperSize: "a4",
  291. MarginLeft: "72",
  292. MarginRight: "72",
  293. MarginTop: "72",
  294. MarginBottom: "72",
  295. Toc: tocList,
  296. More: []string{},
  297. }
  298. if m.Publisher != "" {
  299. ebookConfig.Footer = "<p style='color:#8E8E8E;font-size:12px;'>本文档由 <span style='text-decoration:none;color:#1abc9c;font-weight:bold;'>" + m.Publisher + "</span> 生成<span style='float:right'>- _PAGENUM_ -</span></p>"
  300. }
  301. if m.RealName != "" {
  302. ebookConfig.Creator = m.RealName
  303. }
  304. if tempOutputPath, err = filepath.Abs(tempOutputPath); err != nil {
  305. beego.Error("导出目录配置错误:" + err.Error())
  306. return convertBookResult, errors.Trace(err)
  307. }
  308. for _, item := range docs {
  309. name := strconv.Itoa(item.DocumentId)
  310. fpath := filepath.Join(tempOutputPath, name+".html")
  311. f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0755)
  312. if err != nil {
  313. return convertBookResult, errors.Trace(err)
  314. }
  315. var buf bytes.Buffer
  316. if err := beego.ExecuteViewPathTemplate(&buf, "document/export.tpl", viewPath, map[string]interface{}{"Model": m, "Lists": item, "BaseUrl": conf.BaseUrl}); err != nil {
  317. return convertBookResult, errors.Trace(err)
  318. }
  319. html := buf.String()
  320. bufio := bytes.NewReader(buf.Bytes())
  321. doc, err := goquery.NewDocumentFromReader(bufio)
  322. if err != nil {
  323. return convertBookResult, errors.Trace(err)
  324. }
  325. doc.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  326. if src, ok := contentSelection.Attr("src"); ok {
  327. //var encodeString string
  328. dstSrcString := "Images/" + filepath.Base(src)
  329. //如果是本地路径则直接读取文件内容
  330. if strings.HasPrefix(src, "/") {
  331. spath := filepath.Join(conf.WorkingDirectory, src)
  332. if filetil.CopyFile(spath, filepath.Join(tempOutputPath, dstSrcString)); err != nil {
  333. beego.Error("复制图片失败 -> ", err, src)
  334. return
  335. }
  336. } else {
  337. client := &http.Client{}
  338. if req, err := http.NewRequest("GET", src, nil); err == nil {
  339. req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
  340. req.Header.Set("Referer", src)
  341. //10秒连接超时时间
  342. client.Timeout = time.Second * 100
  343. if resp, err := client.Do(req); err == nil {
  344. defer resp.Body.Close()
  345. if body, err := ioutil.ReadAll(resp.Body); err == nil {
  346. //encodeString = base64.StdEncoding.EncodeToString(body)
  347. if err := ioutil.WriteFile(filepath.Join(tempOutputPath, dstSrcString), body, 0755); err != nil {
  348. beego.Error("下载图片失败 -> ", err, src)
  349. return
  350. }
  351. } else {
  352. beego.Error("下载图片失败 -> ", err, src)
  353. return
  354. }
  355. } else {
  356. beego.Error("下载图片失败 -> ", err, src)
  357. return
  358. }
  359. }
  360. }
  361. contentSelection.SetAttr("src", dstSrcString)
  362. }
  363. })
  364. //移除文档底部的更新信息
  365. if selection := doc.Find("div.wiki-bottom").First(); selection.Size() > 0 {
  366. selection.Remove()
  367. }
  368. html, err = doc.Html()
  369. if err != nil {
  370. _ = f.Close()
  371. return convertBookResult, errors.Trace(err)
  372. }
  373. if _, err := f.WriteString(html); err != nil {
  374. return convertBookResult, errors.Trace(err)
  375. }
  376. _ = f.Close()
  377. }
  378. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "css", "kancloud.css"), filepath.Join(tempOutputPath, "styles", "css", "kancloud.css")); err != nil {
  379. beego.Error("复制CSS样式出错 -> static/css/kancloud.css", err)
  380. }
  381. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "css", "export.css"), filepath.Join(tempOutputPath, "styles", "css", "export.css")); err != nil {
  382. beego.Error("复制CSS样式出错 -> static/css/export.css", err)
  383. }
  384. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "editor.md", "css", "editormd.preview.css"), filepath.Join(tempOutputPath, "styles", "editor.md", "css", "editormd.preview.css")); err != nil {
  385. beego.Error("复制CSS样式出错 -> static/editor.md/css/editormd.preview.css", err)
  386. }
  387. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "css", "markdown.preview.css"), filepath.Join(tempOutputPath, "styles", "css", "markdown.preview.css")); err != nil {
  388. beego.Error("复制CSS样式出错 -> static/css/markdown.preview.css", err)
  389. }
  390. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "editor.md", "lib", "highlight", "styles", "github.css"), filepath.Join(tempOutputPath, "styles", "css", "github.css")); err != nil {
  391. beego.Error("复制CSS样式出错 -> static/editor.md/lib/highlight/styles/github.css", err)
  392. }
  393. if err := filetil.CopyDir(filepath.Join(conf.WorkingDirectory, "static", "font-awesome"), filepath.Join(tempOutputPath, "styles", "font-awesome")); err != nil {
  394. beego.Error("复制CSS样式出错 -> static/font-awesome", err)
  395. }
  396. if err := filetil.CopyFile(filepath.Join(conf.WorkingDirectory, "static", "editor.md", "lib", "mermaid", "mermaid.css"), filepath.Join(tempOutputPath, "styles", "css", "mermaid.css")); err != nil {
  397. beego.Error("复制CSS样式出错 -> static/editor.md/lib/mermaid/mermaid.css", err)
  398. }
  399. eBookConverter := &converter.Converter{
  400. BasePath: tempOutputPath,
  401. OutputPath: filepath.Join(strings.TrimSuffix(tempOutputPath, "source"), "output"),
  402. Config: ebookConfig,
  403. Debug: true,
  404. ProcessNum: conf.GetExportProcessNum(),
  405. }
  406. if err := filetil.MkdirAll(eBookConverter.OutputPath, 0755); err != nil {
  407. return convertBookResult, errors.Trace(err)
  408. }
  409. if err := eBookConverter.Convert(); err != nil {
  410. beego.Error("转换文件错误:" + m.BookName + " -> " + err.Error())
  411. return convertBookResult, err
  412. }
  413. beego.Info("文档转换完成:" + m.BookName)
  414. if err := filetil.CopyFile(filepath.Join(eBookConverter.OutputPath, "output", "book.mobi"), mobipath, ); err != nil {
  415. beego.Error("复制文档失败 -> ", filepath.Join(eBookConverter.OutputPath, "output", "book.mobi"), err)
  416. }
  417. if err := filetil.CopyFile(filepath.Join(eBookConverter.OutputPath, "output", "book.pdf"), pdfpath); err != nil {
  418. beego.Error("复制文档失败 -> ", filepath.Join(eBookConverter.OutputPath, "output", "book.pdf"), err)
  419. }
  420. if err := filetil.CopyFile(filepath.Join(eBookConverter.OutputPath, "output", "book.epub"), epubpath); err != nil {
  421. beego.Error("复制文档失败 -> ", filepath.Join(eBookConverter.OutputPath, "output", "book.epub"), err)
  422. }
  423. if err := filetil.CopyFile(filepath.Join(eBookConverter.OutputPath, "output", "book.docx"), docxpath); err != nil {
  424. beego.Error("复制文档失败 -> ", filepath.Join(eBookConverter.OutputPath, "output", "book.docx"), err)
  425. }
  426. convertBookResult.MobiPath = mobipath
  427. convertBookResult.PDFPath = pdfpath
  428. convertBookResult.EpubPath = epubpath
  429. convertBookResult.WordPath = docxpath
  430. return convertBookResult, nil
  431. }
  432. //导出Markdown原始文件
  433. func (m *BookResult) ExportMarkdown(sessionId string) (string, error) {
  434. outputPath := filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(m.BookId), "book.zip")
  435. if err := filetil.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
  436. return "", errors.Trace(err)
  437. }
  438. tempOutputPath := filepath.Join(os.TempDir(), sessionId, "markdown")
  439. if err := filetil.MkdirAll(tempOutputPath, 0755); err != nil {
  440. return "", errors.Trace(err)
  441. }
  442. defer os.RemoveAll(tempOutputPath)
  443. bookUrl := conf.URLFor("DocumentController.Index", ":key", m.Identify) + "/"
  444. err := exportMarkdown(tempOutputPath, 0, m.BookId, tempOutputPath, bookUrl)
  445. if err != nil {
  446. return "", err
  447. }
  448. if err := ziptil.Compress(outputPath, tempOutputPath); err != nil {
  449. beego.Error("导出Markdown失败->", err)
  450. return "", err
  451. }
  452. return outputPath, nil
  453. }
  454. //递归导出Markdown文档
  455. func exportMarkdown(p string, parentId int, bookId int, baseDir string, bookUrl string) error {
  456. o := orm.NewOrm()
  457. var docs []*Document
  458. _, err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id", bookId).Filter("parent_id", parentId).All(&docs)
  459. if err != nil {
  460. beego.Error("导出Markdown失败->", err)
  461. return err
  462. }
  463. for _, doc := range docs {
  464. //获取当前文档的子文档数量,如果数量不为0,则将当前文档命名为READMD.md并设置成目录。
  465. subDocCount, err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("parent_id", doc.DocumentId).Count()
  466. if err != nil {
  467. beego.Error("导出Markdown失败->", err)
  468. return err
  469. }
  470. var docPath string
  471. if subDocCount > 0 {
  472. if doc.Identify != "" {
  473. docPath = filepath.Join(p, doc.Identify, "README.md")
  474. } else {
  475. docPath = filepath.Join(p, strconv.Itoa(doc.DocumentId), "README.md")
  476. }
  477. } else {
  478. if doc.Identify != "" {
  479. if strings.HasSuffix(doc.Identify, ".md") || strings.HasSuffix(doc.Identify, ".markdown") {
  480. docPath = filepath.Join(p, doc.Identify)
  481. } else {
  482. docPath = filepath.Join(p, doc.Identify+".md")
  483. }
  484. } else {
  485. docPath = filepath.Join(p, strings.TrimSpace(doc.DocumentName)+".md")
  486. }
  487. }
  488. dirPath := filepath.Dir(docPath)
  489. os.MkdirAll(dirPath, 0766)
  490. markdown := doc.Markdown
  491. //如果当前文档不为空
  492. if strings.TrimSpace(doc.Markdown) != "" {
  493. re := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)
  494. //处理文档中图片
  495. markdown = re.ReplaceAllStringFunc(doc.Markdown, func(image string) string {
  496. images := re.FindAllSubmatch([]byte(image), -1)
  497. if len(images) <= 0 || len(images[0]) < 3 {
  498. return image
  499. }
  500. originalImageUrl := string(images[0][2])
  501. imageUrl := strings.Replace(string(originalImageUrl), "\\", "/", -1)
  502. //如果是本地路径,则需要将图片复制到项目目录
  503. if strings.HasPrefix(imageUrl, "http://") || strings.HasPrefix(imageUrl, "https://") {
  504. imageExt := cryptil.Md5Crypt(imageUrl) + filepath.Ext(imageUrl)
  505. dstFile := filepath.Join(baseDir, "uploads", time.Now().Format("200601"), imageExt)
  506. if err := requests.DownloadAndSaveFile(imageUrl, dstFile); err == nil {
  507. imageUrl = strings.TrimPrefix(strings.Replace(dstFile, "\\", "/", -1), strings.Replace(baseDir, "\\", "/", -1))
  508. if !strings.HasPrefix(imageUrl, "/") && !strings.HasPrefix(imageUrl, "\\") {
  509. imageUrl = "/" + imageUrl
  510. }
  511. }
  512. } else if strings.HasPrefix(imageUrl, "/") {
  513. filetil.CopyFile(filepath.Join(conf.WorkingDirectory, imageUrl), filepath.Join(baseDir, imageUrl))
  514. }
  515. imageUrl = strings.Replace(strings.TrimSuffix(image, originalImageUrl+")")+imageUrl+")", "\\", "/", -1)
  516. return imageUrl
  517. })
  518. linkRe := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
  519. markdown = linkRe.ReplaceAllStringFunc(markdown, func(link string) string {
  520. links := linkRe.FindAllStringSubmatch(link, -1)
  521. if len(links) > 0 && len(links[0]) >= 3 {
  522. originalLink := links[0][2]
  523. //如果当前链接位于当前项目内
  524. if strings.HasPrefix(originalLink, bookUrl) {
  525. docIdentify := strings.TrimSpace(strings.TrimPrefix(originalLink, bookUrl))
  526. tempDoc := NewDocument()
  527. if id, err := strconv.Atoi(docIdentify); err == nil && id > 0 {
  528. err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("document_id", id).One(tempDoc, "identify", "parent_id", "document_id")
  529. if err != nil {
  530. beego.Error(err)
  531. return link
  532. }
  533. } else {
  534. err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("identify", docIdentify).One(tempDoc, "identify", "parent_id", "document_id")
  535. if err != nil {
  536. beego.Error(err)
  537. return link
  538. }
  539. }
  540. tempLink := recursiveJoinDocumentIdentify(tempDoc.ParentId, "") + strings.TrimPrefix(originalLink, bookUrl)
  541. if !strings.HasSuffix(tempLink, ".md") && !strings.HasSuffix(doc.Identify, ".markdown") {
  542. tempLink = tempLink + ".md"
  543. }
  544. relative := strings.TrimPrefix(strings.Replace(p, "\\", "/", -1), strings.Replace(baseDir, "\\", "/", -1))
  545. repeat := 0
  546. if relative != "" {
  547. relative = strings.TrimSuffix(strings.TrimPrefix(relative, "/"), "/")
  548. repeat = strings.Count(relative, "/") + 1
  549. }
  550. beego.Info(repeat, "|", relative, "|", p, "|", baseDir)
  551. tempLink = strings.Repeat("../", repeat) + tempLink
  552. link = strings.TrimSuffix(link, originalLink+")") + tempLink + ")"
  553. }
  554. }
  555. return link
  556. })
  557. } else {
  558. markdown = "# " + doc.DocumentName + "\n"
  559. }
  560. if err := ioutil.WriteFile(docPath, []byte(markdown), 0644); err != nil {
  561. beego.Error("导出Markdown失败->", err)
  562. return err
  563. }
  564. if subDocCount > 0 {
  565. if err = exportMarkdown(dirPath, doc.DocumentId, bookId, baseDir, bookUrl); err != nil {
  566. return err
  567. }
  568. }
  569. }
  570. return nil
  571. }
  572. func recursiveJoinDocumentIdentify(parentDocId int, identify string) string {
  573. o := orm.NewOrm()
  574. doc := NewDocument()
  575. err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("document_id", parentDocId).One(doc, "identify", "parent_id", "document_id")
  576. if err != nil {
  577. beego.Error(err)
  578. return identify
  579. }
  580. if doc.Identify == "" {
  581. identify = strconv.Itoa(doc.DocumentId) + "/" + identify
  582. } else {
  583. identify = doc.Identify + "/" + identify
  584. }
  585. if doc.ParentId > 0 {
  586. identify = recursiveJoinDocumentIdentify(doc.ParentId, identify)
  587. }
  588. return identify
  589. }
  590. //查询项目的第一篇文档
  591. func (m *BookResult) FindFirstDocumentByBookId(bookId int) (*Document, error) {
  592. o := orm.NewOrm()
  593. doc := NewDocument()
  594. err := o.QueryTable(doc.TableNameWithPrefix()).Filter("book_id", bookId).Filter("parent_id", 0).OrderBy("order_sort").One(doc)
  595. return doc, err
  596. }