BookResult.go 22 KB

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