mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-09-08 10:14:35 +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:
@@ -141,6 +141,8 @@ type ThumbnailResponse struct {
|
||||
KbID string `json:"kb_id"`
|
||||
}
|
||||
|
||||
const imgBase64Prefix = "data:image/png;base64,"
|
||||
|
||||
type ArtifactResponse struct {
|
||||
Data []byte
|
||||
ContentType string
|
||||
@@ -874,17 +876,48 @@ func (s *DocumentService) ListDocuments(page, pageSize int) ([]*DocumentResponse
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
func (s *DocumentService) GetThumbnail(docID string) (*ThumbnailResponse, error) {
|
||||
document, err := s.documentDAO.GetByID(docID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func (s *DocumentService) GetThumbnails(userID string, docIDs []string) (map[string]string, error) {
|
||||
if len(docIDs) == 0 {
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
|
||||
var result ThumbnailResponse
|
||||
result.ID = document.ID
|
||||
result.Thumbnail = document.Thumbnail
|
||||
result.KbID = document.KbID
|
||||
return &result, nil
|
||||
tenantIDs := []string{userID}
|
||||
if userID != "" {
|
||||
ids, err := dao.NewUserTenantDAO().GetTenantIDsByUserID(userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch user tenants: %w", err)
|
||||
}
|
||||
tenantIDs = append(tenantIDs, ids...)
|
||||
}
|
||||
|
||||
documents, err := s.documentDAO.GetByIDsAndTenantIDs(docIDs, tenantIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch document thumbnails: %w", err)
|
||||
}
|
||||
|
||||
result := make(map[string]string, len(documents))
|
||||
for _, document := range documents {
|
||||
if document == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
thumbnail := ""
|
||||
if document.Thumbnail != nil && *document.Thumbnail != "" {
|
||||
if strings.HasPrefix(*document.Thumbnail, imgBase64Prefix) {
|
||||
thumbnail = *document.Thumbnail
|
||||
} else {
|
||||
thumbnail = fmt.Sprintf(
|
||||
"/api/v1/documents/images/%s-%s",
|
||||
document.KbID,
|
||||
*document.Thumbnail,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
result[document.ID] = thumbnail
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *DocumentService) BatchUpdateDocumentStatus(userID, datasetID, status string, documentIDs []string) (map[string]interface{}, common.ErrorCode, error) {
|
||||
|
||||
@@ -2230,3 +2230,88 @@ func TestGetDocumentArtifact_AuthGate(t *testing.T) {
|
||||
t.Errorf("user-2 with unrelated session: want ErrArtifactNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetThumbnails_AlignsWithPythonFormatting(t *testing.T) {
|
||||
db := setupServiceTestDB(t)
|
||||
pushServiceDB(t, db)
|
||||
|
||||
if err := db.AutoMigrate(&entity.Document{}, &entity.Knowledgebase{}, &entity.UserTenant{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
insertTestKB(t, "kb-1", "tenant-1", 0, 0, 0)
|
||||
insertTestKB(t, "kb-2", "tenant-1", 0, 0, 0)
|
||||
insertTestKB(t, "kb-other", "tenant-other", 0, 0, 0)
|
||||
if err := db.Create(&entity.UserTenant{
|
||||
ID: "user-1_tenant-1",
|
||||
UserID: "user-1",
|
||||
TenantID: "tenant-1",
|
||||
Role: "owner",
|
||||
InvitedBy: "user-1",
|
||||
Status: sptr("1"),
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("seed user tenant: %v", err)
|
||||
}
|
||||
|
||||
base64Thumb := "data:image/png;base64,AAAA"
|
||||
fileThumb := "thumb.png"
|
||||
otherThumb := "secret.png"
|
||||
if err := db.Create(&entity.Document{
|
||||
ID: "doc-file",
|
||||
KbID: "kb-1",
|
||||
Thumbnail: &fileThumb,
|
||||
ParserID: "naive",
|
||||
ParserConfig: entity.JSONMap{},
|
||||
SourceType: "local",
|
||||
Type: "pdf",
|
||||
CreatedBy: "user-1",
|
||||
Suffix: "png",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("seed file thumbnail doc: %v", err)
|
||||
}
|
||||
if err := db.Create(&entity.Document{
|
||||
ID: "doc-base64",
|
||||
KbID: "kb-2",
|
||||
Thumbnail: &base64Thumb,
|
||||
ParserID: "naive",
|
||||
ParserConfig: entity.JSONMap{},
|
||||
SourceType: "local",
|
||||
Type: "pdf",
|
||||
CreatedBy: "user-1",
|
||||
Suffix: "png",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("seed base64 thumbnail doc: %v", err)
|
||||
}
|
||||
if err := db.Create(&entity.Document{
|
||||
ID: "doc-other",
|
||||
KbID: "kb-other",
|
||||
Thumbnail: &otherThumb,
|
||||
ParserID: "naive",
|
||||
ParserConfig: entity.JSONMap{},
|
||||
SourceType: "local",
|
||||
Type: "pdf",
|
||||
CreatedBy: "user-other",
|
||||
Suffix: "png",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("seed other tenant thumbnail doc: %v", err)
|
||||
}
|
||||
|
||||
svc := testDocumentService(t)
|
||||
got, err := svc.GetThumbnails("user-1", []string{"doc-file", "doc-base64", "doc-other", "missing-doc"})
|
||||
if err != nil {
|
||||
t.Fatalf("GetThumbnails failed: %v", err)
|
||||
}
|
||||
|
||||
if got["doc-file"] != "/api/v1/documents/images/kb-1-thumb.png" {
|
||||
t.Fatalf("unexpected file thumbnail: %q", got["doc-file"])
|
||||
}
|
||||
if got["doc-base64"] != base64Thumb {
|
||||
t.Fatalf("unexpected base64 thumbnail: %q", got["doc-base64"])
|
||||
}
|
||||
if _, ok := got["missing-doc"]; ok {
|
||||
t.Fatalf("did not expect missing doc in result: %#v", got)
|
||||
}
|
||||
if _, ok := got["doc-other"]; ok {
|
||||
t.Fatalf("did not expect other tenant doc in result: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user