52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"godemo/file/file"
|
|
"godemo/file/internal/svc"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type DeleteLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
|
|
return &DeleteLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 删除文件
|
|
func (l *DeleteLogic) Delete(in *file.DeleteRequest) (*file.DeleteResponse, error) {
|
|
// 假设 fileId 格式为: "bucketName/objectName"
|
|
parts := strings.SplitN(in.FileId, "/", 2)
|
|
if len(parts) != 2 {
|
|
return nil, status.Error(codes.InvalidArgument, "invalid fileId format")
|
|
}
|
|
|
|
bucketName := parts[0]
|
|
objectName := parts[1]
|
|
|
|
// 删除对象
|
|
err := l.svcCtx.MinioClient.RemoveObject(l.ctx, bucketName, objectName, minio.RemoveObjectOptions{})
|
|
if err != nil {
|
|
// return nil, err
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
return &file.DeleteResponse{
|
|
Success: true,
|
|
}, nil
|
|
}
|