优化结构

This commit is contained in:
Yvan
2026-08-28 09:19:54 +08:00
parent 03a4131f6e
commit 279692375b
57 changed files with 778 additions and 799 deletions
+12
View File
@@ -135,14 +135,26 @@ type mediaUploadRepo struct {
chunks []*UploadChunk
claimed bool
failed bool
err error
}
func (r *mediaUploadRepo) FindUploadSession(context.Context, uint) (*UploadSession, error) {
if r.err != nil {
return nil, r.err
}
if r.session == nil {
return nil, ErrUploadSessionNotFound
}
return r.session, nil
}
func TestSaveChunkPreservesUploadRepositoryErrors(t *testing.T) {
want := errors.New("database unavailable")
uc := NewMediaUsecase(&mediaUploadRepo{err: want}, &mediaLimitStorage{}, mediaLimitSettings{max: 10})
if err := uc.SaveChunk(context.Background(), 1, 7, 0, md5Text("abc"), strings.NewReader("abc")); !errors.Is(err, want) {
t.Fatalf("SaveChunk() error = %v, want %v", err, want)
}
}
func (r *mediaUploadRepo) ClaimUploadSession(context.Context, uint) (bool, error) {
return r.claimed, nil
}
+17 -6
View File
@@ -58,6 +58,17 @@ func expectedChunkSize(session *UploadSession, index int) (int64, error) {
return last, nil
}
func (uc *MediaUsecase) uploadSession(ctx context.Context, id uint) (*UploadSession, error) {
session, err := uc.FindUploadSession(ctx, id)
if errors.Is(err, ErrUploadSessionNotFound) {
return nil, fmt.Errorf("上传会话不存在: %w", ErrUploadSessionNotFound)
}
if err != nil {
return nil, err
}
return session, nil
}
func (uc *MediaUsecase) chunkPrefix(uploadID uint) string {
directory := "uploads/chunks"
if uc.settings != nil {
@@ -117,9 +128,9 @@ func (uc *MediaUsecase) InitUpload(ctx context.Context, userID uint, name, hash
return session, nil, indices, nil
}
func (uc *MediaUsecase) SaveChunk(ctx context.Context, userID, uploadID uint, index int, expected string, reader io.Reader) error {
session, err := uc.FindUploadSession(ctx, uploadID)
session, err := uc.uploadSession(ctx, uploadID)
if err != nil {
return errors.New("上传会话不存在")
return err
}
if session.UserID != userID {
return errors.New("无权操作该上传")
@@ -173,9 +184,9 @@ func (uc *MediaUsecase) SaveChunk(ctx context.Context, userID, uploadID uint, in
return nil
}
func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uint, mime string) (*MediaFile, error) {
session, err := uc.FindUploadSession(ctx, uploadID)
session, err := uc.uploadSession(ctx, uploadID)
if err != nil {
return nil, errors.New("上传会话不存在")
return nil, err
}
if session.UserID != userID {
return nil, errors.New("无权操作该上传")
@@ -241,9 +252,9 @@ func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uin
return media, nil
}
func (uc *MediaUsecase) CancelUpload(ctx context.Context, userID, uploadID uint) error {
session, err := uc.FindUploadSession(ctx, uploadID)
session, err := uc.uploadSession(ctx, uploadID)
if err != nil {
return errors.New("上传会话不存在")
return err
}
if session.UserID != userID {
return errors.New("无权操作该上传")
+30
View File
@@ -0,0 +1,30 @@
package system
import "context"
type ServerDiskInfo struct {
MountPoint string
UsedMB uint64
UsedGB uint64
TotalMB uint64
TotalGB uint64
UsedPercent int
}
type ServerInfo struct {
GOOS string
NumCPU int
Compiler string
GoVersion string
NumGoroutine int
CPUs []float64
Cores int
UsedMB uint64
TotalMB uint64
UsedPercent int
Disk []ServerDiskInfo
}
type SystemInfoProvider interface {
Collect(context.Context, []string) (*ServerInfo, error)
}
+10 -2
View File
@@ -50,10 +50,11 @@ type TaskReloader interface {
type SystemConfigUsecase struct {
repo InitializationRepo
tasks TaskReloader
info SystemInfoProvider
}
func NewSystemConfigUsecase(repo InitializationRepo, tasks TaskReloader) *SystemConfigUsecase {
return &SystemConfigUsecase{repo: repo, tasks: tasks}
func NewSystemConfigUsecase(repo InitializationRepo, tasks TaskReloader, info SystemInfoProvider) *SystemConfigUsecase {
return &SystemConfigUsecase{repo: repo, tasks: tasks, info: info}
}
func (uc *SystemConfigUsecase) IsInitialized(ctx context.Context) (bool, error) {
@@ -128,3 +129,10 @@ func (uc *SystemConfigUsecase) DiskMountPoints() []string {
}
return uc.repo.DiskMountPoints()
}
func (uc *SystemConfigUsecase) ServerInfo(ctx context.Context) (*ServerInfo, error) {
if uc == nil || uc.repo == nil || uc.info == nil {
return nil, errors.New("系统信息服务未就绪")
}
return uc.info.Collect(ctx, uc.repo.DiskMountPoints())
}
+3 -3
View File
@@ -37,7 +37,7 @@ func (r taskReloaderStub) Reload(context.Context) error { return r.err }
func TestInitializeClassifiesPostCommitTaskReloadFailure(t *testing.T) {
reloadErr := errors.New("scheduler unavailable")
uc := NewSystemConfigUsecase(&initializationRepoStub{}, taskReloaderStub{err: reloadErr})
uc := NewSystemConfigUsecase(&initializationRepoStub{}, taskReloaderStub{err: reloadErr}, nil)
err := uc.Initialize(context.Background(), &DatabaseConfig{})
if !errors.Is(err, ErrTaskRuntimeReload) || !errors.Is(err, reloadErr) {
t.Fatalf("Initialize() error = %v", err)
@@ -45,7 +45,7 @@ func TestInitializeClassifiesPostCommitTaskReloadFailure(t *testing.T) {
}
func TestReloadConfigClassifiesMissingTaskRuntime(t *testing.T) {
uc := NewSystemConfigUsecase(&initializationRepoStub{}, nil)
uc := NewSystemConfigUsecase(&initializationRepoStub{}, nil, nil)
err := uc.ReloadConfig(context.Background())
if !errors.Is(err, ErrTaskRuntimeReload) {
t.Fatalf("ReloadConfig() error = %v", err)
@@ -53,7 +53,7 @@ func TestReloadConfigClassifiesMissingTaskRuntime(t *testing.T) {
}
func TestSystemConfigUsecaseRejectsMissingRepository(t *testing.T) {
uc := NewSystemConfigUsecase(nil, nil)
uc := NewSystemConfigUsecase(nil, nil, nil)
if _, err := uc.IsInitialized(context.Background()); err == nil {
t.Fatal("IsInitialized accepted a missing repository")
}