document.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package models
  2. import (
  3. "time"
  4. "bytes"
  5. "fmt"
  6. "github.com/astaxie/beego"
  7. "github.com/astaxie/beego/orm"
  8. "github.com/lifei6671/mindoc/conf"
  9. "strings"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "github.com/PuerkitoBio/goquery"
  14. "github.com/lifei6671/mindoc/cache"
  15. "encoding/json"
  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:"type(bigint);column(version)" json:"version"`
  37. AttachList []*Attachment `orm:"-" json:"attach"`
  38. }
  39. // TableName 获取对应数据库表名.
  40. func (m *Document) TableName() string {
  41. return "documents"
  42. }
  43. // TableEngine 获取数据使用的引擎.
  44. func (m *Document) TableEngine() string {
  45. return "INNODB"
  46. }
  47. func (m *Document) TableNameWithPrefix() string {
  48. return conf.GetDatabasePrefix() + m.TableName()
  49. }
  50. func NewDocument() *Document {
  51. return &Document{
  52. Version: time.Now().Unix(),
  53. }
  54. }
  55. //根据文档ID查询指定文档.
  56. func (m *Document) Find(id int) (*Document, error) {
  57. if id <= 0 {
  58. return m, ErrInvalidParameter
  59. }
  60. o := orm.NewOrm()
  61. err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
  62. if err == orm.ErrNoRows {
  63. return m, ErrDataNotExist
  64. }
  65. return m, nil
  66. }
  67. //插入和更新文档.
  68. func (m *Document) InsertOrUpdate(cols ...string) error {
  69. o := orm.NewOrm()
  70. var err error
  71. if m.DocumentId > 0 {
  72. _, err = o.Update(m)
  73. } else {
  74. _, err = o.Insert(m)
  75. NewBook().ResetDocumentNumber(m.BookId)
  76. }
  77. if err != nil {
  78. return err
  79. }
  80. return nil
  81. }
  82. //根据指定字段查询一条文档.
  83. func (m *Document) FindByFieldFirst(field string, v interface{}) (*Document, error) {
  84. o := orm.NewOrm()
  85. err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, v).One(m)
  86. return m, err
  87. }
  88. //递归删除一个文档.
  89. func (m *Document) RecursiveDocument(docId int) error {
  90. o := orm.NewOrm()
  91. if doc, err := m.Find(docId); err == nil {
  92. o.Delete(doc)
  93. NewDocumentHistory().Clear(doc.DocumentId)
  94. }
  95. //
  96. //var docs []*Document
  97. //
  98. //_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id", doc_id).All(&docs)
  99. var maps []orm.Params
  100. _, err := o.Raw("SELECT document_id FROM " + m.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
  101. if err != nil {
  102. beego.Error("RecursiveDocument => ", err)
  103. return err
  104. }
  105. for _, item := range maps {
  106. if docId,ok := item["document_id"].(string); ok{
  107. id,_ := strconv.Atoi(docId)
  108. o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).Delete()
  109. m.RecursiveDocument(id)
  110. }
  111. }
  112. return nil
  113. }
  114. //发布文档
  115. func (m *Document) ReleaseContent(bookId int) {
  116. o := orm.NewOrm()
  117. var docs []*Document
  118. _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).All(&docs, "document_id","identify", "content")
  119. if err != nil {
  120. beego.Error("发布失败 => ", err)
  121. return
  122. }
  123. for _, item := range docs {
  124. if item.Content != "" {
  125. item.Release = item.Content
  126. bufio := bytes.NewReader([]byte(item.Content))
  127. //解析文档中非本站的链接,并设置为新窗口打开
  128. if content, err := goquery.NewDocumentFromReader(bufio);err == nil {
  129. content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
  130. if src, ok := contentSelection.Attr("href"); ok{
  131. if strings.HasPrefix(src, "http://") || strings.HasPrefix(src,"https://") {
  132. //beego.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
  133. if conf.BaseUrl != "" && !strings.HasPrefix(src,conf.BaseUrl) {
  134. contentSelection.SetAttr("target", "_blank")
  135. if html, err := content.Html();err == nil {
  136. item.Release = html
  137. }
  138. }
  139. }
  140. }
  141. })
  142. }
  143. }
  144. attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
  145. if err == nil && len(attachList) > 0 {
  146. content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
  147. for _, attach := range attachList {
  148. if strings.HasPrefix(attach.HttpPath, "/") {
  149. attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
  150. }
  151. li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
  152. content.WriteString(li)
  153. }
  154. content.WriteString("</ul></div>")
  155. item.Release += content.String()
  156. }
  157. _, err = o.Update(item, "release")
  158. if err != nil {
  159. beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
  160. }else {
  161. //当文档发布后,需要清除已缓存的转换文档和文档缓存
  162. if doc,err := NewDocument().Find(item.DocumentId); err == nil {
  163. doc.PutToCache()
  164. }else{
  165. doc.RemoveCache()
  166. }
  167. os.RemoveAll(filepath.Join(conf.WorkingDirectory,"uploads","books",strconv.Itoa(bookId)))
  168. }
  169. }
  170. }
  171. //将文档写入缓存
  172. func (m *Document) PutToCache(){
  173. go func(m Document) {
  174. if v,err := json.Marshal(&m);err == nil {
  175. if m.Identify == "" {
  176. if err := cache.Put("Document.Id." + strconv.Itoa(m.DocumentId), v, time.Second*3600); err != nil {
  177. beego.Info("文档缓存失败:", m.DocumentId)
  178. }
  179. }else{
  180. if err := cache.Put("Document.Identify."+ m.Identify, v, time.Second*3600); err != nil {
  181. beego.Info("文档缓存失败:", m.DocumentId)
  182. }
  183. }
  184. }
  185. }(*m)
  186. }
  187. //清除缓存
  188. func (m *Document) RemoveCache() {
  189. go func(m Document) {
  190. cache.Put("Document.Id." + strconv.Itoa(m.DocumentId), m, time.Second*3600);
  191. if m.Identify != "" {
  192. cache.Put("Document.Identify."+ m.Identify, m, time.Second*3600);
  193. }
  194. }(*m)
  195. }
  196. //从缓存获取
  197. func (m *Document) FromCacheById(id int) (*Document,error) {
  198. b := cache.Get("Document.Id." + strconv.Itoa(id))
  199. if v,ok := b.([]byte); ok {
  200. if err := json.Unmarshal(v,m);err == nil{
  201. beego.Info("从缓存中获取文档信息成功",m.DocumentId)
  202. return m,nil
  203. }
  204. }
  205. defer func() {
  206. if m.DocumentId > 0 {
  207. m.PutToCache()
  208. }
  209. }()
  210. return m.Find(id)
  211. }
  212. //根据文档标识从缓存中查询文档
  213. func (m *Document) FromCacheByIdentify(identify string) (*Document,error) {
  214. b := cache.Get("Document.Identify." + identify)
  215. if v,ok := b.([]byte); ok {
  216. if err := json.Unmarshal(v,m);err == nil{
  217. beego.Info("从缓存中获取文档信息成功",m.DocumentId,identify)
  218. return m,nil
  219. }
  220. }
  221. defer func() {
  222. if m.DocumentId > 0 {
  223. m.PutToCache()
  224. }
  225. }()
  226. return m.FindByFieldFirst("identify",identify)
  227. }
  228. //根据项目ID查询文档列表.
  229. func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
  230. o := orm.NewOrm()
  231. _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
  232. return
  233. }