You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

72 lines
1.8 KiB

// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package file
import (
"context"
"fmt"
"github.com/youruser/base/internal/svc"
"github.com/youruser/base/internal/types"
"github.com/youruser/base/model"
"github.com/zeromicro/go-zero/core/logx"
)
type GetFileUrlLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取文件访问URL
func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileUrlLogic {
return &GetFileUrlLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetFileUrlLogic) GetFileUrl(req *types.GetFileRequest) (resp *types.FileUrlResponse, err error) {
// Get userId and role from context
userId, _ := l.ctx.Value("userId").(int64)
if userId == 0 {
if num, ok := l.ctx.Value("userId").(interface{ Int64() (int64, error) }); ok {
userId, _ = num.Int64()
}
}
userRole, _ := l.ctx.Value("role").(string)
// Find file by ID
fileRecord, err := model.FileFindOne(l.ctx, l.svcCtx.DB, req.Id)
if err != nil {
if err == model.ErrNotFound {
return nil, fmt.Errorf("file not found")
}
return nil, fmt.Errorf("failed to query file: %v", err)
}
// Permission check: non-admin can only access own files + public files
if userRole != model.RoleAdmin && userRole != model.RoleSuperAdmin {
if fileRecord.UserId != userId && !fileRecord.IsPublic {
return nil, fmt.Errorf("permission denied")
}
}
// Get URL from storage backend
// For local storage, this returns "local://" + key
// The handler will detect the "local://" prefix and serve the file directly
// For OSS/MinIO, this returns a signed URL
url, err := l.svcCtx.Storage.GetURL(l.ctx, fileRecord.Key)
if err != nil {
return nil, fmt.Errorf("failed to get file URL: %v", err)
}
resp = &types.FileUrlResponse{
Url: url,
}
return resp, nil
}