Files
ocean/file/internal/logic/getfileurllogic.go
2025-05-22 19:39:08 +08:00

58 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}