forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment.go
125 lines (103 loc) · 3.46 KB
/
attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//数据库模型.
package models
import (
"os"
"time"
"strings"
"github.com/TruthHun/BookStack/conf"
"github.com/TruthHun/BookStack/utils"
"github.com/astaxie/beego/orm"
)
// Attachment struct .
type Attachment struct {
AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"`
FileName string `orm:"column(file_name);size(255)" json:"file_name"`
FilePath string `orm:"column(file_path);size(2000)" json:"file_path"`
FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"`
HttpPath string `orm:"column(http_path);size(2000)" json:"http_path"`
FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"`
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
}
// TableName 获取对应数据库表名.
func (m *Attachment) TableName() string {
return "attachment"
}
// TableEngine 获取数据使用的引擎.
func (m *Attachment) TableEngine() string {
return "INNODB"
}
func (m *Attachment) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
func NewAttachment() *Attachment {
return &Attachment{}
}
func (m *Attachment) Insert() error {
o := orm.NewOrm()
_, err := o.Insert(m)
return err
}
func (m *Attachment) Update() error {
o := orm.NewOrm()
_, err := o.Update(m)
return err
}
func (m *Attachment) Delete() (err error) {
o := orm.NewOrm()
if _, err = o.Delete(m); err != nil {
return err
}
return os.Remove(m.FilePath)
}
func (m *Attachment) Find(id int) (*Attachment, error) {
if id <= 0 {
return m, ErrInvalidParameter
}
o := orm.NewOrm()
err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(m)
return m, err
}
func (m *Attachment) FindListByDocumentId(docId int) (attaches []*Attachment, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).OrderBy("-attachment_id").All(&attaches)
return
}
//分页查询附件
func (m *Attachment) FindToPager(pageIndex, pageSize int) (attachList []*AttachmentResult, totalCount int64, err error) {
o := orm.NewOrm()
totalCount, err = o.QueryTable(m.TableNameWithPrefix()).Count()
if err != nil {
return
}
offset := (pageIndex - 1) * pageSize
var list []*Attachment
_, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-attachment_id").Offset(offset).Limit(pageSize).All(&list)
if err != nil {
return
}
for _, item := range list {
attach := &AttachmentResult{}
attach.Attachment = *item
attach.FileShortSize = utils.FormatBytes(int64(attach.FileSize))
book := NewBook()
if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", item.BookId).One(book, "book_name", "identify"); e == nil {
attach.BookName = book.BookName
attach.BookIdentify = book.Identify
} else {
attach.BookName = "[不存在]"
}
doc := NewDocument()
if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", item.DocumentId).One(doc, "document_name", "identify"); e == nil {
attach.DocumentName = doc.DocumentName
attach.DocIdentify = doc.Identify
} else {
attach.DocumentName = "[不存在]"
}
attach.LocalHttpPath = strings.Replace(item.FilePath, "\\", "/", -1)
attachList = append(attachList, attach)
}
return
}