使用Request.Context

This commit is contained in:
j2rong4cn
2025-07-12 22:47:26 +08:00
parent d669a62ccf
commit a0dc472e0e
11 changed files with 54 additions and 54 deletions

View File

@ -12,7 +12,7 @@ import (
)
func NewAuthnInstance(c *gin.Context) (*webauthn.WebAuthn, error) {
siteUrl, err := url.Parse(common.GetApiUrl(c))
siteUrl, err := url.Parse(common.GetApiUrl(c.Request.Context()))
if err != nil {
return nil, err
}

View File

@ -17,14 +17,14 @@ func _pprof(g *gin.RouterGroup) {
}
func debug(g *gin.RouterGroup) {
g.GET("/path/*path", middlewares.Down(sign.Verify), func(ctx *gin.Context) {
rawPath := ctx.Request.Context().Value(conf.PathKey).(string)
ctx.JSON(200, gin.H{
g.GET("/path/*path", middlewares.Down(sign.Verify), func(c *gin.Context) {
rawPath := c.Request.Context().Value(conf.PathKey).(string)
c.JSON(200, gin.H{
"path": rawPath,
})
})
g.GET("/hide_privacy", func(ctx *gin.Context) {
common.ErrorStrResp(ctx, "This is ip: 1.1.1.1", 400)
g.GET("/hide_privacy", func(c *gin.Context) {
common.ErrorStrResp(c, "This is ip: 1.1.1.1", 400)
})
g.GET("/gc", func(c *gin.Context) {
runtime.GC()

View File

@ -106,7 +106,7 @@ func FsArchiveMeta(c *gin.Context) {
},
Password: req.ArchivePass,
}
ret, err := fs.ArchiveMeta(c, reqPath, model.ArchiveMetaArgs{
ret, err := fs.ArchiveMeta(c.Request.Context(), reqPath, model.ArchiveMetaArgs{
ArchiveArgs: archiveArgs,
Refresh: req.Refresh,
})
@ -176,7 +176,7 @@ func FsArchiveList(c *gin.Context) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
return
}
objs, err := fs.ArchiveList(c, reqPath, model.ArchiveListArgs{
objs, err := fs.ArchiveList(c.Request.Context(), reqPath, model.ArchiveListArgs{
ArchiveInnerArgs: model.ArchiveInnerArgs{
ArchiveArgs: model.ArchiveArgs{
LinkArgs: model.LinkArgs{
@ -260,7 +260,7 @@ func FsArchiveDecompress(c *gin.Context) {
}
tasks := make([]task.TaskExtensionInfo, 0, len(srcPaths))
for _, srcPath := range srcPaths {
t, e := fs.ArchiveDecompress(c, srcPath, dstDir, model.ArchiveDecompressArgs{
t, e := fs.ArchiveDecompress(c.Request.Context(), srcPath, dstDir, model.ArchiveDecompressArgs{
ArchiveInnerArgs: model.ArchiveInnerArgs{
ArchiveArgs: model.ArchiveArgs{
LinkArgs: model.LinkArgs{
@ -305,7 +305,7 @@ func ArchiveDown(c *gin.Context) {
ArchiveProxy(c)
return
} else {
link, _, err := fs.ArchiveDriverExtract(c, archiveRawPath, model.ArchiveInnerArgs{
link, _, err := fs.ArchiveDriverExtract(c.Request.Context(), archiveRawPath, model.ArchiveInnerArgs{
ArchiveArgs: model.ArchiveArgs{
LinkArgs: model.LinkArgs{
IP: c.ClientIP(),
@ -337,7 +337,7 @@ func ArchiveProxy(c *gin.Context) {
}
if canProxy(storage, filename) {
// TODO: Support external download proxy URL
link, file, err := fs.ArchiveDriverExtract(c, archiveRawPath, model.ArchiveInnerArgs{
link, file, err := fs.ArchiveDriverExtract(c.Request.Context(), archiveRawPath, model.ArchiveInnerArgs{
ArchiveArgs: model.ArchiveArgs{
LinkArgs: model.LinkArgs{
Header: c.Request.Header,
@ -362,7 +362,7 @@ func ArchiveInternalExtract(c *gin.Context) {
archiveRawPath := c.Request.Context().Value(conf.PathKey).(string)
innerPath := utils.FixAndCleanPath(c.Query("inner"))
password := c.Query("pass")
rc, size, err := fs.ArchiveInternalExtract(c, archiveRawPath, model.ArchiveInnerArgs{
rc, size, err := fs.ArchiveInternalExtract(c.Request.Context(), archiveRawPath, model.ArchiveInnerArgs{
ArchiveArgs: model.ArchiveArgs{
LinkArgs: model.LinkArgs{
Header: c.Request.Header,

View File

@ -35,7 +35,7 @@ func Down(c *gin.Context) {
Proxy(c)
return
} else {
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
link, _, err := fs.Link(c.Request.Context(), rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
Type: c.Query("type"),
@ -70,7 +70,7 @@ func Proxy(c *gin.Context) {
return
}
}
link, file, err := fs.Link(c, rawPath, model.LinkArgs{
link, file, err := fs.Link(c.Request.Context(), rawPath, model.LinkArgs{
Header: c.Request.Header,
Type: c.Query("type"),
})

View File

@ -54,7 +54,7 @@ func FsRecursiveMove(c *gin.Context) {
}
common.GinWithValue(c, conf.MetaKey, meta)
rootFiles, err := fs.List(c, srcDir, &fs.ListArgs{})
rootFiles, err := fs.List(c.Request.Context(), srcDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -62,7 +62,7 @@ func FsRecursiveMove(c *gin.Context) {
var existingFileNames []string
if req.ConflictPolicy != OVERWRITE {
dstFiles, err := fs.List(c, dstDir, &fs.ListArgs{})
dstFiles, err := fs.List(c.Request.Context(), dstDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -90,7 +90,7 @@ func FsRecursiveMove(c *gin.Context) {
if movingFile.IsDir() {
// directory, recursive move
subFilePath := movingFileName
subFiles, err := fs.List(c, movingFileName, &fs.ListArgs{Refresh: true})
subFiles, err := fs.List(c.Request.Context(), movingFileName, &fs.ListArgs{Refresh: true})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -124,7 +124,7 @@ func FsRecursiveMove(c *gin.Context) {
var count = 0
for i, fileName := range movingFileNames {
// move
err := fs.Move(c, fileName, dstDir, len(movingFileNames) > i+1)
err := fs.Move(c.Request.Context(), fileName, dstDir, len(movingFileNames) > i+1)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -174,7 +174,7 @@ func FsBatchRename(c *gin.Context) {
continue
}
filePath := fmt.Sprintf("%s/%s", reqPath, renameObject.SrcName)
if err := fs.Rename(c, filePath, renameObject.NewName); err != nil {
if err := fs.Rename(c.Request.Context(), filePath, renameObject.NewName); err != nil {
common.ErrorResp(c, err, 500)
return
}
@ -221,7 +221,7 @@ func FsRegexRename(c *gin.Context) {
return
}
files, err := fs.List(c, reqPath, &fs.ListArgs{})
files, err := fs.List(c.Request.Context(), reqPath, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -232,7 +232,7 @@ func FsRegexRename(c *gin.Context) {
if srcRegexp.MatchString(file.GetName()) {
filePath := fmt.Sprintf("%s/%s", reqPath, file.GetName())
newFileName := srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex)
if err := fs.Rename(c, filePath, newFileName); err != nil {
if err := fs.Rename(c.Request.Context(), filePath, newFileName); err != nil {
common.ErrorResp(c, err, 500)
return
}

View File

@ -48,7 +48,7 @@ func FsMkdir(c *gin.Context) {
return
}
}
if err := fs.MakeDir(c, reqPath); err != nil {
if err := fs.MakeDir(c.Request.Context(), reqPath); err != nil {
common.ErrorResp(c, err, 500)
return
}
@ -90,7 +90,7 @@ func FsMove(c *gin.Context) {
if !req.Overwrite {
for _, name := range req.Names {
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
if res, _ := fs.Get(c.Request.Context(), stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", name), 403)
return
}
@ -101,7 +101,7 @@ func FsMove(c *gin.Context) {
// All validation will be done asynchronously in the background
var addedTasks []task.TaskExtensionInfo
for i, name := range req.Names {
t, err := fs.MoveWithTaskAndValidation(c, stdpath.Join(srcDir, name), dstDir, !req.Overwrite, len(req.Names) > i+1)
t, err := fs.MoveWithTaskAndValidation(c.Request.Context(), stdpath.Join(srcDir, name), dstDir, !req.Overwrite, len(req.Names) > i+1)
if t != nil {
addedTasks = append(addedTasks, t)
}
@ -152,7 +152,7 @@ func FsCopy(c *gin.Context) {
if !req.Overwrite {
for _, name := range req.Names {
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
if res, _ := fs.Get(c.Request.Context(), stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", name), 403)
return
}
@ -163,7 +163,7 @@ func FsCopy(c *gin.Context) {
// All validation will be done asynchronously in the background
var addedTasks []task.TaskExtensionInfo
for i, name := range req.Names {
t, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
t, err := fs.Copy(c.Request.Context(), stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
if t != nil {
addedTasks = append(addedTasks, t)
}
@ -211,13 +211,13 @@ func FsRename(c *gin.Context) {
if !req.Overwrite {
dstPath := stdpath.Join(stdpath.Dir(reqPath), req.Name)
if dstPath != reqPath {
if res, _ := fs.Get(c, dstPath, &fs.GetArgs{NoLog: true}); res != nil {
if res, _ := fs.Get(c.Request.Context(), dstPath, &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", req.Name), 403)
return
}
}
}
if err := fs.Rename(c, reqPath, req.Name); err != nil {
if err := fs.Rename(c.Request.Context(), reqPath, req.Name); err != nil {
common.ErrorResp(c, err, 500)
return
}
@ -250,7 +250,7 @@ func FsRemove(c *gin.Context) {
return
}
for _, name := range req.Names {
err := fs.Remove(c, stdpath.Join(reqDir, name))
err := fs.Remove(c.Request.Context(), stdpath.Join(reqDir, name))
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -291,7 +291,7 @@ func FsRemoveEmptyDirectory(c *gin.Context) {
}
common.GinWithValue(c, conf.MetaKey, meta)
rootFiles, err := fs.List(c, srcDir, &fs.ListArgs{})
rootFiles, err := fs.List(c.Request.Context(), srcDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -322,7 +322,7 @@ func FsRemoveEmptyDirectory(c *gin.Context) {
continue
}
subFiles, err := fs.List(c, removingFilePath, &fs.ListArgs{Refresh: true})
subFiles, err := fs.List(c.Request.Context(), removingFilePath, &fs.ListArgs{Refresh: true})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -330,7 +330,7 @@ func FsRemoveEmptyDirectory(c *gin.Context) {
if len(subFiles) == 0 {
// remove empty directory
err = fs.Remove(c, removingFilePath)
err = fs.Remove(c.Request.Context(), removingFilePath)
removedFiles[removingFilePath] = true
if err != nil {
common.ErrorResp(c, err, 500)
@ -384,7 +384,7 @@ func Link(c *gin.Context) {
})
return
}
link, _, err := fs.Link(c, rawPath, model.LinkArgs{IP: c.ClientIP(), Header: c.Request.Header, Redirect: true})
link, _, err := fs.Link(c.Request.Context(), rawPath, model.LinkArgs{IP: c.ClientIP(), Header: c.Request.Header, Redirect: true})
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -85,7 +85,7 @@ func FsList(c *gin.Context) {
common.ErrorStrResp(c, "Refresh without permission", 403)
return
}
objs, err := fs.List(c, reqPath, &fs.ListArgs{Refresh: req.Refresh})
objs, err := fs.List(c.Request.Context(), reqPath, &fs.ListArgs{Refresh: req.Refresh})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -139,7 +139,7 @@ func FsDirs(c *gin.Context) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
return
}
objs, err := fs.List(c, reqPath, &fs.ListArgs{})
objs, err := fs.List(c.Request.Context(), reqPath, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -267,7 +267,7 @@ func FsGet(c *gin.Context) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
return
}
obj, err := fs.Get(c, reqPath, &fs.GetArgs{})
obj, err := fs.Get(c.Request.Context(), reqPath, &fs.GetArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -306,7 +306,7 @@ func FsGet(c *gin.Context) {
rawURL = url
} else {
// if storage is not proxy, use raw url by fs.Link
link, _, err := fs.Link(c, reqPath, model.LinkArgs{
link, _, err := fs.Link(c.Request.Context(), reqPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
Redirect: true,
@ -322,7 +322,7 @@ func FsGet(c *gin.Context) {
}
var related []model.Obj
parentPath := stdpath.Dir(reqPath)
sameLevelFiles, err := fs.List(c, parentPath, &fs.ListArgs{})
sameLevelFiles, err := fs.List(c.Request.Context(), parentPath, &fs.ListArgs{})
if err == nil {
related = filterRelated(sameLevelFiles, obj)
}
@ -395,7 +395,7 @@ func FsOther(c *gin.Context) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
return
}
res, err := fs.Other(c, req.FsOtherArgs)
res, err := fs.Other(c.Request.Context(), req.FsOtherArgs)
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -50,7 +50,7 @@ func FsStream(c *gin.Context) {
return
}
if !overwrite {
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
if res, _ := fs.Get(c.Request.Context(), path, &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
@ -92,9 +92,9 @@ func FsStream(c *gin.Context) {
}
var t task.TaskExtensionInfo
if asTask {
t, err = fs.PutAsTask(c, dir, s)
t, err = fs.PutAsTask(c.Request.Context(), dir, s)
} else {
err = fs.PutDirectly(c, dir, s, true)
err = fs.PutDirectly(c.Request.Context(), dir, s, true)
}
if err != nil {
common.ErrorResp(c, err, 500)
@ -131,7 +131,7 @@ func FsForm(c *gin.Context) {
return
}
if !overwrite {
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
if res, _ := fs.Get(c.Request.Context(), path, &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
@ -187,9 +187,9 @@ func FsForm(c *gin.Context) {
s.Reader = struct {
io.Reader
}{f}
t, err = fs.PutAsTask(c, dir, s)
t, err = fs.PutAsTask(c.Request.Context(), dir, s)
} else {
err = fs.PutDirectly(c, dir, s, true)
err = fs.PutDirectly(c.Request.Context(), dir, s, true)
}
if err != nil {
common.ErrorResp(c, err, 500)

View File

@ -17,7 +17,7 @@ type SSHKeyAddReq struct {
}
func AddMyPublicKey(c *gin.Context) {
userObj, ok := c.Value(conf.UserKey).(*model.User)
userObj, ok := c.Request.Context().Value(conf.UserKey).(*model.User)
if !ok || userObj.IsGuest() {
common.ErrorStrResp(c, "user invalid", 401)
return
@ -48,7 +48,7 @@ func AddMyPublicKey(c *gin.Context) {
}
func ListMyPublicKey(c *gin.Context) {
userObj, ok := c.Value(conf.UserKey).(*model.User)
userObj, ok := c.Request.Context().Value(conf.UserKey).(*model.User)
if !ok || userObj.IsGuest() {
common.ErrorStrResp(c, "user invalid", 401)
return
@ -57,7 +57,7 @@ func ListMyPublicKey(c *gin.Context) {
}
func DeleteMyPublicKey(c *gin.Context) {
userObj, ok := c.Value(conf.UserKey).(*model.User)
userObj, ok := c.Request.Context().Value(conf.UserKey).(*model.User)
if !ok || userObj.IsGuest() {
common.ErrorStrResp(c, "user invalid", 401)
return

View File

@ -38,7 +38,7 @@ func CreateStorage(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if id, err := op.CreateStorage(c, req); err != nil {
if id, err := op.CreateStorage(c.Request.Context(), req); err != nil {
common.ErrorWithDataResp(c, err, 500, gin.H{
"id": id,
}, true)
@ -55,7 +55,7 @@ func UpdateStorage(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if err := op.UpdateStorage(c, req); err != nil {
if err := op.UpdateStorage(c.Request.Context(), req); err != nil {
common.ErrorResp(c, err, 500, true)
} else {
common.SuccessResp(c)
@ -69,7 +69,7 @@ func DeleteStorage(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if err := op.DeleteStorageById(c, uint(id)); err != nil {
if err := op.DeleteStorageById(c.Request.Context(), uint(id)); err != nil {
common.ErrorResp(c, err, 500, true)
return
}
@ -83,7 +83,7 @@ func DisableStorage(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if err := op.DisableStorage(c, uint(id)); err != nil {
if err := op.DisableStorage(c.Request.Context(), uint(id)); err != nil {
common.ErrorResp(c, err, 500, true)
return
}
@ -97,7 +97,7 @@ func EnableStorage(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if err := op.EnableStorage(c, uint(id)); err != nil {
if err := op.EnableStorage(c.Request.Context(), uint(id)); err != nil {
common.ErrorResp(c, err, 500, true)
return
}

View File

@ -70,7 +70,7 @@ func argsContains[T comparable](v T, slice ...T) bool {
}
func getUserInfo(c *gin.Context) (bool, uint, bool) {
if user, ok := c.Value(conf.UserKey).(*model.User); ok {
if user, ok := c.Request.Context().Value(conf.UserKey).(*model.User); ok {
return user.IsAdmin(), user.ID, true
} else {
return false, 0, false