chore: standardize context keys with custom ContextKey type (#697)

* chore: standardize context keys with custom ContextKey type

* fix bug

* 使用Request.Context
This commit is contained in:
j2rong4cn
2025-07-14 23:55:17 +08:00
committed by GitHub
parent 2a4c546a8b
commit 0c461991f9
51 changed files with 253 additions and 219 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

@ -149,7 +149,19 @@ const (
)
// ContextKey is the type of context keys.
type ContextKey int
const (
NoTaskKey = "no_task"
ApiUrlKey = "api_url"
_ ContextKey = iota
NoTaskKey
ApiUrlKey
UserKey
MetaKey
MetaPassKey
ClientIPKey
ProxyHeaderKey
RequestHeaderKey
UserAgentKey
PathKey
)

View File

@ -7,7 +7,6 @@ import (
"io"
"math/rand"
"mime"
"net/http"
"os"
stdpath "path"
"path/filepath"
@ -68,9 +67,7 @@ func (t *ArchiveDownloadTask) RunWithoutPushUploadTask() (*ArchiveContentUploadT
if t.srcStorage == nil {
t.srcStorage, err = op.GetStorageByMountPath(t.SrcStorageMp)
}
srcObj, tool, ss, err := op.GetArchiveToolAndStream(t.Ctx(), t.srcStorage, t.SrcObjPath, model.LinkArgs{
Header: http.Header{},
})
srcObj, tool, ss, err := op.GetArchiveToolAndStream(t.Ctx(), t.srcStorage, t.SrcObjPath, model.LinkArgs{})
if err != nil {
return nil, err
}
@ -355,7 +352,7 @@ func archiveDecompress(ctx context.Context, srcObjPath, dstDirPath string, args
return nil, err
}
}
taskCreator, _ := ctx.Value("user").(*model.User)
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User)
tsk := &ArchiveDownloadTask{
TaskExtension: task.TaskExtension{
Creator: taskCreator,

View File

@ -102,7 +102,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
}
}
// not in the same storage
taskCreator, _ := ctx.Value("user").(*model.User)
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User)
t := &CopyTask{
TaskExtension: task.TaskExtension{
Creator: taskCreator,

View File

@ -3,6 +3,7 @@ package fs
import (
"context"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
@ -12,8 +13,8 @@ import (
// List files
func list(ctx context.Context, path string, args *ListArgs) ([]model.Obj, error) {
meta, _ := ctx.Value("meta").(*model.Meta)
user, _ := ctx.Value("user").(*model.User)
meta, _ := ctx.Value(conf.MetaKey).(*model.Meta)
user, _ := ctx.Value(conf.UserKey).(*model.User)
virtualFiles := op.GetStorageVirtualFilesByPath(path)
storage, actualPath, err := op.GetStorageAndActualPath(path)
if err != nil && len(virtualFiles) == 0 {

View File

@ -7,6 +7,7 @@ import (
"sync"
"time"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/driver"
"github.com/OpenListTeam/OpenList/v4/internal/errs"
"github.com/OpenListTeam/OpenList/v4/internal/model"
@ -586,7 +587,7 @@ func _moveWithValidation(ctx context.Context, srcObjPath, dstDirPath string, val
}
}
taskCreator, _ := ctx.Value("user").(*model.User)
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User)
// Create task immediately without any synchronous checks to avoid blocking frontend
// All validation and type checking will be done asynchronously in the Run method

View File

@ -5,6 +5,7 @@ import (
"fmt"
"time"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/driver"
"github.com/OpenListTeam/OpenList/v4/internal/errs"
"github.com/OpenListTeam/OpenList/v4/internal/model"
@ -55,7 +56,7 @@ func putAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer)
//file.SetReader(tempFile)
//file.SetTmpFile(tempFile)
}
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User) // taskCreator is nil when convert failed
t := &UploadTask{
TaskExtension: task.TaskExtension{
Creator: taskCreator,

View File

@ -5,6 +5,7 @@ import (
"path"
"path/filepath"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
)
@ -28,7 +29,7 @@ func WalkFS(ctx context.Context, depth int, name string, info model.Obj, walkFn
}
meta, _ := op.GetNearestMeta(name)
// Read directory names.
objs, err := List(context.WithValue(ctx, "meta", meta), name, &ListArgs{})
objs, err := List(context.WithValue(ctx, conf.MetaKey, meta), name, &ListArgs{})
if err != nil {
return walkFnErr
}

View File

@ -350,5 +350,3 @@ func GetRangedHttpReader(readCloser io.ReadCloser, offset, length int64) (io.Rea
// return an io.ReadCloser that is limited to `length` bytes.
return &LimitedReadCloser{readCloser, length_int}, nil
}
type RequestHeaderKey struct{}

View File

@ -122,7 +122,7 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
}
}
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User) // taskCreator is nil when convert failed
t := &DownloadTask{
TaskExtension: task.TaskExtension{
Creator: taskCreator,

View File

@ -181,7 +181,7 @@ func (t *DownloadTask) Transfer() error {
if err != nil {
return errors.WithMessage(err, "failed get dst storage")
}
taskCreator, _ := t.Ctx().Value("user").(*model.User)
taskCreator, _ := t.Ctx().Value(conf.UserKey).(*model.User)
task := &TransferTask{
TaskExtension: task.TaskExtension{
Creator: taskCreator,

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"time"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/driver"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
@ -116,7 +117,7 @@ func transferStd(ctx context.Context, tempDir, dstDirPath string, deletePolicy D
if err != nil {
return err
}
taskCreator, _ := ctx.Value("user").(*model.User)
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User)
for _, entry := range entries {
t := &TransferTask{
TaskExtension: task.TaskExtension{
@ -216,7 +217,7 @@ func transferObj(ctx context.Context, tempDir, dstDirPath string, deletePolicy D
if err != nil {
return errors.WithMessagef(err, "failed list src [%s] objs", tempDir)
}
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User) // taskCreator is nil when convert failed
for _, obj := range objs {
t := &TransferTask{
TaskExtension: task.TaskExtension{

View File

@ -179,7 +179,7 @@ func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth
return err
}
// TODO: run walkFS concurrently
err = fs.WalkFS(context.WithValue(ctx, "user", admin), maxDepth, indexPath, fi, walkFn)
err = fs.WalkFS(context.WithValue(ctx, conf.UserKey, admin), maxDepth, indexPath, fi, walkFn)
if err != nil {
return err
}

View File

@ -8,6 +8,7 @@ import (
"io"
"net/http"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/net"
"github.com/OpenListTeam/OpenList/v4/pkg/http_range"
@ -38,7 +39,7 @@ func GetRangeReaderFromLink(size int64, link *model.Link) (model.RangeReaderIF,
Size: size,
}
} else {
requestHeader, _ := ctx.Value(net.RequestHeaderKey{}).(http.Header)
requestHeader, _ := ctx.Value(conf.RequestHeaderKey).(http.Header)
header := net.ProcessHeader(requestHeader, link.Header)
req = &net.HttpRequestParams{
Range: httpRange,
@ -67,7 +68,7 @@ func GetRangeReaderFromLink(size int64, link *model.Link) (model.RangeReaderIF,
if httpRange.Length < 0 || httpRange.Start+httpRange.Length > size {
httpRange.Length = size - httpRange.Start
}
requestHeader, _ := ctx.Value(net.RequestHeaderKey{}).(http.Header)
requestHeader, _ := ctx.Value(conf.RequestHeaderKey).(http.Header)
header := net.ProcessHeader(requestHeader, link.Header)
header = http_range.ApplyRangeToHttpHeader(httpRange, header)

View File

@ -20,7 +20,7 @@ type TaskExtension struct {
func (t *TaskExtension) SetCtx(ctx context.Context) {
if t.Creator != nil {
ctx = context.WithValue(ctx, "user", t.Creator)
ctx = context.WithValue(ctx, conf.UserKey, t.Creator)
}
if len(t.ApiUrl) > 0 {
ctx = context.WithValue(ctx, conf.ApiUrlKey, t.ApiUrl)