优化结构

This commit is contained in:
Yvan
2026-08-22 12:13:16 +08:00
parent d987244d0e
commit b27a66e9d3
200 changed files with 1794 additions and 1715 deletions
+10 -10
View File
@@ -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 {