1
0

DocumentModel.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package models
  2. import (
  3. "time"
  4. "fmt"
  5. "strconv"
  6. "github.com/astaxie/beego"
  7. "github.com/astaxie/beego/orm"
  8. "github.com/lifei6671/mindoc/cache"
  9. "github.com/lifei6671/mindoc/conf"
  10. "os"
  11. "path/filepath"
  12. "github.com/PuerkitoBio/goquery"
  13. "bytes"
  14. "strings"
  15. "github.com/lifei6671/mindoc/utils"
  16. )
  17. // Document struct.
  18. type Document struct {
  19. DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
  20. DocumentName string `orm:"column(document_name);size(500)" json:"doc_name"`
  21. // Identify 文档唯一标识
  22. Identify string `orm:"column(identify);size(100);index;null;default(null)" json:"identify"`
  23. BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
  24. ParentId int `orm:"column(parent_id);type(int);index;default(0)" json:"parent_id"`
  25. OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
  26. // Markdown markdown格式文档.
  27. Markdown string `orm:"column(markdown);type(text);null" json:"markdown"`
  28. // Release 发布后的Html格式内容.
  29. Release string `orm:"column(release);type(text);null" json:"release"`
  30. // Content 未发布的 Html 格式内容.
  31. Content string `orm:"column(content);type(text);null" json:"content"`
  32. CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
  33. MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
  34. ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
  35. ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
  36. Version int64 `orm:"column(version);type(bigint);" json:"version"`
  37. //是否展开子目录:0 否/1 是
  38. IsOpen int `orm:"column(is_open);type(int);default(0)" json:"is_open"`
  39. AttachList []*Attachment `orm:"-" json:"attach"`
  40. }
  41. // 多字段唯一键
  42. func (item *Document) TableUnique() [][]string {
  43. return [][]string{
  44. []string{"book_id", "identify"},
  45. }
  46. }
  47. // TableName 获取对应数据库表名.
  48. func (item *Document) TableName() string {
  49. return "documents"
  50. }
  51. // TableEngine 获取数据使用的引擎.
  52. func (item *Document) TableEngine() string {
  53. return "INNODB"
  54. }
  55. func (item *Document) TableNameWithPrefix() string {
  56. return conf.GetDatabasePrefix() + item.TableName()
  57. }
  58. func NewDocument() *Document {
  59. return &Document{
  60. Version: time.Now().Unix(),
  61. }
  62. }
  63. //根据文档ID查询指定文档.
  64. func (item *Document) Find(id int) (*Document, error) {
  65. if id <= 0 {
  66. return item, ErrInvalidParameter
  67. }
  68. o := orm.NewOrm()
  69. err := o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", id).One(item)
  70. if err == orm.ErrNoRows {
  71. return item, ErrDataNotExist
  72. }
  73. return item, nil
  74. }
  75. //插入和更新文档.
  76. func (item *Document) InsertOrUpdate(cols ...string) error {
  77. o := orm.NewOrm()
  78. var err error
  79. if item.DocumentId > 0 {
  80. _, err = o.Update(item, cols...)
  81. } else {
  82. if item.Identify == "" {
  83. book := NewBook()
  84. identify := "docs"
  85. if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", item.BookId).One(book, "identify"); err == nil {
  86. identify = book.Identify
  87. }
  88. item.Identify = fmt.Sprintf("%s-%s", identify, strconv.FormatInt(time.Now().UnixNano(), 32))
  89. }
  90. if item.OrderSort == 0 {
  91. sort, _ := o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", item.BookId).Filter("parent_id", item.ParentId).Count()
  92. item.OrderSort = int(sort) + 1
  93. }
  94. _, err = o.Insert(item)
  95. NewBook().ResetDocumentNumber(item.BookId)
  96. }
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. //根据文档识别编号和项目id获取一篇文档
  103. func (item *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
  104. o := orm.NewOrm()
  105. err := o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(item)
  106. return item, err
  107. }
  108. //递归删除一个文档.
  109. func (item *Document) RecursiveDocument(docId int) error {
  110. o := orm.NewOrm()
  111. if doc, err := item.Find(docId); err == nil {
  112. o.Delete(doc)
  113. NewDocumentHistory().Clear(doc.DocumentId)
  114. }
  115. var maps []orm.Params
  116. _, err := o.Raw("SELECT document_id FROM " + item.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
  117. if err != nil {
  118. beego.Error("RecursiveDocument => ", err)
  119. return err
  120. }
  121. for _, param := range maps {
  122. if docId, ok := param["document_id"].(string); ok {
  123. id, _ := strconv.Atoi(docId)
  124. o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", id).Delete()
  125. item.RecursiveDocument(id)
  126. }
  127. }
  128. return nil
  129. }
  130. //将文档写入缓存
  131. func (item *Document) PutToCache() {
  132. go func(m Document) {
  133. if m.Identify == "" {
  134. if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600); err != nil {
  135. beego.Info("文档缓存失败:", m.DocumentId)
  136. }
  137. } else {
  138. if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600); err != nil {
  139. beego.Info("文档缓存失败:", m.DocumentId)
  140. }
  141. }
  142. }(*item)
  143. }
  144. //清除缓存
  145. func (item *Document) RemoveCache() {
  146. go func(m Document) {
  147. cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
  148. if m.Identify != "" {
  149. cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
  150. }
  151. }(*item)
  152. }
  153. //从缓存获取
  154. func (item *Document) FromCacheById(id int) (*Document, error) {
  155. if err := cache.Get("Document.Id."+strconv.Itoa(id), &item); err == nil && item.DocumentId > 0 {
  156. beego.Info("从缓存中获取文档信息成功 ->", item.DocumentId)
  157. return item, nil
  158. }
  159. if item.DocumentId > 0 {
  160. item.PutToCache()
  161. }
  162. item, err := item.Find(id)
  163. if err == nil {
  164. item.PutToCache()
  165. }
  166. return item, err
  167. }
  168. //根据文档标识从缓存中查询文档
  169. func (item *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
  170. key := fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify)
  171. if err := cache.Get(key, item); err == nil && item.DocumentId > 0 {
  172. beego.Info("从缓存中获取文档信息成功 ->", key)
  173. return item, nil
  174. }
  175. defer func() {
  176. if item.DocumentId > 0 {
  177. item.PutToCache()
  178. }
  179. }()
  180. return item.FindByIdentityFirst(identify, bookId)
  181. }
  182. //根据项目ID查询文档列表.
  183. func (item *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
  184. o := orm.NewOrm()
  185. _, err = o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
  186. return
  187. }
  188. //判断文章是否存在
  189. func (item *Document) IsExist(documentId int) bool {
  190. o := orm.NewOrm()
  191. return o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", documentId).Exist()
  192. }
  193. //发布单篇文档
  194. func (item *Document) ReleaseContent() error {
  195. item.Release = strings.TrimSpace(item.Content)
  196. err := item.Processor().InsertOrUpdate("release")
  197. if err != nil {
  198. beego.Error(fmt.Sprintf("发布失败 -> %+v", item), err)
  199. return err
  200. }
  201. //当文档发布后,需要清除已缓存的转换文档和文档缓存
  202. item.RemoveCache()
  203. if err := os.RemoveAll(filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(item.BookId))); err != nil {
  204. beego.Error("删除已缓存的文档目录失败 -> ", filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(item.BookId)))
  205. return err
  206. }
  207. return nil
  208. }
  209. //处理文档的外链,附件,底部编辑信息等.
  210. func (item *Document) Processor() *Document {
  211. if item.Release != "" {
  212. item.Release = utils.SafetyProcessor(item.Release)
  213. //安全过滤,移除危险标签和属性
  214. if docQuery, err := goquery.NewDocumentFromReader(bytes.NewBufferString(item.Release)); err == nil {
  215. //处理附件
  216. if selector := docQuery.Find("div.attach-list").First(); selector.Size() <= 0 {
  217. //处理附件
  218. attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
  219. if err == nil && len(attachList) > 0 {
  220. content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
  221. for _, attach := range attachList {
  222. if strings.HasPrefix(attach.HttpPath, "/") {
  223. attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
  224. }
  225. li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
  226. content.WriteString(li)
  227. }
  228. content.WriteString("</ul></div>")
  229. if docQuery == nil {
  230. docQuery, err = goquery.NewDocumentFromReader(content);
  231. } else {
  232. if selector := docQuery.Find("div.wiki-bottom").First(); selector.Size() > 0 {
  233. selector.BeforeHtml(content.String())
  234. } else if selector := docQuery.Find("div.markdown-article").First(); selector.Size() > 0 {
  235. selector.AppendHtml(content.String())
  236. } else if selector := docQuery.Find("article.markdown-article-inner").First(); selector.Size() > 0 {
  237. selector.AppendHtml(content.String())
  238. }
  239. }
  240. }
  241. }
  242. //处理了文档底部信息
  243. if selector := docQuery.Find("div.wiki-bottom").First(); selector.Size() <= 0 && item.MemberId > 0 {
  244. //处理文档结尾信息
  245. docCreator, err := NewMember().Find(item.MemberId, "real_name", "account")
  246. release := "<div class=\"wiki-bottom\">文档更新时间: " + item.ModifyTime.Local().Format("2006-01-02 15:04") + " &nbsp;&nbsp;作者:"
  247. if err == nil && docCreator != nil {
  248. if docCreator.RealName != "" {
  249. release += docCreator.RealName
  250. } else {
  251. release += docCreator.Account
  252. }
  253. }
  254. release += "</div>"
  255. if selector := docQuery.Find("div.markdown-article").First(); selector.Size() > 0 {
  256. selector.AppendHtml(release)
  257. } else if selector := docQuery.Find("article.markdown-article-inner").First(); selector.Size() > 0 {
  258. selector.First().AppendHtml(release)
  259. }
  260. }
  261. //设置图片为CDN地址
  262. if cdnimg := beego.AppConfig.String("cdnimg"); cdnimg != "" {
  263. docQuery.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  264. if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
  265. contentSelection.SetAttr("src", utils.JoinURI(cdnimg, src))
  266. }
  267. })
  268. }
  269. if html, err := docQuery.Html(); err == nil {
  270. item.Release = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(html), "<html><head></head><body>"), "</body></html>")
  271. }
  272. }
  273. }
  274. return item
  275. }