file_system/internal/api/handlers/query_handlers.go
2025-12-19 16:32:20 +08:00

93 lines
2.0 KiB
Go

package handlers
import (
"context"
"file-system/internal/common"
"file-sys
"file-system/internal/domain/repository"
"io"
"file-system/internal/common"
)
// Queries & Commands
type ListFilesQuery struct {
BucketName string
Prefix string
MaxKeys int32
Token *string
}
type GetFilePreviewQuery struct {
BucketName string
ObjectKey string
Expiry time.Duration
}
type InitMultipartCommand struct {
BucketName string
ObjectKey string
}
type UploadPartCommand struct {
BucketName string
ObjectKey string
UploadId string
PartNumber int32
Data io.Reader
}
type CompleteMultipartCommand struct {
BucketName string
ObjectKey string
UploadId string
Parts []common.Part
}
// Handlers
type ListFilesHandler struct {
Repo repository.FileRepository
}
func NewListFilesHandler(repo repository.FileRepository) *ListFilesHandler {
return &ListFilesHandler{Repo: repo}
}
func (h *ListFilesHandler) Handle(ctx context.Context, q ListFilesQuery) (*repository.ListFilesResult, error) {
return h.Repo.ListObjectsV2(ctx, q.BucketName, q.Prefix, q.MaxKeys, q.Token)
}
type GetFilePreviewHandler struct {
Repo repository.FileRepository
}
func NewGetFilePreviewHandler(repo repository.FileRepository) *GetFilePreviewHandler {
return &GetFilePreviewHandler{Repo: repo}
}
func (h *GetFilePreviewHandler) Handle(ctx context.Context, q GetFilePreviewQuery) (string, error) {
return h.Repo.GeneratePresignedURL(ctx, q.BucketName, q.ObjectKey, q.Expiry)
}
// DeleteFileCommand 删除文件命令
type DeleteFileCommand struct {
BucketName string
ObjectKey string
}
type DeleteFileHandler struct {
Repo repository.FileRepository
}
func NewDeleteFileHandler(repo repository.FileRepository) *DeleteFileHandler {
return &DeleteFileHandler{Repo: repo}
}
func (h *DeleteFileHandler) Handle(ctx context.Context, cmd DeleteFileCommand) (string, error) {
err := h.Repo.DeleteFile(ctx, cmd.BucketName, cmd.ObjectKey)
if err != nil {
return "", err
}
return "File deleted successfully", nil
}