56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type DownloadPresignedURLLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Get presigned URL for file download
|
|
func NewDownloadPresignedURLLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DownloadPresignedURLLogic {
|
|
return &DownloadPresignedURLLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *DownloadPresignedURLLogic) DownloadPresignedURL(req *types.DownloadPresignedURLReq) (resp *types.DownloadPresignedURLResp, err error) {
|
|
// Calculate expiration time
|
|
expiration := time.Duration(l.svcCtx.Config.S3.PresignedURLExpiration) * time.Second
|
|
expireAt := time.Now().Add(expiration).Unix()
|
|
|
|
// Create presigned client
|
|
presignClient := s3.NewPresignClient(l.svcCtx.S3Client)
|
|
|
|
// Generate presigned GET URL
|
|
getObjectInput := &s3.GetObjectInput{
|
|
Bucket: &l.svcCtx.Config.S3.Bucket,
|
|
Key: &req.File_key,
|
|
}
|
|
|
|
presignedReq, err := presignClient.PresignGetObject(l.ctx, getObjectInput, func(opts *s3.PresignOptions) {
|
|
opts.Expires = expiration
|
|
})
|
|
if err != nil {
|
|
l.Errorf("Failed to generate presigned URL: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &types.DownloadPresignedURLResp{
|
|
Url: presignedReq.URL,
|
|
Expire_at: expireAt,
|
|
}, nil
|
|
}
|