mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-08 19:31:33 +08:00
refactor: replace key package with ctxkey for context keys
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
|
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
|
||||||
"github.com/krau/SaveAny-Bot/config"
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get file stat: %w", err)
|
return fmt.Errorf("failed to get file stat: %w", err)
|
||||||
}
|
}
|
||||||
vctx := context.WithValue(ctx, key.ContextKeyContentLength, fileStat.Size())
|
vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size())
|
||||||
err = retry.Retry(func() error {
|
err = retry.Retry(func() error {
|
||||||
var file *os.File
|
var file *os.File
|
||||||
file, err = os.Open(elem.localPath)
|
file, err = os.Open(elem.localPath)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/common/tdler"
|
"github.com/krau/SaveAny-Bot/common/tdler"
|
||||||
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
"github.com/krau/SaveAny-Bot/config"
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *TGFileTask) Execute(ctx context.Context) error {
|
func (t *TGFileTask) Execute(ctx context.Context) error {
|
||||||
@@ -52,7 +52,7 @@ func (t *TGFileTask) Execute(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get file stat: %w", err)
|
return fmt.Errorf("failed to get file stat: %w", err)
|
||||||
}
|
}
|
||||||
vctx := context.WithValue(ctx, key.ContextKeyContentLength, fileStat.Size())
|
vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size())
|
||||||
for i := range config.Cfg.Retry + 1 {
|
for i := range config.Cfg.Retry + 1 {
|
||||||
if err = vctx.Err(); err != nil {
|
if err = vctx.Err(); err != nil {
|
||||||
return fmt.Errorf("context canceled while saving file: %w", err)
|
return fmt.Errorf("context canceled while saving file: %w", err)
|
||||||
|
|||||||
5
pkg/enums/ctxkey/context_key.go
Normal file
5
pkg/enums/ctxkey/context_key.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package ctxkey
|
||||||
|
|
||||||
|
//go:generate go-enum --values --names --flag --nocase --noprefix
|
||||||
|
// ENUM(content-length)
|
||||||
|
type ContextKey string
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
// Build Date: 2025-03-18T23:42:14Z
|
// Build Date: 2025-03-18T23:42:14Z
|
||||||
// Built By: goreleaser
|
// Built By: goreleaser
|
||||||
|
|
||||||
package key
|
package ctxkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,14 +12,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ContextKeyContentLength is a ContextKey of type content-length.
|
// ContentLength is a ContextKey of type content-length.
|
||||||
ContextKeyContentLength ContextKey = "content-length"
|
ContentLength ContextKey = "content-length"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrInvalidContextKey = fmt.Errorf("not a valid ContextKey, try [%s]", strings.Join(_ContextKeyNames, ", "))
|
var ErrInvalidContextKey = fmt.Errorf("not a valid ContextKey, try [%s]", strings.Join(_ContextKeyNames, ", "))
|
||||||
|
|
||||||
var _ContextKeyNames = []string{
|
var _ContextKeyNames = []string{
|
||||||
string(ContextKeyContentLength),
|
string(ContentLength),
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContextKeyNames returns a list of possible string values of ContextKey.
|
// ContextKeyNames returns a list of possible string values of ContextKey.
|
||||||
@@ -32,7 +32,7 @@ func ContextKeyNames() []string {
|
|||||||
// ContextKeyValues returns a list of the values for ContextKey
|
// ContextKeyValues returns a list of the values for ContextKey
|
||||||
func ContextKeyValues() []ContextKey {
|
func ContextKeyValues() []ContextKey {
|
||||||
return []ContextKey{
|
return []ContextKey{
|
||||||
ContextKeyContentLength,
|
ContentLength,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ func (x ContextKey) IsValid() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var _ContextKeyValue = map[string]ContextKey{
|
var _ContextKeyValue = map[string]ContextKey{
|
||||||
"content-length": ContextKeyContentLength,
|
"content-length": ContentLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseContextKey attempts to convert a string to a ContextKey.
|
// ParseContextKey attempts to convert a string to a ContextKey.
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package key
|
|
||||||
|
|
||||||
//go:generate go-enum --values --names --flag --nocase
|
|
||||||
// ENUM(content-length)
|
|
||||||
type ContextKey string
|
|
||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
config "github.com/krau/SaveAny-Bot/config/storage"
|
config "github.com/krau/SaveAny-Bot/config/storage"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string)
|
|||||||
req.Header.Set("Authorization", a.token)
|
req.Header.Set("Authorization", a.token)
|
||||||
req.Header.Set("File-Path", url.PathEscape(candidate))
|
req.Header.Set("File-Path", url.PathEscape(candidate))
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
if length := ctx.Value(key.ContextKeyContentLength); length != nil {
|
if length := ctx.Value(ctxkey.ContentLength); length != nil {
|
||||||
length, ok := length.(int64)
|
length, ok := length.(int64)
|
||||||
if ok {
|
if ok {
|
||||||
req.ContentLength = length
|
req.ContentLength = length
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
config "github.com/krau/SaveAny-Bot/config/storage"
|
config "github.com/krau/SaveAny-Bot/config/storage"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
@@ -74,7 +74,7 @@ func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error
|
|||||||
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
||||||
}
|
}
|
||||||
size := int64(-1)
|
size := int64(-1)
|
||||||
if length := ctx.Value(key.ContextKeyContentLength); length != nil {
|
if length := ctx.Value(ctxkey.ContentLength); length != nil {
|
||||||
length, ok := length.(int64)
|
length, ok := length.(int64)
|
||||||
if ok && length > 0 {
|
if ok && length > 0 {
|
||||||
size = length
|
size = length
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -54,7 +54,7 @@ func (c *Client) doRequest(ctx context.Context, method WebdavMethod, url string,
|
|||||||
req.Header.Set("Depth", "1")
|
req.Header.Set("Depth", "1")
|
||||||
}
|
}
|
||||||
if method == WebdavMethodPut && ctx != nil {
|
if method == WebdavMethodPut && ctx != nil {
|
||||||
if length := ctx.Value(key.ContextKeyContentLength); length != nil {
|
if length := ctx.Value(ctxkey.ContentLength); length != nil {
|
||||||
if l, ok := length.(int64); ok {
|
if l, ok := length.(int64); ok {
|
||||||
req.ContentLength = l
|
req.ContentLength = l
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user