feat: complete Kra admin backend migration
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
@@ -20,7 +21,22 @@ type fileStorage struct {
|
||||
urlPrefix string
|
||||
}
|
||||
|
||||
func NewFileStorage(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
type reloadableStorage struct {
|
||||
mu sync.RWMutex
|
||||
current biz.FileStorage
|
||||
}
|
||||
|
||||
func NewFileStorage(data *Data) (biz.FileStorage, error) {
|
||||
storage, err := buildFileStorage(data.admin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wrapper := &reloadableStorage{current: storage}
|
||||
data.storage = wrapper
|
||||
return wrapper, nil
|
||||
}
|
||||
|
||||
func buildFileStorage(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
storageType := "local"
|
||||
if config != nil && config.Storage != nil && config.Storage.Type != "" {
|
||||
storageType = strings.ToLower(config.Storage.Type)
|
||||
@@ -32,15 +48,15 @@ func NewFileStorage(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
var object *conf.AdminBackend_ObjectStore
|
||||
switch storageType {
|
||||
case "aliyun-oss":
|
||||
object = config.Storage.AliyunOss
|
||||
return newAliyunStorage(config.Storage.AliyunOss)
|
||||
case "huawei-obs":
|
||||
object = config.Storage.HuaweiObs
|
||||
return newHuaweiStorage(config.Storage.HuaweiObs)
|
||||
case "tencent-cos":
|
||||
object = config.Storage.TencentCos
|
||||
return newTencentStorage(config.Storage.TencentCos)
|
||||
case "aws-s3":
|
||||
object = config.Storage.AwsS3
|
||||
return newAWSStorage(storageType, config.Storage.AwsS3)
|
||||
case "cloudflare-r2":
|
||||
object = config.Storage.CloudflareR2
|
||||
return newAWSStorage(storageType, config.Storage.CloudflareR2)
|
||||
case "minio":
|
||||
object = config.Storage.Minio
|
||||
default:
|
||||
@@ -64,6 +80,42 @@ func NewFileStorage(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
return &fileStorage{root: root, urlPrefix: "/" + strings.Trim(prefix, "/")}, nil
|
||||
}
|
||||
|
||||
func (s *reloadableStorage) replace(storage biz.FileStorage) {
|
||||
s.mu.Lock()
|
||||
s.current = storage
|
||||
s.mu.Unlock()
|
||||
}
|
||||
func (s *reloadableStorage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Put(ctx, name, reader)
|
||||
}
|
||||
func (s *reloadableStorage) Open(ctx context.Context, name string) (io.ReadCloser, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Open(ctx, name)
|
||||
}
|
||||
func (s *reloadableStorage) Delete(ctx context.Context, name string) error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Delete(ctx, name)
|
||||
}
|
||||
func (s *reloadableStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Compose(ctx, names, destination)
|
||||
}
|
||||
func (s *reloadableStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.DeletePrefix(ctx, prefix)
|
||||
}
|
||||
func (s *reloadableStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.List(ctx, prefix, cursor, limit)
|
||||
}
|
||||
|
||||
func composeFiles(ctx context.Context, storage biz.FileStorage, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
reader, writer := io.Pipe()
|
||||
hash := md5.New()
|
||||
|
||||
Reference in New Issue
Block a user