// Code scaffolded by goctl. Safe to edit. // goctl 1.9.2 package file import ( "context" "fmt" "strings" "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 GetFileListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // 获取文件列表 func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic { return &GetFileListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetFileListLogic) GetFileList(req *types.FileListRequest) (resp *types.FileListResponse, 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) // Query file list with pagination and permission filtering page := int64(req.Page) pageSize := int64(req.PageSize) if page <= 0 { page = 1 } if pageSize <= 0 { pageSize = 20 } files, total, err := model.FileFindList( l.ctx, l.svcCtx.DB, page, pageSize, req.Keyword, req.Category, req.MimeType, userId, userRole, ) if err != nil { return nil, fmt.Errorf("failed to query file list: %v", err) } // Convert to response list := make([]types.FileInfo, 0, len(files)) for _, f := range files { url := l.buildFileURL(f.Id, f.Key) list = append(list, types.FileInfo{ Id: f.Id, Name: f.Name, Key: f.Key, Size: f.Size, MimeType: f.MimeType, Category: f.Category, IsPublic: f.IsPublic, UserId: f.UserId, StorageType: f.StorageType, Url: url, CreatedAt: f.CreatedAt.Format("2006-01-02 15:04:05"), UpdatedAt: f.UpdatedAt.Format("2006-01-02 15:04:05"), }) } resp = &types.FileListResponse{ Total: total, List: list, } return resp, nil } // buildFileURL generates the appropriate URL for the file func (l *GetFileListLogic) buildFileURL(fileId int64, key string) string { rawURL, err := l.svcCtx.Storage.GetURL(l.ctx, key) if err != nil { l.Errorf("failed to get URL for file %d: %v", fileId, err) return "" } // For local storage, return API endpoint URL if strings.HasPrefix(rawURL, "local://") { return fmt.Sprintf("/api/v1/file/%d/url", fileId) } return rawURL }