优化结构
This commit is contained in:
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
)
|
||||
|
||||
type aliyunStorage struct {
|
||||
@@ -17,7 +18,7 @@ type aliyunStorage struct {
|
||||
baseURL, prefix string
|
||||
}
|
||||
|
||||
func newAliyunStorage(config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newAliyunStorage(config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Endpoint == "" || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("aliyun-oss storage configuration is incomplete")
|
||||
}
|
||||
@@ -40,12 +41,12 @@ func (s *aliyunStorage) key(name string) string {
|
||||
func (s *aliyunStorage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *aliyunStorage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *aliyunStorage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
url := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: url, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: url, Size: size}
|
||||
}
|
||||
func (s *aliyunStorage) Put(_ context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *aliyunStorage) Put(_ context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
if err := s.bucket.PutObject(key, reader); err != nil {
|
||||
return nil, err
|
||||
@@ -64,7 +65,7 @@ func (s *aliyunStorage) Open(_ context.Context, name string) (io.ReadCloser, err
|
||||
func (s *aliyunStorage) Delete(_ context.Context, name string) error {
|
||||
return s.bucket.DeleteObject(s.key(name))
|
||||
}
|
||||
func (s *aliyunStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *aliyunStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *aliyunStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -85,7 +86,7 @@ func (s *aliyunStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
cursor = next
|
||||
}
|
||||
}
|
||||
func (s *aliyunStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *aliyunStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -93,7 +94,7 @@ func (s *aliyunStorage) List(_ context.Context, prefix, cursor string, limit int
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
items := make([]*biz.StoredFile, 0, len(result.Objects))
|
||||
items := make([]*system.StoredFile, 0, len(result.Objects))
|
||||
for _, object := range result.Objects {
|
||||
item := s.file(object.Key, object.Size)
|
||||
item.LastModified = object.LastModified
|
||||
|
||||
@@ -4,16 +4,17 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
type awsStorage struct {
|
||||
@@ -21,7 +22,7 @@ type awsStorage struct {
|
||||
bucket, baseURL, prefix string
|
||||
}
|
||||
|
||||
func newAWSStorage(provider string, config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newAWSStorage(provider string, config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("%s storage configuration is incomplete", provider)
|
||||
}
|
||||
@@ -68,12 +69,12 @@ func (s *awsStorage) key(name string) string {
|
||||
func (s *awsStorage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *awsStorage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *awsStorage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
url := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: url, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: url, Size: size}
|
||||
}
|
||||
func (s *awsStorage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *awsStorage) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
result, err := manager.NewUploader(s.client).Upload(ctx, &s3.PutObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(key), Body: reader})
|
||||
if err != nil {
|
||||
@@ -101,7 +102,7 @@ func (s *awsStorage) Delete(ctx context.Context, name string) error {
|
||||
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(s.key(name))})
|
||||
return err
|
||||
}
|
||||
func (s *awsStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *awsStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *awsStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -120,7 +121,7 @@ func (s *awsStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *awsStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *awsStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func (s *awsStorage) List(ctx context.Context, prefix, cursor string, limit int)
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
items := make([]*biz.StoredFile, 0, len(result.Contents))
|
||||
items := make([]*system.StoredFile, 0, len(result.Contents))
|
||||
for _, object := range result.Contents {
|
||||
if object.Key == nil {
|
||||
continue
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
|
||||
)
|
||||
|
||||
type huaweiStorage struct {
|
||||
@@ -17,7 +18,7 @@ type huaweiStorage struct {
|
||||
bucket, baseURL, prefix string
|
||||
}
|
||||
|
||||
func newHuaweiStorage(config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newHuaweiStorage(config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Endpoint == "" || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("huawei-obs storage configuration is incomplete")
|
||||
}
|
||||
@@ -36,12 +37,12 @@ func (s *huaweiStorage) key(name string) string {
|
||||
func (s *huaweiStorage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *huaweiStorage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *huaweiStorage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
fileURL := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
}
|
||||
func (s *huaweiStorage) Put(_ context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *huaweiStorage) Put(_ context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
if _, err := s.client.PutObject(&obs.PutObjectInput{PutObjectBasicInput: obs.PutObjectBasicInput{ObjectOperationInput: obs.ObjectOperationInput{Bucket: s.bucket, Key: key}}, Body: reader}); err != nil {
|
||||
return nil, err
|
||||
@@ -63,7 +64,7 @@ func (s *huaweiStorage) Delete(_ context.Context, name string) error {
|
||||
_, err := s.client.DeleteObject(&obs.DeleteObjectInput{Bucket: s.bucket, Key: s.key(name)})
|
||||
return err
|
||||
}
|
||||
func (s *huaweiStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *huaweiStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *huaweiStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -84,7 +85,7 @@ func (s *huaweiStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
cursor = next
|
||||
}
|
||||
}
|
||||
func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -92,7 +93,7 @@ func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
items := make([]*biz.StoredFile, 0, len(result.Contents))
|
||||
items := make([]*system.StoredFile, 0, len(result.Contents))
|
||||
for _, object := range result.Contents {
|
||||
item := s.file(object.Key, object.Size)
|
||||
item.LastModified = object.LastModified
|
||||
|
||||
@@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
@@ -19,7 +19,7 @@ type fileStorage struct {
|
||||
urlPrefix string
|
||||
}
|
||||
|
||||
func New(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
func New(config *conf.AdminBackend) (system.FileStorage, error) {
|
||||
storageType := "local"
|
||||
if config != nil && config.Storage != nil && config.Storage.Type != "" {
|
||||
storageType = strings.ToLower(config.Storage.Type)
|
||||
@@ -63,8 +63,8 @@ func New(config *conf.AdminBackend) (biz.FileStorage, error) {
|
||||
return &fileStorage{root: root, urlPrefix: "/" + strings.Trim(prefix, "/")}, nil
|
||||
}
|
||||
|
||||
func composeFiles(ctx context.Context, storage biz.FileStorage, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
var stored *biz.StoredFile
|
||||
func composeFiles(ctx context.Context, storage system.FileStorage, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
var stored *system.StoredFile
|
||||
hash, err := composeStreams(ctx, names, storage.Open, func(ctx context.Context, destination string, reader io.Reader) error {
|
||||
var putErr error
|
||||
stored, putErr = storage.Put(ctx, destination, reader)
|
||||
@@ -87,7 +87,7 @@ func (s *fileStorage) resolve(name string) (string, error) {
|
||||
return filepath.Join(s.root, clean), nil
|
||||
}
|
||||
|
||||
func (s *fileStorage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *fileStorage) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
path, err := s.resolve(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -107,7 +107,7 @@ func (s *fileStorage) Put(ctx context.Context, name string, reader io.Reader) (*
|
||||
if closeErr != nil {
|
||||
return nil, closeErr
|
||||
}
|
||||
return &biz.StoredFile{Name: filepath.Base(name), Path: name, URL: s.urlPrefix + "/" + strings.TrimPrefix(filepath.ToSlash(name), "/"), Size: size}, nil
|
||||
return &system.StoredFile{Name: filepath.Base(name), Path: name, URL: s.urlPrefix + "/" + strings.TrimPrefix(filepath.ToSlash(name), "/"), Size: size}, nil
|
||||
}
|
||||
|
||||
func (s *fileStorage) Open(ctx context.Context, name string) (io.ReadCloser, error) {
|
||||
@@ -134,7 +134,7 @@ func (s *fileStorage) Delete(ctx context.Context, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *fileStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *fileStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *fileStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -151,7 +151,7 @@ func (s *fileStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *fileStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *fileStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func (s *fileStorage) List(ctx context.Context, prefix, cursor string, limit int
|
||||
return nil, "", false, err
|
||||
}
|
||||
walkRoot := filepath.Dir(root)
|
||||
items := []*biz.StoredFile{}
|
||||
items := []*system.StoredFile{}
|
||||
err = filepath.WalkDir(walkRoot, func(path string, entry os.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
if os.IsNotExist(walkErr) {
|
||||
@@ -183,7 +183,7 @@ func (s *fileStorage) List(ctx context.Context, prefix, cursor string, limit int
|
||||
if infoErr != nil {
|
||||
return infoErr
|
||||
}
|
||||
items = append(items, &biz.StoredFile{Name: entry.Name(), Path: relative, URL: s.urlPrefix + "/" + relative, Size: info.Size(), LastModified: info.ModTime(), ContentType: mime.TypeByExtension(filepath.Ext(entry.Name()))})
|
||||
items = append(items, &system.StoredFile{Name: entry.Name(), Path: relative, URL: s.urlPrefix + "/" + relative, Size: info.Size(), LastModified: info.ModTime(), ContentType: mime.TypeByExtension(filepath.Ext(entry.Name()))})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -4,16 +4,17 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
||||
qstorage "github.com/qiniu/go-sdk/v7/storage"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
type qiniuStorage struct {
|
||||
@@ -24,7 +25,7 @@ type qiniuStorage struct {
|
||||
token string
|
||||
}
|
||||
|
||||
func newQiniuStorage(config *conf.AdminBackend_Qiniu) (biz.FileStorage, error) {
|
||||
func newQiniuStorage(config *conf.AdminBackend_Qiniu) (system.FileStorage, error) {
|
||||
if config == nil || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("qiniu storage configuration is incomplete")
|
||||
}
|
||||
@@ -46,10 +47,10 @@ func newQiniuStorage(config *conf.AdminBackend_Qiniu) (biz.FileStorage, error) {
|
||||
token := policy.UploadToken(mac)
|
||||
return &qiniuStorage{config: config, upload: qstorage.NewFormUploader(&cfg), manager: qstorage.NewBucketManager(mac, &cfg), mac: mac, token: token}, nil
|
||||
}
|
||||
func (s *qiniuStorage) file(key string, size int64) *biz.StoredFile {
|
||||
return &biz.StoredFile{Name: path.Base(key), Path: key, URL: strings.TrimSuffix(s.config.BaseUrl, "/") + "/" + key, Size: size}
|
||||
func (s *qiniuStorage) file(key string, size int64) *system.StoredFile {
|
||||
return &system.StoredFile{Name: path.Base(key), Path: key, URL: strings.TrimSuffix(s.config.BaseUrl, "/") + "/" + key, Size: size}
|
||||
}
|
||||
func (s *qiniuStorage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *qiniuStorage) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
temporary, err := os.CreateTemp("", "kra-qiniu-upload-*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -91,7 +92,7 @@ func (s *qiniuStorage) Open(ctx context.Context, name string) (io.ReadCloser, er
|
||||
func (s *qiniuStorage) Delete(ctx context.Context, name string) error {
|
||||
return s.manager.Delete(s.config.Bucket, strings.TrimPrefix(name, "/"))
|
||||
}
|
||||
func (s *qiniuStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *qiniuStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *qiniuStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -110,7 +111,7 @@ func (s *qiniuStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *qiniuStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *qiniuStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -118,7 +119,7 @@ func (s *qiniuStorage) List(ctx context.Context, prefix, cursor string, limit in
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
out := make([]*biz.StoredFile, 0, len(entries))
|
||||
out := make([]*system.StoredFile, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
item := s.file(entry.Key, entry.Fsize)
|
||||
item.LastModified = time.Unix(0, entry.PutTime*100)
|
||||
|
||||
@@ -3,15 +3,15 @@ package storage
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"sync"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
type Reloadable struct {
|
||||
mu sync.RWMutex
|
||||
current biz.FileStorage
|
||||
current system.FileStorage
|
||||
}
|
||||
|
||||
func NewFileStorage(runtime *conf.Runtime) (*Reloadable, error) {
|
||||
@@ -30,13 +30,13 @@ func NewReloadable(config *conf.AdminBackend) (*Reloadable, error) {
|
||||
return &Reloadable{current: current}, nil
|
||||
}
|
||||
|
||||
func (s *Reloadable) Replace(current biz.FileStorage) {
|
||||
func (s *Reloadable) Replace(current system.FileStorage) {
|
||||
s.mu.Lock()
|
||||
s.current = current
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Put(ctx, name, reader)
|
||||
@@ -51,7 +51,7 @@ func (s *Reloadable) Delete(ctx context.Context, name string) error {
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Delete(ctx, name)
|
||||
}
|
||||
func (s *Reloadable) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *Reloadable) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Compose(ctx, names, destination)
|
||||
@@ -61,7 +61,7 @@ func (s *Reloadable) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.DeletePrefix(ctx, prefix)
|
||||
}
|
||||
func (s *Reloadable) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *Reloadable) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.List(ctx, prefix, cursor, limit)
|
||||
|
||||
@@ -4,14 +4,15 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
type s3Storage struct {
|
||||
@@ -19,7 +20,7 @@ type s3Storage struct {
|
||||
bucket, baseURL, prefix string
|
||||
}
|
||||
|
||||
func newS3Storage(provider string, config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newS3Storage(provider string, config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("%s storage configuration is incomplete", provider)
|
||||
}
|
||||
@@ -66,12 +67,12 @@ func (s *s3Storage) key(name string) string {
|
||||
func (s *s3Storage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *s3Storage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *s3Storage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
rawURL := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: rawURL, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: rawURL, Size: size}
|
||||
}
|
||||
func (s *s3Storage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *s3Storage) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
info, err := s.client.PutObject(ctx, s.bucket, key, reader, -1, minio.PutObjectOptions{})
|
||||
if err != nil {
|
||||
@@ -93,7 +94,7 @@ func (s *s3Storage) Open(ctx context.Context, name string) (io.ReadCloser, error
|
||||
func (s *s3Storage) Delete(ctx context.Context, name string) error {
|
||||
return s.client.RemoveObject(ctx, s.bucket, s.key(name), minio.RemoveObjectOptions{})
|
||||
}
|
||||
func (s *s3Storage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *s3Storage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *s3Storage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -108,12 +109,12 @@ func (s *s3Storage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *s3Storage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *s3Storage) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
items := s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: s.key(prefix), Recursive: true, StartAfter: s.key(cursor)})
|
||||
out := make([]*biz.StoredFile, 0, limit+1)
|
||||
out := make([]*system.StoredFile, 0, limit+1)
|
||||
for item := range items {
|
||||
if item.Err != nil {
|
||||
return nil, "", false, item.Err
|
||||
|
||||
@@ -4,15 +4,16 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
cos "github.com/tencentyun/cos-go-sdk-v5"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
|
||||
cos "github.com/tencentyun/cos-go-sdk-v5"
|
||||
)
|
||||
|
||||
type tencentStorage struct {
|
||||
@@ -20,7 +21,7 @@ type tencentStorage struct {
|
||||
baseURL, prefix string
|
||||
}
|
||||
|
||||
func newTencentStorage(config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newTencentStorage(config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Bucket == "" || config.Region == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("tencent-cos storage configuration is incomplete")
|
||||
}
|
||||
@@ -50,12 +51,12 @@ func (s *tencentStorage) key(name string) string {
|
||||
func (s *tencentStorage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *tencentStorage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *tencentStorage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
fileURL := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
}
|
||||
func (s *tencentStorage) Put(ctx context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *tencentStorage) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
if _, err := s.client.Object.Put(ctx, key, reader, nil); err != nil {
|
||||
return nil, err
|
||||
@@ -77,7 +78,7 @@ func (s *tencentStorage) Delete(ctx context.Context, name string) error {
|
||||
_, err := s.client.Object.Delete(ctx, s.key(name))
|
||||
return err
|
||||
}
|
||||
func (s *tencentStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *tencentStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *tencentStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -98,7 +99,7 @@ func (s *tencentStorage) DeletePrefix(ctx context.Context, prefix string) error
|
||||
cursor = next
|
||||
}
|
||||
}
|
||||
func (s *tencentStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *tencentStorage) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -106,7 +107,7 @@ func (s *tencentStorage) List(ctx context.Context, prefix, cursor string, limit
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
items := make([]*biz.StoredFile, 0, len(result.Contents))
|
||||
items := make([]*system.StoredFile, 0, len(result.Contents))
|
||||
for _, object := range result.Contents {
|
||||
item := s.file(object.Key, object.Size)
|
||||
for _, layout := range []string{time.RFC3339, "2006-01-02T15:04:05.000Z"} {
|
||||
|
||||
Reference in New Issue
Block a user