/
attachment.go
61 lines (49 loc) · 2.17 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
package model
import (
"gorm.io/gorm"
"github.com/deatil/go-hash/hash"
"github.com/deatil/go-datebin/datebin"
"github.com/deatil/lakego-doak/lakego/random"
"github.com/deatil/lakego-doak/lakego/facade/database"
"github.com/deatil/lakego-doak-admin/admin/support/url"
)
// 附件
type Attachment struct {
ID string `gorm:"column:id;size:32;not null;index;" json:"id"`
OwnerType string `gorm:"column:owner_type;size:50;not null;" json:"owner_type"`
OwnerID string `gorm:"column:owner_id;size:32;" json:"owner_id"`
Name string `gorm:"column:name;size:50;" json:"name"`
Path string `gorm:"column:path;size:255;" json:"path"`
Mime string `gorm:"column:mime;size:100;" json:"mime"`
Extension string `gorm:"column:extension;size:10;" json:"extension"`
Size string `gorm:"column:size;size:100;" json:"size"`
Md5 string `gorm:"column:md5;size:32;" json:"md5"`
Sha1 string `gorm:"column:sha1;size:40;" json:"sha1"`
Disk string `gorm:"column:disk;size:16;" json:"disk"`
Status int `gorm:"column:status;not null;size:1;" json:"status"`
UpdateTime int `gorm:"column:update_time;size:10;" json:"update_time"`
CreateTime int `gorm:"column:create_time;size:10;" json:"create_time"`
AddTime int `gorm:"column:add_time;size:10;" json:"add_time"`
AddIp string `gorm:"column:add_ip;size:50;" json:"add_ip"`
}
func (this *Attachment) BeforeCreate(tx *gorm.DB) error {
this.ID = hash.MD5(datebin.NowDatetimeString() + random.String(15))
return nil
}
func NewAttachment() *gorm.DB {
return database.New().Model(&Attachment{})
}
// 附件链接
func AttachmentUrl(id string) string {
result := map[string]any{}
// 附件模型
err := NewAttachment().
Where("id = ?", id).
First(&result).
Error
if err != nil || len(result) < 1 {
return ""
}
// 格式化链接
return url.AttachmentUrl(result["path"].(string), result["disk"].(string))
}