Go: add context (#17354)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-24 20:19:41 +08:00
committed by GitHub
parent 64dbc518aa
commit d9e359d481
25 changed files with 438 additions and 351 deletions

View File

@@ -108,7 +108,8 @@ func (h *FileHandler) ListFiles(c *gin.Context) {
desc = descStr != "false"
}
result, err := h.fileService.ListFiles(userID, parentID, page, pageSize, orderby, desc, keywords)
ctx := c.Request.Context()
result, err := h.fileService.ListFiles(ctx, userID, parentID, page, pageSize, orderby, desc, keywords)
if err != nil {
jsonInternalError(c, err)
return
@@ -133,8 +134,9 @@ func (h *FileHandler) GetRootFolder(c *gin.Context) {
}
userID := user.ID
ctx := c.Request.Context()
// Get root folder
rootFolder, err := h.fileService.GetRootFolder(userID)
rootFolder, err := h.fileService.GetRootFolder(ctx, userID)
if err != nil {
jsonInternalError(c, err)
return
@@ -167,8 +169,9 @@ func (h *FileHandler) GetParentFolder(c *gin.Context) {
return
}
ctx := c.Request.Context()
// Get parent folder with permission check
parentFolder, err := h.fileService.GetParentFolder(userID, fileID)
parentFolder, err := h.fileService.GetParentFolder(ctx, userID, fileID)
if err != nil {
jsonInternalError(c, err)
return
@@ -201,8 +204,9 @@ func (h *FileHandler) GetAllParentFolders(c *gin.Context) {
return
}
ctx := c.Request.Context()
// Get all parent folders with permission check
parentFolders, err := h.fileService.GetAllParentFolders(userID, fileID)
parentFolders, err := h.fileService.GetAllParentFolders(ctx, userID, fileID)
if err != nil {
jsonInternalError(c, err)
return
@@ -234,8 +238,9 @@ func (h *FileHandler) GetFileAncestors(c *gin.Context) {
return
}
ctx := c.Request.Context()
// Get all parent folders with permission check
parentFolders, err := h.fileService.GetAllParentFolders(userID, fileID)
parentFolders, err := h.fileService.GetAllParentFolders(ctx, userID, fileID)
if err != nil {
jsonInternalError(c, err)
return
@@ -271,6 +276,7 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
userID := user.ID
contentType := c.ContentType()
ctx := c.Request.Context()
if strings.Contains(contentType, "multipart/form-data") {
if err := c.Request.ParseMultipartForm(32 << 20); err != nil {
@@ -285,7 +291,7 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
}
parentID := c.PostForm("parent_id")
if parentID == "" {
rootFolder, err := h.fileService.GetRootFolder(userID)
rootFolder, err := h.fileService.GetRootFolder(ctx, userID)
if err != nil {
jsonInternalError(c, err)
return
@@ -326,7 +332,7 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
parentID := req.ParentID
if parentID == "" {
rootFolder, err := h.fileService.GetRootFolder(userID)
rootFolder, err := h.fileService.GetRootFolder(ctx, userID)
if err != nil {
jsonInternalError(c, err)
return
@@ -334,7 +340,7 @@ func (h *FileHandler) UploadFile(c *gin.Context) {
parentID = rootFolder["id"].(string)
}
result, err := h.fileService.CreateFolder(userID, req.Name, parentID, req.Type)
result, err := h.fileService.CreateFolder(ctx, userID, req.Name, parentID, req.Type)
if err != nil {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
@@ -461,8 +467,9 @@ func (h *FileHandler) Download(c *gin.Context) {
return
}
ctx := c.Request.Context()
// Get file metadata and check permission
file, err := h.fileService.GetFileContent(userID, fileID)
file, err := h.fileService.GetFileContent(ctx, userID, fileID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeUnauthorized, nil, err.Error())
return

View File

@@ -17,6 +17,7 @@
package handler
import (
"context"
"fmt"
"ragflow/internal/common"
"ragflow/internal/dao"
@@ -28,15 +29,15 @@ import (
// fileCommitService is the consumer-side interface for FileCommitHandler's service dependency.
type fileCommitService interface {
CreateCommit(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
ListCommits(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
GetCommit(commitID string) (*entity.FileCommit, error)
ListCommitFiles(commitID string) ([]*entity.FileCommitItem, error)
DiffCommits(fromID, toID string) ([]entity.DiffEntry, error)
GetUncommittedChanges(folderID string) ([]entity.DiffEntry, error)
GetCommitTree(commitID string) (map[string]interface{}, error)
GetCommitFileContent(folderID, commitID, fileID string) ([]byte, error)
GetFileVersionHistory(fileID string) ([]entity.VersionEntry, error)
CreateCommit(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
ListCommits(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
GetCommit(ctx context.Context, commitID string) (*entity.FileCommit, error)
ListCommitFiles(ctx context.Context, commitID string) ([]*entity.FileCommitItem, error)
DiffCommits(ctx context.Context, fromID, toID string) ([]entity.DiffEntry, error)
GetUncommittedChanges(ctx context.Context, folderID string) ([]entity.DiffEntry, error)
GetCommitTree(ctx context.Context, commitID string) (map[string]interface{}, error)
GetCommitFileContent(ctx context.Context, folderID, commitID, fileID string) ([]byte, error)
GetFileVersionHistory(ctx context.Context, fileID string) ([]entity.VersionEntry, error)
}
// FileCommitHandler file commit handler
@@ -57,10 +58,10 @@ func NewFileCommitHandler(commitService fileCommitService) *FileCommitHandler {
// ResolveFolderID resolves a resource ID (dataset/memory/skill) to its folder_id.
// entityType is the plural resource name (e.g. "datasets", "memories", "skills").
func (h *FileCommitHandler) ResolveFolderID(entityType, entityID string) (string, error) {
func (h *FileCommitHandler) ResolveFolderID(ctx context.Context, entityType, entityID string) (string, error) {
switch entityType {
case "datasets":
return h.resolveDatasetFolderID(entityID)
return h.resolveDatasetFolderID(ctx, entityID)
default:
return "", fmt.Errorf("unsupported entity type: %s", entityType)
}
@@ -80,7 +81,8 @@ func CommitFolderResolver(h *FileCommitHandler, entityType, urlParam string) gin
c.Abort()
return
}
folderID, err := h.ResolveFolderID(entityType, id)
ctx := c.Request.Context()
folderID, err := h.ResolveFolderID(ctx, entityType, id)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, fmt.Sprintf("%s folder not found", entityType))
c.Abort()
@@ -91,12 +93,15 @@ func CommitFolderResolver(h *FileCommitHandler, entityType, urlParam string) gin
}
}
func (h *FileCommitHandler) resolveDatasetFolderID(datasetID string) (string, error) {
func (h *FileCommitHandler) resolveDatasetFolderID(ctx context.Context, datasetID string) (string, error) {
kb, err := h.kbDAO.GetByID(datasetID)
if err != nil {
return "", err
}
files := h.fileDAO.Query(kb.Name, "", kb.TenantID)
files, err := h.fileDAO.Query(ctx, dao.DB, kb.Name, "", kb.TenantID)
if err != nil {
return "", err
}
for _, f := range files {
if f.SourceType == string(entity.FileSourceKnowledgebase) && f.Type == "folder" && f.TenantID == kb.TenantID {
return f.ID, nil
@@ -115,8 +120,6 @@ type CreateCommitRequest struct {
// @Summary Create Commit
// @Description Create a new commit with file changes for a workspace folder
// @Tags file_commit
// @Accept json
// @Produce json
// @Param folder_id path string true "workspace folder ID"
// @Param body body CreateCommitRequest true "commit request"
// @Success 200 {object} map[string]interface{}
@@ -140,7 +143,8 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {
return
}
commit, err := h.commitService.CreateCommit(folderID, user.ID, req.Message, req.Files)
ctx := c.Request.Context()
commit, err := h.commitService.CreateCommit(ctx, folderID, user.ID, req.Message, req.Files)
if err != nil {
jsonInternalError(c, err)
return
@@ -212,7 +216,8 @@ func (h *FileCommitHandler) ListCommits(c *gin.Context) {
desc = descStr != "false"
}
commits, total, err := h.commitService.ListCommits(folderID, page, pageSize, orderBy, desc)
ctx := c.Request.Context()
commits, total, err := h.commitService.ListCommits(ctx, folderID, page, pageSize, orderBy, desc)
if err != nil {
jsonInternalError(c, err)
return
@@ -267,7 +272,8 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -278,7 +284,7 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {
return
}
items, err := h.commitService.ListCommitFiles(commitID)
items, err := h.commitService.ListCommitFiles(ctx, commitID)
if err != nil {
items = []*entity.FileCommitItem{}
}
@@ -324,7 +330,8 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -334,7 +341,7 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {
return
}
items, err := h.commitService.ListCommitFiles(commitID)
items, err := h.commitService.ListCommitFiles(ctx, commitID)
if err != nil {
jsonInternalError(c, err)
return
@@ -369,12 +376,13 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {
return
}
fromCommit, err := h.commitService.GetCommit(fromID)
ctx := c.Request.Context()
fromCommit, err := h.commitService.GetCommit(ctx, fromID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
}
toCommit, err := h.commitService.GetCommit(toID)
toCommit, err := h.commitService.GetCommit(ctx, toID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -384,7 +392,7 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {
return
}
diff, err := h.commitService.DiffCommits(fromID, toID)
diff, err := h.commitService.DiffCommits(ctx, fromID, toID)
if err != nil {
jsonInternalError(c, err)
return
@@ -415,7 +423,8 @@ func (h *FileCommitHandler) GetUncommittedChanges(c *gin.Context) {
return
}
changes, err := h.commitService.GetUncommittedChanges(folderID)
ctx := c.Request.Context()
changes, err := h.commitService.GetUncommittedChanges(ctx, folderID)
if err != nil {
jsonInternalError(c, err)
return
@@ -448,7 +457,8 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -458,7 +468,7 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {
return
}
tree, err := h.commitService.GetCommitTree(commitID)
tree, err := h.commitService.GetCommitTree(ctx, commitID)
if err != nil {
jsonInternalError(c, err)
return
@@ -494,7 +504,8 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -504,7 +515,7 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {
return
}
content, err := h.commitService.GetCommitFileContent(folderID, commitID, fileID)
content, err := h.commitService.GetCommitFileContent(ctx, folderID, commitID, fileID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, err.Error())
return
@@ -535,7 +546,8 @@ func (h *FileCommitHandler) GetFileVersionHistory(c *gin.Context) {
return
}
versions, err := h.commitService.GetFileVersionHistory(fileID)
ctx := c.Request.Context()
versions, err := h.commitService.GetFileVersionHistory(ctx, fileID)
if err != nil {
jsonInternalError(c, err)
return

View File

@@ -17,6 +17,7 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -31,20 +32,20 @@ import (
// mockFileCommitSvc implements FileCommitServiceInterface for testing
type mockFileCommitSvc struct {
createCommitFn func(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
listCommitsFn func(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
getCommitFn func(commitID string) (*entity.FileCommit, error)
listCommitFilesFn func(commitID string) ([]*entity.FileCommitItem, error)
diffCommitsFn func(fromID, toID string) ([]entity.DiffEntry, error)
getUncommittedChangesFn func(folderID string) ([]entity.DiffEntry, error)
getCommitTreeFn func(commitID string) (map[string]interface{}, error)
getCommitFileContentFn func(folderID, commitID, fileID string) ([]byte, error)
getFileVersionHistoryFn func(fileID string) ([]entity.VersionEntry, error)
createCommitFn func(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
listCommitsFn func(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
getCommitFn func(ctx context.Context, commitID string) (*entity.FileCommit, error)
listCommitFilesFn func(ctx context.Context, commitID string) ([]*entity.FileCommitItem, error)
diffCommitsFn func(ctx context.Context, fromID, toID string) ([]entity.DiffEntry, error)
getUncommittedChangesFn func(ctx context.Context, folderID string) ([]entity.DiffEntry, error)
getCommitTreeFn func(ctx context.Context, commitID string) (map[string]interface{}, error)
getCommitFileContentFn func(ctx context.Context, folderID, commitID, fileID string) ([]byte, error)
getFileVersionHistoryFn func(ctx context.Context, fileID string) ([]entity.VersionEntry, error)
}
func (m *mockFileCommitSvc) CreateCommit(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
func (m *mockFileCommitSvc) CreateCommit(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
if m.createCommitFn != nil {
return m.createCommitFn(folderID, authorID, message, changes)
return m.createCommitFn(ctx, folderID, authorID, message, changes)
}
return &entity.FileCommit{
ID: "commit-1",
@@ -55,9 +56,9 @@ func (m *mockFileCommitSvc) CreateCommit(folderID, authorID, message string, cha
}, nil
}
func (m *mockFileCommitSvc) ListCommits(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
func (m *mockFileCommitSvc) ListCommits(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
if m.listCommitsFn != nil {
return m.listCommitsFn(folderID, page, pageSize, orderBy, desc)
return m.listCommitsFn(ctx, folderID, page, pageSize, orderBy, desc)
}
now := int64(1718200000000)
return []*entity.FileCommit{
@@ -66,59 +67,59 @@ func (m *mockFileCommitSvc) ListCommits(folderID string, page, pageSize int, ord
}, 2, nil
}
func (m *mockFileCommitSvc) GetCommit(commitID string) (*entity.FileCommit, error) {
func (m *mockFileCommitSvc) GetCommit(ctx context.Context, commitID string) (*entity.FileCommit, error) {
if m.getCommitFn != nil {
return m.getCommitFn(commitID)
return m.getCommitFn(ctx, commitID)
}
return &entity.FileCommit{ID: commitID, FolderID: "folder-1", Message: "test commit", AuthorID: "u1", FileCount: 1}, nil
}
func (m *mockFileCommitSvc) ListCommitFiles(commitID string) ([]*entity.FileCommitItem, error) {
func (m *mockFileCommitSvc) ListCommitFiles(ctx context.Context, commitID string) ([]*entity.FileCommitItem, error) {
if m.listCommitFilesFn != nil {
return m.listCommitFilesFn(commitID)
return m.listCommitFilesFn(ctx, commitID)
}
return []*entity.FileCommitItem{
{ID: "i1", CommitID: commitID, FileID: "f1", Operation: "add"},
}, nil
}
func (m *mockFileCommitSvc) DiffCommits(fromID, toID string) ([]entity.DiffEntry, error) {
func (m *mockFileCommitSvc) DiffCommits(ctx context.Context, fromID, toID string) ([]entity.DiffEntry, error) {
if m.diffCommitsFn != nil {
return m.diffCommitsFn(fromID, toID)
return m.diffCommitsFn(ctx, fromID, toID)
}
return []entity.DiffEntry{
{FileID: "f1", FileName: "file.txt", Operation: "modify"},
}, nil
}
func (m *mockFileCommitSvc) GetUncommittedChanges(folderID string) ([]entity.DiffEntry, error) {
func (m *mockFileCommitSvc) GetUncommittedChanges(ctx context.Context, folderID string) ([]entity.DiffEntry, error) {
if m.getUncommittedChangesFn != nil {
return m.getUncommittedChangesFn(folderID)
return m.getUncommittedChangesFn(ctx, folderID)
}
return []entity.DiffEntry{
{FileID: "f1", FileName: "new.txt", Operation: "add"},
}, nil
}
func (m *mockFileCommitSvc) GetCommitTree(commitID string) (map[string]interface{}, error) {
func (m *mockFileCommitSvc) GetCommitTree(ctx context.Context, commitID string) (map[string]interface{}, error) {
if m.getCommitTreeFn != nil {
return m.getCommitTreeFn(commitID)
return m.getCommitTreeFn(ctx, commitID)
}
return map[string]interface{}{
"f1": map[string]interface{}{"name": "file.txt", "hash": "abc123", "size": 100, "status": "1"},
}, nil
}
func (m *mockFileCommitSvc) GetCommitFileContent(folderID, commitID, fileID string) ([]byte, error) {
func (m *mockFileCommitSvc) GetCommitFileContent(ctx context.Context, folderID, commitID, fileID string) ([]byte, error) {
if m.getCommitFileContentFn != nil {
return m.getCommitFileContentFn(folderID, commitID, fileID)
return m.getCommitFileContentFn(ctx, folderID, commitID, fileID)
}
return []byte("file content"), nil
}
func (m *mockFileCommitSvc) GetFileVersionHistory(fileID string) ([]entity.VersionEntry, error) {
func (m *mockFileCommitSvc) GetFileVersionHistory(ctx context.Context, fileID string) ([]entity.VersionEntry, error) {
if m.getFileVersionHistoryFn != nil {
return m.getFileVersionHistoryFn(fileID)
return m.getFileVersionHistoryFn(ctx, fileID)
}
now := int64(1718200000000)
return []entity.VersionEntry{
@@ -159,7 +160,7 @@ func setupFileCommitTestNoAuth() *gin.Engine {
func TestFileCommit_CreateCommit_Success(t *testing.T) {
r, mock := setupFileCommitTest("user-1")
mock.createCommitFn = func(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
mock.createCommitFn = func(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
if folderID != "folder-1" {
t.Errorf("expected folder-1, got %s", folderID)
}
@@ -244,7 +245,7 @@ func TestFileCommit_CreateCommit_InvalidJSON(t *testing.T) {
func TestFileCommit_ListCommits_Success(t *testing.T) {
r, mock := setupFileCommitTest("user-1")
mock.listCommitsFn = func(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
mock.listCommitsFn = func(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error) {
if folderID != "folder-1" {
t.Errorf("expected folder-1, got %s", folderID)
}
@@ -292,7 +293,7 @@ func TestFileCommit_GetCommit_Success(t *testing.T) {
func TestFileCommit_GetCommit_NotFound(t *testing.T) {
r, mock := setupFileCommitTest("user-1")
mock.getCommitFn = func(commitID string) (*entity.FileCommit, error) {
mock.getCommitFn = func(ctx context.Context, commitID string) (*entity.FileCommit, error) {
return nil, common.ErrNotFound
}
@@ -379,7 +380,7 @@ func TestFileCommit_GetCommitTree_Success(t *testing.T) {
func TestFileCommit_GetCommitFileContent_Success(t *testing.T) {
r, mock := setupFileCommitTest("user-1")
mock.getCommitFileContentFn = func(folderID, commitID, fileID string) ([]byte, error) {
mock.getCommitFileContentFn = func(ctx context.Context, folderID, commitID, fileID string) ([]byte, error) {
return []byte("hello world"), nil
}

View File

@@ -400,7 +400,8 @@ func (h *SkillSearchHandler) CreateSpace(c *gin.Context) {
return
}
result, code, err := h.spaceService.CreateSpace(&service.CreateSpaceRequest{
ctx := c.Request.Context()
result, code, err := h.spaceService.CreateSpace(ctx, &service.CreateSpaceRequest{
TenantID: user.ID,
Name: req.Name,
Description: req.Description,
@@ -486,7 +487,8 @@ func (h *SkillSearchHandler) UpdateSpace(c *gin.Context) {
return
}
result, code, err := h.spaceService.UpdateSpace(spaceID, user.ID, &service.UpdateSpaceRequest{
ctx := c.Request.Context()
result, code, err := h.spaceService.UpdateSpace(ctx, spaceID, user.ID, &service.UpdateSpaceRequest{
Name: req.Name,
Description: req.Description,
EmbdID: req.EmbdID,