chore: stabilize lint and verify builds
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
@@ -22,6 +23,20 @@ import (
|
||||
|
||||
const DefaultPrefix = "Storage"
|
||||
|
||||
var (
|
||||
errStorageBucketNotFound = errors.New("storage bucket not found")
|
||||
errStorageBucketCheckFailed = errors.New("storage bucket check failed")
|
||||
errStorageEndpointRequired = errors.New("storage endpoint is required")
|
||||
errStorageAccessKeyRequired = errors.New("storage access key or secret key is required")
|
||||
errStorageBucketRequired = errors.New("storage bucket is required")
|
||||
errStorageInvalidEndpoint = errors.New("storage endpoint is invalid")
|
||||
errStorageUnsupportedMethod = errors.New("unsupported method")
|
||||
errStorageSignedURLUnsupported = errors.New("s3 storage does not use signed local urls")
|
||||
errStorageInvalidExpiry = errors.New("invalid expiry")
|
||||
errStorageExpired = errors.New("expired")
|
||||
errStorageInvalidSignature = errors.New("invalid signature")
|
||||
)
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
@@ -37,6 +52,7 @@ func Provide(opts ...opt.Option) error {
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*Storage, error) {
|
||||
store := &Storage{Config: &config}
|
||||
if store.storageType() == "s3" {
|
||||
@@ -48,13 +64,14 @@ func Provide(opts ...opt.Option) error {
|
||||
// 启动时可选检查 bucket 是否可用,便于尽早暴露配置问题。
|
||||
exists, err := client.BucketExists(context.Background(), store.Config.Bucket)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("storage bucket check failed: %w", err)
|
||||
return nil, fmt.Errorf("%w: %w", errStorageBucketCheckFailed, err)
|
||||
}
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("storage bucket not found: %s", store.Config.Bucket)
|
||||
return nil, fmt.Errorf("%w: %s", errStorageBucketNotFound, store.Config.Bucket)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return store, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
@@ -72,23 +89,24 @@ func (s *Storage) Download(ctx context.Context, key, filePath string) error {
|
||||
}
|
||||
srcPath := filepath.Join(localPath, key)
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0o755); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("create download dir: %w", err)
|
||||
}
|
||||
src, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("open source file: %w", err)
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
dst, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("create destination file: %w", err)
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, src); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("copy file content: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -97,9 +115,13 @@ func (s *Storage) Download(ctx context.Context, key, filePath string) error {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0o755); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("create download dir: %w", err)
|
||||
}
|
||||
return client.FGetObject(ctx, s.Config.Bucket, key, filePath, minio.GetObjectOptions{})
|
||||
if err := client.FGetObject(ctx, s.Config.Bucket, key, filePath, minio.GetObjectOptions{}); err != nil {
|
||||
return fmt.Errorf("download object: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) Delete(key string) error {
|
||||
@@ -108,14 +130,22 @@ func (s *Storage) Delete(key string) error {
|
||||
if localPath == "" {
|
||||
localPath = "./storage"
|
||||
}
|
||||
path := filepath.Join(localPath, key)
|
||||
return os.Remove(path)
|
||||
filePath := filepath.Join(localPath, key)
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
return fmt.Errorf("remove local object: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
client, err := s.s3ClientForUse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.RemoveObject(context.Background(), s.Config.Bucket, key, minio.RemoveObjectOptions{})
|
||||
if err := client.RemoveObject(context.Background(), s.Config.Bucket, key, minio.RemoveObjectOptions{}); err != nil {
|
||||
return fmt.Errorf("remove s3 object: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) SignURL(method, key string, expires time.Duration) (string, error) {
|
||||
@@ -128,17 +158,19 @@ func (s *Storage) SignURL(method, key string, expires time.Duration) (string, er
|
||||
case "GET":
|
||||
u, err := client.PresignedGetObject(context.Background(), s.Config.Bucket, key, expires, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("presign get object: %w", err)
|
||||
}
|
||||
|
||||
return u.String(), nil
|
||||
case "PUT":
|
||||
u, err := client.PresignedPutObject(context.Background(), s.Config.Bucket, key, expires)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("presign put object: %w", err)
|
||||
}
|
||||
|
||||
return u.String(), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported method")
|
||||
return "", errStorageUnsupportedMethod
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,13 +178,10 @@ func (s *Storage) SignURL(method, key string, expires time.Duration) (string, er
|
||||
sign := s.signature(method, key, exp)
|
||||
|
||||
baseURL := strings.TrimRight(s.Config.BaseURL, "/")
|
||||
// Ensure BaseURL doesn't end with slash if we add one
|
||||
// Simplified: assume standard /v1/storage prefix in BaseURL or append it
|
||||
// We'll append /<key>
|
||||
|
||||
u, err := url.Parse(baseURL + "/" + key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("parse base url: %w", err)
|
||||
}
|
||||
|
||||
q := u.Query()
|
||||
@@ -165,20 +194,21 @@ func (s *Storage) SignURL(method, key string, expires time.Duration) (string, er
|
||||
|
||||
func (s *Storage) Verify(method, key, expStr, sign string) error {
|
||||
if s.storageType() == "s3" {
|
||||
return fmt.Errorf("s3 storage does not use signed local urls")
|
||||
return errStorageSignedURLUnsupported
|
||||
}
|
||||
exp, err := strconv.ParseInt(expStr, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid expiry")
|
||||
return errStorageInvalidExpiry
|
||||
}
|
||||
if time.Now().Unix() > exp {
|
||||
return fmt.Errorf("expired")
|
||||
return errStorageExpired
|
||||
}
|
||||
|
||||
expected := s.signature(method, key, exp)
|
||||
if !hmac.Equal([]byte(expected), []byte(sign)) {
|
||||
return fmt.Errorf("invalid signature")
|
||||
return errStorageInvalidSignature
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -186,6 +216,7 @@ func (s *Storage) signature(method, key string, exp int64) string {
|
||||
str := fmt.Sprintf("%s\n%s\n%d", method, key, exp)
|
||||
h := hmac.New(sha256.New, []byte(s.Config.Secret))
|
||||
h.Write([]byte(str))
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
@@ -197,9 +228,13 @@ func (s *Storage) PutObject(ctx context.Context, key, filePath, contentType stri
|
||||
}
|
||||
dstPath := filepath.Join(localPath, key)
|
||||
if err := os.MkdirAll(filepath.Dir(dstPath), 0o755); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("create object dir: %w", err)
|
||||
}
|
||||
return os.Rename(filePath, dstPath)
|
||||
if err := os.Rename(filePath, dstPath); err != nil {
|
||||
return fmt.Errorf("move object file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
client, err := s.s3ClientForUse()
|
||||
@@ -210,14 +245,18 @@ func (s *Storage) PutObject(ctx context.Context, key, filePath, contentType stri
|
||||
if contentType != "" {
|
||||
opts.ContentType = contentType
|
||||
}
|
||||
_, err = client.FPutObject(ctx, s.Config.Bucket, key, filePath, opts)
|
||||
return err
|
||||
if _, err := client.FPutObject(ctx, s.Config.Bucket, key, filePath, opts); err != nil {
|
||||
return fmt.Errorf("upload object: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) Provider() string {
|
||||
if s.storageType() == "s3" {
|
||||
return "s3"
|
||||
}
|
||||
|
||||
return "local"
|
||||
}
|
||||
|
||||
@@ -225,6 +264,7 @@ func (s *Storage) Bucket() string {
|
||||
if s.storageType() == "s3" && s.Config.Bucket != "" {
|
||||
return s.Config.Bucket
|
||||
}
|
||||
|
||||
return "default"
|
||||
}
|
||||
|
||||
@@ -236,6 +276,7 @@ func (s *Storage) storageType() string {
|
||||
if typ == "" {
|
||||
return "local"
|
||||
}
|
||||
|
||||
return typ
|
||||
}
|
||||
|
||||
@@ -244,13 +285,13 @@ func (s *Storage) s3ClientForUse() (*minio.Client, error) {
|
||||
return s.s3Client, nil
|
||||
}
|
||||
if strings.TrimSpace(s.Config.Endpoint) == "" {
|
||||
return nil, fmt.Errorf("storage endpoint is required")
|
||||
return nil, errStorageEndpointRequired
|
||||
}
|
||||
if strings.TrimSpace(s.Config.AccessKey) == "" || strings.TrimSpace(s.Config.SecretKey) == "" {
|
||||
return nil, fmt.Errorf("storage access key or secret key is required")
|
||||
return nil, errStorageAccessKeyRequired
|
||||
}
|
||||
if strings.TrimSpace(s.Config.Bucket) == "" {
|
||||
return nil, fmt.Errorf("storage bucket is required")
|
||||
return nil, errStorageBucketRequired
|
||||
}
|
||||
|
||||
endpoint, secure, err := parseEndpoint(s.Config.Endpoint)
|
||||
@@ -274,6 +315,7 @@ func (s *Storage) s3ClientForUse() (*minio.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
s.s3Client = client
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -281,12 +323,14 @@ func parseEndpoint(endpoint string) (string, bool, error) {
|
||||
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", false, fmt.Errorf("parse endpoint: %w", err)
|
||||
}
|
||||
if u.Host == "" {
|
||||
return "", false, fmt.Errorf("invalid endpoint")
|
||||
return "", false, errStorageInvalidEndpoint
|
||||
}
|
||||
|
||||
return u.Host, u.Scheme == "https", nil
|
||||
}
|
||||
|
||||
return endpoint, false, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user