Skip to content

Commit 219564b

Browse files
authored
refactor(func): replace GinWithValue with GinAppendValues (#2475)
1 parent ea19bed commit 219564b

10 files changed

Lines changed: 43 additions & 32 deletions

File tree

server/common/common.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,24 @@ func Pluralize(count int, singular, plural string) string {
128128
return plural
129129
}
130130

131-
func GinWithValue(c *gin.Context, keyAndValue ...any) {
131+
type requestContext struct {
132+
context.Context
133+
}
134+
135+
// GinAppendValues 向当前请求上下文追加键值,提供类似 gin.Context Set/Get 的可变语义。
136+
// 同一请求内,已持有的上下文引用会同步看到后续更新。
137+
func GinAppendValues(c *gin.Context, keyAndValue ...any) {
138+
ctx := c.Request.Context()
139+
if r, ok := ctx.(*requestContext); ok {
140+
r.Context = ContentWithValues(r.Context, keyAndValue...)
141+
return
142+
}
132143
c.Request = c.Request.WithContext(
133-
ContentWithValue(c.Request.Context(), keyAndValue...),
144+
&requestContext{ContentWithValues(ctx, keyAndValue...)},
134145
)
135146
}
136147

137-
func ContentWithValue(ctx context.Context, keyAndValue ...any) context.Context {
148+
func ContentWithValues(ctx context.Context, keyAndValue ...any) context.Context {
138149
if len(keyAndValue) < 1 || len(keyAndValue)%2 != 0 {
139150
panic("keyAndValue must be an even number of arguments (key, value, ...)")
140151
}

server/handles/archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func FsArchiveMeta(c *gin.Context, req *ArchiveMetaReq, user *model.User) {
105105
common.ErrorResp(c, err, 500, true)
106106
return
107107
}
108-
common.GinWithValue(c, conf.MetaKey, meta)
108+
common.GinAppendValues(c, conf.MetaKey, meta)
109109
if !common.CanAccess(user, meta, reqPath, req.Password) {
110110
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
111111
return
@@ -188,7 +188,7 @@ func FsArchiveList(c *gin.Context, req *ArchiveListReq, user *model.User) {
188188
common.ErrorResp(c, err, 500, true)
189189
return
190190
}
191-
common.GinWithValue(c, conf.MetaKey, meta)
191+
common.GinAppendValues(c, conf.MetaKey, meta)
192192
if !common.CanAccess(user, meta, reqPath, req.Password) {
193193
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
194194
return

server/handles/fsbatch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func FsRecursiveMove(c *gin.Context) {
4949
common.ErrorResp(c, errs.PermissionDenied, 403)
5050
return
5151
}
52-
common.GinWithValue(c, conf.MetaKey, srcMeta)
52+
common.GinAppendValues(c, conf.MetaKey, srcMeta)
5353

5454
dstDir, err := user.JoinPath(req.DstDir)
5555
if err != nil {
@@ -183,7 +183,7 @@ func FsBatchRename(c *gin.Context) {
183183
common.ErrorResp(c, errs.PermissionDenied, 403)
184184
return
185185
}
186-
common.GinWithValue(c, conf.MetaKey, meta)
186+
common.GinAppendValues(c, conf.MetaKey, meta)
187187
for _, renameObject := range req.RenameObjects {
188188
if renameObject.SrcName == "" || renameObject.NewName == "" {
189189
continue
@@ -236,7 +236,7 @@ func FsRegexRename(c *gin.Context) {
236236
common.ErrorResp(c, errs.PermissionDenied, 403)
237237
return
238238
}
239-
common.GinWithValue(c, conf.MetaKey, meta)
239+
common.GinAppendValues(c, conf.MetaKey, meta)
240240

241241
srcRegexp, err := regexp.Compile(req.SrcNameRegex)
242242
if err != nil {

server/handles/fsmanage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func FsRemoveEmptyDirectory(c *gin.Context) {
425425
common.ErrorResp(c, errs.PermissionDenied, 403)
426426
return
427427
}
428-
common.GinWithValue(c, conf.MetaKey, meta)
428+
common.GinAppendValues(c, conf.MetaKey, meta)
429429

430430
rootFiles, err := fs.List(c.Request.Context(), srcDir, &fs.ListArgs{})
431431
if err != nil {

server/handles/fsread.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func FsList(c *gin.Context, req *ListReq, user *model.User) {
8888
common.ErrorResp(c, err, 500, true)
8989
return
9090
}
91-
common.GinWithValue(c, conf.MetaKey, meta)
91+
common.GinAppendValues(c, conf.MetaKey, meta)
9292
if !common.CanAccess(user, meta, reqPath, req.Password) {
9393
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
9494
return
@@ -152,7 +152,7 @@ func FsDirs(c *gin.Context) {
152152
common.ErrorResp(c, err, 500, true)
153153
return
154154
}
155-
common.GinWithValue(c, conf.MetaKey, meta)
155+
common.GinAppendValues(c, conf.MetaKey, meta)
156156
if !common.CanAccess(user, meta, reqPath, req.Password) {
157157
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
158158
return
@@ -291,7 +291,7 @@ func FsGet(c *gin.Context, req *FsGetReq, user *model.User) {
291291
common.ErrorResp(c, err, 500, true)
292292
return
293293
}
294-
common.GinWithValue(c, conf.MetaKey, meta)
294+
common.GinAppendValues(c, conf.MetaKey, meta)
295295
if !common.CanAccess(user, meta, reqPath, req.Password) {
296296
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
297297
return
@@ -415,7 +415,7 @@ func FsOther(c *gin.Context) {
415415
common.ErrorResp(c, err, 500)
416416
return
417417
}
418-
common.GinWithValue(c, conf.MetaKey, meta)
418+
common.GinAppendValues(c, conf.MetaKey, meta)
419419
if !common.CanAccess(user, meta, req.Path, req.Password) {
420420
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
421421
return

server/middlewares/auth.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Auth(allowDisabledGuest bool) func(c *gin.Context) {
2424
c.Abort()
2525
return
2626
}
27-
common.GinWithValue(c, conf.UserKey, admin)
27+
common.GinAppendValues(c, conf.UserKey, admin)
2828
log.Debugf("use admin token: %+v", admin)
2929
c.Next()
3030
return
@@ -41,7 +41,7 @@ func Auth(allowDisabledGuest bool) func(c *gin.Context) {
4141
c.Abort()
4242
return
4343
}
44-
common.GinWithValue(c, conf.UserKey, guest)
44+
common.GinAppendValues(c, conf.UserKey, guest)
4545
log.Debugf("use empty token: %+v", guest)
4646
c.Next()
4747
return
@@ -69,7 +69,7 @@ func Auth(allowDisabledGuest bool) func(c *gin.Context) {
6969
c.Abort()
7070
return
7171
}
72-
common.GinWithValue(c, conf.UserKey, user)
72+
common.GinAppendValues(c, conf.UserKey, user)
7373
log.Debugf("use login token: %+v", user)
7474
c.Next()
7575
}
@@ -84,7 +84,7 @@ func Authn(c *gin.Context) {
8484
c.Abort()
8585
return
8686
}
87-
common.GinWithValue(c, conf.UserKey, admin)
87+
common.GinAppendValues(c, conf.UserKey, admin)
8888
log.Debugf("use admin token: %+v", admin)
8989
c.Next()
9090
return
@@ -96,7 +96,7 @@ func Authn(c *gin.Context) {
9696
c.Abort()
9797
return
9898
}
99-
common.GinWithValue(c, conf.UserKey, guest)
99+
common.GinAppendValues(c, conf.UserKey, guest)
100100
log.Debugf("use empty token: %+v", guest)
101101
c.Next()
102102
return
@@ -124,7 +124,7 @@ func Authn(c *gin.Context) {
124124
c.Abort()
125125
return
126126
}
127-
common.GinWithValue(c, conf.UserKey, user)
127+
common.GinAppendValues(c, conf.UserKey, user)
128128
log.Debugf("use login token: %+v", user)
129129
c.Next()
130130
}

server/middlewares/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func StoragesLoaded(c *gin.Context) {
2929
return
3030
}
3131
}
32-
common.GinWithValue(c,
32+
common.GinAppendValues(c,
3333
conf.ApiUrlKey, common.GetApiUrlFromRequest(c.Request),
3434
)
3535
c.Next()

server/middlewares/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
func PathParse(c *gin.Context) {
1919
rawPath := parsePath(c.Param("path"))
20-
common.GinWithValue(c, conf.PathKey, rawPath)
20+
common.GinAppendValues(c, conf.PathKey, rawPath)
2121
c.Next()
2222
}
2323

@@ -29,7 +29,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
2929
common.ErrorPage(c, err, 500, true)
3030
return
3131
}
32-
common.GinWithValue(c, conf.MetaKey, meta)
32+
common.GinAppendValues(c, conf.MetaKey, meta)
3333
// verify sign
3434
if needSign(meta, rawPath) {
3535
s := c.Query("sign")

server/middlewares/sharing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
func SharingIdParse(c *gin.Context) {
1010
sid := c.Param("sid")
11-
common.GinWithValue(c, conf.SharingIDKey, sid)
11+
common.GinAppendValues(c, conf.SharingIDKey, sid)
1212
c.Next()
1313
}
1414

1515
func EmptyPathParse(c *gin.Context) {
16-
common.GinWithValue(c, conf.PathKey, "/")
16+
common.GinAppendValues(c, conf.PathKey, "/")
1717
c.Next()
1818
}

server/webdav.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func WebDAVAuth(c *gin.Context) {
5454
count, cok := model.LoginCache.Get(ip)
5555
if cok && count >= model.DefaultMaxAuthRetries {
5656
if c.Request.Method == "OPTIONS" {
57-
common.GinWithValue(c, conf.UserKey, guest)
57+
common.GinAppendValues(c, conf.UserKey, guest)
5858
c.Next()
5959
return
6060
}
@@ -78,13 +78,13 @@ func WebDAVAuth(c *gin.Context) {
7878
c.Abort()
7979
return
8080
}
81-
common.GinWithValue(c, conf.UserKey, admin)
81+
common.GinAppendValues(c, conf.UserKey, admin)
8282
c.Next()
8383
return
8484
}
8585
}
8686
if c.Request.Method == "OPTIONS" {
87-
common.GinWithValue(c, conf.UserKey, guest)
87+
common.GinAppendValues(c, conf.UserKey, guest)
8888
c.Next()
8989
return
9090
}
@@ -96,7 +96,7 @@ func WebDAVAuth(c *gin.Context) {
9696
user, ok := tryLogin(username, password)
9797
if !ok {
9898
if c.Request.Method == "OPTIONS" {
99-
common.GinWithValue(c, conf.UserKey, guest)
99+
common.GinAppendValues(c, conf.UserKey, guest)
100100
c.Next()
101101
return
102102
}
@@ -109,7 +109,7 @@ func WebDAVAuth(c *gin.Context) {
109109
model.LoginCache.Del(ip)
110110
if user.Disabled || !user.CanWebdavRead() {
111111
if c.Request.Method == "OPTIONS" {
112-
common.GinWithValue(c, conf.UserKey, guest)
112+
common.GinAppendValues(c, conf.UserKey, guest)
113113
c.Next()
114114
return
115115
}
@@ -142,11 +142,11 @@ func WebDAVAuth(c *gin.Context) {
142142
c.Abort()
143143
return
144144
}
145-
common.GinWithValue(c, conf.UserKey, user)
145+
common.GinAppendValues(c, conf.UserKey, user)
146146
if user.IsGuest() {
147-
common.GinWithValue(c, conf.MetaPassKey, password)
147+
common.GinAppendValues(c, conf.MetaPassKey, password)
148148
} else {
149-
common.GinWithValue(c, conf.MetaPassKey, "")
149+
common.GinAppendValues(c, conf.MetaPassKey, "")
150150
}
151151
c.Next()
152152
}

0 commit comments

Comments
 (0)