58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"net/url"
|
||
"strings"
|
||
"time"
|
||
|
||
"godemo/file/file"
|
||
"godemo/file/internal/svc"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"google.golang.org/grpc/codes"
|
||
"google.golang.org/grpc/status"
|
||
)
|
||
|
||
type GetFileUrlLogic struct {
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
logx.Logger
|
||
}
|
||
|
||
func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileUrlLogic {
|
||
return &GetFileUrlLogic{
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
Logger: logx.WithContext(ctx),
|
||
}
|
||
}
|
||
|
||
// 获取文件访问链接(带签名,防盗链)
|
||
func (l *GetFileUrlLogic) GetFileUrl(in *file.GetFileUrlRequest) (*file.GetFileUrlResponse, 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]
|
||
|
||
// 生成预签名 URL,有效期 1 小时
|
||
presignedUrl, err := l.svcCtx.MinioClient.PresignedGetObject(
|
||
l.ctx,
|
||
bucketName,
|
||
objectName,
|
||
time.Hour,
|
||
url.Values{},
|
||
)
|
||
if err != nil {
|
||
return nil, status.Error(codes.Internal, err.Error())
|
||
}
|
||
|
||
return &file.GetFileUrlResponse{
|
||
Url: presignedUrl.String(),
|
||
}, nil
|
||
}
|