/
user.go
156 lines (138 loc) · 4.22 KB
/
user.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package serializer
import (
"fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/duo-labs/webauthn/webauthn"
"time"
)
// CheckLogin 检查登录
func CheckLogin() Response {
return Response{
Code: CodeCheckLogin,
Msg: "Login required",
}
}
// User 用户序列化器
type User struct {
ID string `json:"id"`
Email string `json:"user_name"`
Nickname string `json:"nickname"`
Status int `json:"status"`
Avatar string `json:"avatar"`
CreatedAt time.Time `json:"created_at"`
PreferredTheme string `json:"preferred_theme"`
Anonymous bool `json:"anonymous"`
Group group `json:"group"`
Tags []tag `json:"tags"`
}
type group struct {
ID uint `json:"id"`
Name string `json:"name"`
AllowShare bool `json:"allowShare"`
AllowRemoteDownload bool `json:"allowRemoteDownload"`
AllowArchiveDownload bool `json:"allowArchiveDownload"`
ShareDownload bool `json:"shareDownload"`
CompressEnabled bool `json:"compress"`
WebDAVEnabled bool `json:"webdav"`
SourceBatchSize int `json:"sourceBatch"`
AdvanceDelete bool `json:"advanceDelete"`
AllowWebDAVProxy bool `json:"allowWebDAVProxy"`
}
type tag struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Color string `json:"color"`
Type int `json:"type"`
Expression string `json:"expression"`
}
type storage struct {
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Total uint64 `json:"total"`
}
// WebAuthnCredentials 外部验证器凭证
type WebAuthnCredentials struct {
ID []byte `json:"id"`
FingerPrint string `json:"fingerprint"`
}
// BuildWebAuthnList 构建设置页面凭证列表
func BuildWebAuthnList(credentials []webauthn.Credential) []WebAuthnCredentials {
res := make([]WebAuthnCredentials, 0, len(credentials))
for _, v := range credentials {
credential := WebAuthnCredentials{
ID: v.ID,
FingerPrint: fmt.Sprintf("% X", v.Authenticator.AAGUID),
}
res = append(res, credential)
}
return res
}
// BuildUser 序列化用户
func BuildUser(user model.User) User {
tags, _ := model.GetTagsByUID(user.ID)
return User{
ID: hashid.HashID(user.ID, hashid.UserID),
Email: user.Email,
Nickname: user.Nick,
Status: user.Status,
Avatar: user.Avatar,
CreatedAt: user.CreatedAt,
PreferredTheme: user.OptionsSerialized.PreferredTheme,
Anonymous: user.IsAnonymous(),
Group: group{
ID: user.GroupID,
Name: user.Group.Name,
AllowShare: user.Group.ShareEnabled,
AllowRemoteDownload: user.Group.OptionsSerialized.Aria2,
AllowArchiveDownload: user.Group.OptionsSerialized.ArchiveDownload,
ShareDownload: user.Group.OptionsSerialized.ShareDownload,
CompressEnabled: user.Group.OptionsSerialized.ArchiveTask,
WebDAVEnabled: user.Group.WebDAVEnabled,
AllowWebDAVProxy: user.Group.OptionsSerialized.WebDAVProxy,
SourceBatchSize: user.Group.OptionsSerialized.SourceBatchSize,
AdvanceDelete: user.Group.OptionsSerialized.AdvanceDelete,
},
Tags: buildTagRes(tags),
}
}
// BuildUserResponse 序列化用户响应
func BuildUserResponse(user model.User) Response {
return Response{
Data: BuildUser(user),
}
}
// BuildUserStorageResponse 序列化用户存储概况响应
func BuildUserStorageResponse(user model.User) Response {
total := user.Group.MaxStorage
storageResp := storage{
Used: user.Storage,
Free: total - user.Storage,
Total: total,
}
if total < user.Storage {
storageResp.Free = 0
}
return Response{
Data: storageResp,
}
}
// buildTagRes 构建标签列表
func buildTagRes(tags []model.Tag) []tag {
res := make([]tag, 0, len(tags))
for i := 0; i < len(tags); i++ {
newTag := tag{
ID: hashid.HashID(tags[i].ID, hashid.TagID),
Name: tags[i].Name,
Icon: tags[i].Icon,
Color: tags[i].Color,
Type: tags[i].Type,
}
if newTag.Type != 0 {
newTag.Expression = tags[i].Expression
}
res = append(res, newTag)
}
return res
}