mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-16 04:37:21 +08:00
fix(go): agent explore thumbnail loading for multiple doc_ids (#16514)
## Summary - align the Go `/api/v1/thumbnails` endpoint with the frontend request format for repeated `doc_ids` - return thumbnail mappings for multiple documents instead of failing on a single missing document - preserve Python-compatible thumbnail formatting, including base64 thumbnail passthrough
This commit is contained in:
@@ -52,7 +52,7 @@ type documentServiceIface interface {
|
||||
ListDocuments(page, pageSize int) ([]*service.DocumentResponse, int64, error)
|
||||
ListDocumentsByDatasetID(kbID string, page, pageSize int) ([]*entity.DocumentListItem, int64, error)
|
||||
GetDocumentsByAuthorID(authorID, page, pageSize int) ([]*service.DocumentResponse, int64, error)
|
||||
GetThumbnail(docID string) (*service.ThumbnailResponse, error)
|
||||
GetThumbnails(userID string, docIDs []string) (map[string]string, error)
|
||||
GetDocumentImage(imageID string) ([]byte, error)
|
||||
GetMetadataSummary(kbID string, docIDs []string) (map[string]interface{}, error)
|
||||
SetDocumentMetadata(docID string, meta map[string]interface{}) error
|
||||
@@ -168,40 +168,51 @@ func (h *DocumentHandler) GetDocumentByID(c *gin.Context) {
|
||||
|
||||
// GetThumbnail Get thumbnails for documents.
|
||||
func (h *DocumentHandler) GetThumbnail(c *gin.Context) {
|
||||
_, errorCode, errorMessage := GetUser(c)
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Query("doc_ids")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": errors.New("invalid document id"),
|
||||
})
|
||||
docIDs := parseThumbnailDocIDs(c)
|
||||
if len(docIDs) == 0 {
|
||||
jsonError(c, common.CodeArgumentError, `Lack of "Document ID"`)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.documentService.GetThumbnail(id)
|
||||
result, err := h.documentService.GetThumbnails(user.ID, docIDs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": fmt.Errorf("thumbnail not found"),
|
||||
})
|
||||
jsonError(c, common.CodeServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if result.Thumbnail != nil && *result.Thumbnail != "" {
|
||||
newThumbURL := fmt.Sprintf("/api/v1/documents/images/%s-%s", result.KbID, *result.Thumbnail)
|
||||
result.Thumbnail = &newThumbURL
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeSuccess,
|
||||
"data": map[string]interface{}{result.ID: result.Thumbnail},
|
||||
"data": result,
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
||||
func parseThumbnailDocIDs(c *gin.Context) []string {
|
||||
rawValues := c.QueryArray("doc_ids")
|
||||
seen := make(map[string]struct{}, len(rawValues))
|
||||
docIDs := make([]string, 0, len(rawValues))
|
||||
|
||||
for _, raw := range rawValues {
|
||||
id := strings.TrimSpace(raw)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
docIDs = append(docIDs, id)
|
||||
}
|
||||
|
||||
return docIDs
|
||||
}
|
||||
|
||||
// GetDocumentImage returns a document image from object storage.
|
||||
func (h *DocumentHandler) GetDocumentImage(c *gin.Context) {
|
||||
imageID := c.Param("image_id")
|
||||
|
||||
@@ -42,6 +42,10 @@ type fakeDocumentService struct {
|
||||
err error
|
||||
stopResult map[string]interface{}
|
||||
stopErr error
|
||||
thumbnails map[string]string
|
||||
thumbnailErr error
|
||||
thumbnailUserID string
|
||||
thumbnailDocIDs []string
|
||||
metadataSummary map[string]interface{}
|
||||
metadataErr error
|
||||
metadataKBID string
|
||||
@@ -145,8 +149,10 @@ func (f *fakeDocumentService) ListDocumentsByDatasetID(kbID string, page, pageSi
|
||||
func (f *fakeDocumentService) BatchUpdateDocumentStatus(userID, datasetID, status string, documentIDs []string) (map[string]interface{}, common.ErrorCode, error) {
|
||||
return map[string]interface{}{}, common.CodeSuccess, nil
|
||||
}
|
||||
func (f *fakeDocumentService) GetThumbnail(docID string) (*service.ThumbnailResponse, error) {
|
||||
return nil, nil
|
||||
func (f *fakeDocumentService) GetThumbnails(userID string, docIDs []string) (map[string]string, error) {
|
||||
f.thumbnailUserID = userID
|
||||
f.thumbnailDocIDs = append([]string(nil), docIDs...)
|
||||
return f.thumbnails, f.thumbnailErr
|
||||
}
|
||||
func (f *fakeDocumentService) GetDocumentImage(imageID string) ([]byte, error) {
|
||||
return nil, nil
|
||||
@@ -908,6 +914,48 @@ func TestMetadataSummaryByDataset_Success(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetThumbnail_Success(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
fake := &fakeDocumentService{
|
||||
thumbnails: map[string]string{
|
||||
"doc-1": "/api/v1/documents/images/kb-1-thumb-1.png",
|
||||
"doc-2": "",
|
||||
},
|
||||
}
|
||||
h := &DocumentHandler{
|
||||
documentService: fake,
|
||||
}
|
||||
|
||||
c, w := setupGinContextWithUser("GET", "/api/v1/thumbnails?doc_ids=doc-1&doc_ids=doc-2", "")
|
||||
|
||||
h.GetThumbnail(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(fake.thumbnailDocIDs) != 2 || fake.thumbnailDocIDs[0] != "doc-1" || fake.thumbnailDocIDs[1] != "doc-2" {
|
||||
t.Fatalf("unexpected docIDs: %#v", fake.thumbnailDocIDs)
|
||||
}
|
||||
if fake.thumbnailUserID != "user-1" {
|
||||
t.Fatalf("unexpected userID: %s", fake.thumbnailUserID)
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeSuccess) {
|
||||
t.Fatalf("expected code %d, got %v", common.CodeSuccess, resp["code"])
|
||||
}
|
||||
data := resp["data"].(map[string]interface{})
|
||||
if data["doc-1"] != "/api/v1/documents/images/kb-1-thumb-1.png" {
|
||||
t.Fatalf("unexpected thumbnail for doc-1: %v", data["doc-1"])
|
||||
}
|
||||
if data["doc-2"] != "" {
|
||||
t.Fatalf("unexpected thumbnail for doc-2: %v", data["doc-2"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDocumentArtifact_Success(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := &DocumentHandler{
|
||||
|
||||
Reference in New Issue
Block a user