AttachmentResult.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package models
  2. import (
  3. "strings"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/mindoc-org/mindoc/utils/filetil"
  6. )
  7. type AttachmentResult struct {
  8. Attachment
  9. IsExist bool
  10. BookName string
  11. DocumentName string
  12. FileShortSize string
  13. Account string
  14. LocalHttpPath string
  15. }
  16. func NewAttachmentResult() *AttachmentResult {
  17. return &AttachmentResult{IsExist: false}
  18. }
  19. func (m *AttachmentResult) Find(id int) (*AttachmentResult, error) {
  20. o := orm.NewOrm()
  21. attach := NewAttachment()
  22. err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(attach)
  23. if err != nil {
  24. return m, err
  25. }
  26. m.Attachment = *attach
  27. if attach.BookId == 0 && attach.DocumentId > 0 {
  28. blog := NewBlog()
  29. if err := o.QueryTable(blog.TableNameWithPrefix()).Filter("blog_id", attach.DocumentId).One(blog, "blog_title"); err == nil {
  30. m.BookName = blog.BlogTitle
  31. } else {
  32. m.BookName = "[文章不存在]"
  33. }
  34. } else {
  35. book := NewBook()
  36. if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", attach.BookId).One(book, "book_name"); e == nil {
  37. m.BookName = book.BookName
  38. } else {
  39. m.BookName = "[不存在]"
  40. }
  41. doc := NewDocument()
  42. if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", attach.DocumentId).One(doc, "document_name"); e == nil {
  43. m.DocumentName = doc.DocumentName
  44. } else {
  45. m.DocumentName = "[不存在]"
  46. }
  47. }
  48. if attach.CreateAt > 0 {
  49. member := NewMember()
  50. if e := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", attach.CreateAt).One(member, "account"); e == nil {
  51. m.Account = member.Account
  52. }
  53. }
  54. m.FileShortSize = filetil.FormatBytes(int64(attach.FileSize))
  55. m.LocalHttpPath = strings.Replace(m.FilePath, "\\", "/", -1)
  56. return m, nil
  57. }