mindoc/models/AttachmentResult.go

72 lines
1.7 KiB
Go
Raw Normal View History

2017-06-09 18:14:55 +08:00
package models
import (
"strings"
2018-03-24 17:24:02 +08:00
"github.com/beego/beego/v2/client/orm"
"github.com/mindoc-org/mindoc/utils/filetil"
2017-06-09 18:14:55 +08:00
)
type AttachmentResult struct {
Attachment
IsExist bool
BookName string
DocumentName string
2017-06-09 18:14:55 +08:00
FileShortSize string
Account string
2017-06-09 18:14:55 +08:00
LocalHttpPath string
}
func NewAttachmentResult() *AttachmentResult {
return &AttachmentResult{IsExist: false}
2017-06-09 18:14:55 +08:00
}
func (m *AttachmentResult) Find(id int) (*AttachmentResult, error) {
2017-06-09 18:14:55 +08:00
o := orm.NewOrm()
attach := NewAttachment()
err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(attach)
2017-06-09 18:14:55 +08:00
if err != nil {
return m, err
2017-06-09 18:14:55 +08:00
}
m.Attachment = *attach
if attach.BookId == 0 && attach.DocumentId > 0 {
blog := NewBlog()
if err := o.QueryTable(blog.TableNameWithPrefix()).Filter("blog_id", attach.DocumentId).One(blog, "blog_title"); err == nil {
m.BookName = blog.BlogTitle
} else {
m.BookName = "[文章不存在]"
}
2017-06-09 18:14:55 +08:00
} else {
book := NewBook()
2017-06-09 18:14:55 +08:00
if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", attach.BookId).One(book, "book_name"); e == nil {
m.BookName = book.BookName
} else {
m.BookName = "[不存在]"
}
doc := NewDocument()
2017-06-09 18:14:55 +08:00
if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", attach.DocumentId).One(doc, "document_name"); e == nil {
m.DocumentName = doc.DocumentName
} else {
m.DocumentName = "[不存在]"
}
}
2017-06-09 18:14:55 +08:00
if attach.CreateAt > 0 {
member := NewMember()
if e := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", attach.CreateAt).One(member, "account"); e == nil {
2017-06-09 18:14:55 +08:00
m.Account = member.Account
}
}
2018-03-24 17:24:02 +08:00
m.FileShortSize = filetil.FormatBytes(int64(attach.FileSize))
m.LocalHttpPath = strings.Replace(m.FilePath, "\\", "/", -1)
2017-06-09 18:14:55 +08:00
return m, nil
}