优化结构
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
|
||||
|
||||
Reference in New Issue
Block a user