RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(api): enforce document image ownership authorization (#19818)
Sbaaoui Idriss9ea0246f8d45
diff --git a/api/apps/restful_apis/document_api.py b/api/apps/restful_apis/document_api.pyindex 7a9fd9616c..c2def75f1d 100644--- a/api/apps/restful_apis/document_api.py+++ b/api/apps/restful_apis/document_api.py@@ -80,6 +80,7 @@ from api.utils.file_response import apply_preview_file_response_headersfrom api.utils.web_utils import CONTENT_TYPE_MAP, html2pdf, is_valid_url, apply_safe_file_response_headersfrom common.ssrf_guard import assert_url_is_safefrom rag.nlp import search+from rag.utils.base64_image import parse_storage_composite_iddef _normalize_legacy_raptor_config(req: dict) -> None:@@ -843,7 +844,7 @@ def list_docs(dataset_id, tenant_id):renamed_doc_list = [map_doc_keys(doc) for doc in payload]for doc_item in renamed_doc_list:if doc_item["thumbnail"] and not doc_item["thumbnail"].startswith(IMG_BASE64_PREFIX):- doc_item["thumbnail"] = f"/api/v1/documents/images/{dataset_id}-{doc_item['thumbnail']}"+ doc_item["thumbnail"] = f"/api/v1/documents/{doc_item['id']}/thumbnail"if doc_item.get("source_type"):doc_item["source_type"] = doc_item["source_type"].split("/")[0]if doc_item["parser_config"].get("metadata"):@@ -1324,10 +1325,10 @@ def list_thumbnails():return get_error_argument_result(str(e))try:- docs = DocumentService.get_thumbnails(doc_ids)+ docs = [doc for doc in DocumentService.get_thumbnails(doc_ids) if DocumentService.accessible(doc["id"], current_user.id)]for doc_item in docs:if doc_item["thumbnail"] and not doc_item["thumbnail"].startswith(IMG_BASE64_PREFIX):- doc_item["thumbnail"] = f"/api/v1/documents/images/{doc_item['kb_id']}-{doc_item['thumbnail']}"+ doc_item["thumbnail"] = f"/api/v1/documents/{doc_item['id']}/thumbnail"return get_json_result(data={d["id"]: d["thumbnail"] for d in docs})except Exception as e:@@ -1786,8 +1787,8 @@ async def stop_parse_documents(tenant_id, dataset_id):def _parse_document_image_id(image_id: str) -> tuple[str, str] | None:"""Split a composite document image ID into storage bucket and object key.- Thumbnail URLs use ``{dataset_id}-{thumbnail}``. Only the first hyphen- separates the dataset/kb id (bucket) from the object key, which may+ Legacy chunk image IDs use ``{dataset_id}-{object_key}``. Only the first+ hyphen separates the dataset/kb id (bucket) from the object key, which maycontain additional hyphens (e.g. ``page-1.png``).Args:@@ -1816,16 +1817,57 @@ def _detect_image_content_type_from_bytes(data):return None-def _content_type_for_document_image(object_name, data):- ext_match = re.search(r"\.([^.]+)$", object_name.lower())- if ext_match:- content_type = CONTENT_TYPE_MAP.get(ext_match.group(1))- if content_type and content_type.startswith("image/"):- return content_type- detected = _detect_image_content_type_from_bytes(data)- if detected:- return detected- return "application/octet-stream"+async def _document_image_response(data):+ content_type = _detect_image_content_type_from_bytes(data)+ if not content_type:+ return get_data_error_result(message="Image not found.")+ response = await make_response(data)+ response.headers.set("Content-Type", content_type)+ response.headers.set("Cache-Control", "no-store")+ return response+++async def _get_document_image_bytes(bucket, object_name):+ try:+ return await thread_pool_exec(settings.STORAGE_IMPL.get, bucket, object_name)+ except Exception:+ logging.warning("Failed to retrieve authorized document image")+ return None++[email protected]("/documents/<doc_id>/thumbnail", methods=["GET"]) # noqa: F821+@login_required(auth_types=[AUTH_JWT, AUTH_API, AUTH_BETA])+async def get_document_thumbnail(doc_id):+ try:+ if not DocumentService.accessible(doc_id, current_user.id):+ return get_data_error_result(message="Image not found.")+ e, doc = DocumentService.get_by_id(doc_id)+ if not e or not doc.thumbnail or doc.thumbnail.startswith(IMG_BASE64_PREFIX):+ return get_data_error_result(message="Image not found.")+ data = await _get_document_image_bytes(doc.kb_id, doc.thumbnail)+ if not data:+ return get_data_error_result(message="Image not found.")+ return await _document_image_response(data)+ except Exception as e:+ return server_error_response(e)++[email protected]("/documents/<doc_id>/images/<image_id>", methods=["GET"]) # noqa: F821+@login_required(auth_types=[AUTH_JWT, AUTH_API, AUTH_BETA])+async def get_document_image_for_document(doc_id, image_id):+ try:+ if not DocumentService.accessible(doc_id, current_user.id):+ return get_data_error_result(message="Image not found.")+ e, doc = DocumentService.get_by_id(doc_id)+ parsed = parse_storage_composite_id(image_id)+ if not e or not parsed or not DocumentService.image_belongs_to_document(doc, image_id):+ return get_data_error_result(message="Image not found.")+ data = await _get_document_image_bytes(*parsed)+ if not data:+ return get_data_error_result(message="Image not found.")+ return await _document_image_response(data)+ except Exception as e:+ return server_error_response(e)@manager.route("/documents/images/<image_id>", methods=["GET"]) # noqa: F821@@ -1857,14 +1899,13 @@ async def get_document_image(image_id):if not parsed:return get_data_error_result(message="Image not found.")bkt, nm = parsed- data = await thread_pool_exec(settings.STORAGE_IMPL.get, bkt, nm)+ e, doc = DocumentService.get_by_image_id(bkt, image_id)+ if not e or not DocumentService.accessible(doc.id, current_user.id):+ return get_data_error_result(message="Image not found.")+ data = await _get_document_image_bytes(bkt, nm)if data is None:- logging.warning("get_document_image: storage miss image_id: %s, bucket: %s, key: %s", image_id, bkt, nm)return get_data_error_result(message="Image not found.")- content_type = _content_type_for_document_image(nm, data)- response = await make_response(data)- response.headers.set("Content-Type", content_type)- return response+ return await _document_image_response(data)except Exception as e:return server_error_response(e)diff --git a/api/db/services/document_service.py b/api/db/services/document_service.pyindex 7073dd3d25..cdc195314e 100644--- a/api/db/services/document_service.py+++ b/api/db/services/document_service.py@@ -1028,6 +1028,51 @@ class DocumentService(CommonService):fields = [cls.model.id, cls.model.kb_id, cls.model.thumbnail]return list(cls.model.select(*fields).where(cls.model.id.in_(docids)).dicts())+ @classmethod+ def _find_image_document_id(cls, kb_id, image_id, doc_id=None):+ e, kb = KnowledgebaseService.get_by_id(kb_id)+ if not e:+ return None++ filters = {"img_id": image_id}+ if doc_id:+ filters["doc_id"] = doc_id+ try:+ result = settings.docStoreConn.search(+ ["doc_id", "img_id"],+ [],+ filters,+ [],+ OrderByExpr(),+ 0,+ 1,+ search.index_name(kb.tenant_id),+ [kb_id],+ )+ rows = settings.docStoreConn.get_fields(result, ["doc_id", "img_id"])+ except Exception:+ logging.warning("Failed to resolve document image ownership")+ return None++ for row in (rows or {}).values():+ if row.get("img_id") == image_id and row.get("doc_id") and (not doc_id or row["doc_id"] == doc_id):+ return row["doc_id"]+ return None++ @classmethod+ def image_belongs_to_document(cls, doc, image_id):+ return cls._find_image_document_id(doc.kb_id, image_id, doc.id) == doc.id++ @classmethod+ def get_by_image_id(cls, kb_id, image_id):+ doc_id = cls._find_image_document_id(kb_id, image_id)+ if not doc_id:+ return False, None+ e, doc = cls.get_by_id(doc_id)+ if not e or doc.kb_id != kb_id:+ return False, None+ return True, doc+@classmethod@DB.connection_context()def update_parser_config(cls, id, config):diff --git a/internal/dao/document.go b/internal/dao/document.goindex 582d53b0db..d706ecf37d 100644--- a/internal/dao/document.go+++ b/internal/dao/document.go@@ -447,22 +447,6 @@ func (dao *DocumentDAO) GetByIDs(ctx context.Context, db *gorm.DB, ids []string)return documents, nil}-// GetByIDsAndTenantIDs retrieves documents by IDs scoped to knowledgebase owners.-func (dao *DocumentDAO) GetByIDsAndTenantIDs(ctx context.Context, db *gorm.DB, ids, tenantIDs []string) ([]*entity.Document, error) {- if len(ids) == 0 || len(tenantIDs) == 0 {- return nil, nil- }- var documents []*entity.Document- err := db.WithContext(ctx).Model(&entity.Document{}).- Joins("JOIN knowledgebase ON document.kb_id = knowledgebase.id").- Where("document.id IN ? AND knowledgebase.tenant_id IN ? AND knowledgebase.status = ?", ids, tenantIDs, string(entity.StatusValid)).- Find(&documents).Error- if err != nil {- return nil, err- }- return documents, nil-}-// GetByDocumentIDAndDatasetID retrieves a document by document ID and dataset/KB ID.func (dao *DocumentDAO) GetByDocumentIDAndDatasetID(ctx context.Context, db *gorm.DB, documentID, datasetID string) (*entity.Document, error) {var document entity.Documentdiff --git a/internal/handler/document.go b/internal/handler/document.goindex 8743d53c74..6efddefbbc 100644--- a/internal/handler/document.go+++ b/internal/handler/document.go@@ -23,11 +23,9 @@ import ("errors""fmt""io"- "mime""mime/multipart""net/http""net/url"- "path/filepath""ragflow/internal/common""ragflow/internal/entity""ragflow/internal/utility"@@ -63,7 +61,9 @@ type documentServiceIface interface {GetMetadataByKBs(ctx context.Context, kbIDs []string) (map[string]interface{}, error)GetDocumentsByAuthorID(ctx context.Context, authorID, page, pageSize int) ([]*document.DocumentResponse, int64, error)GetThumbnails(ctx context.Context, userID string, docIDs []string) (map[string]string, error)- GetDocumentImage(ctx context.Context, imageID string) ([]byte, error)+ GetDocumentImage(ctx context.Context, userID, imageID string) ([]byte, error)+ GetDocumentImageForDocument(ctx context.Context, userID, docID, imageID string) ([]byte, error)+ GetDocumentThumbnail(ctx context.Context, userID, docID string) ([]byte, error)GetMetadataSummary(ctx context.Context, kbID string, docIDs []string) (map[string]interface{}, error)SetDocumentMetadata(ctx context.Context, docID string, meta map[string]interface{}) errorDeleteDocumentMetadata(ctx context.Context, docID string, keys []string) error@@ -194,22 +194,60 @@ func parseThumbnailDocIDs(c *gin.Context) []string {// GetDocumentImage returns a document image from object storage.func (h *DocumentHandler) GetDocumentImage(c *gin.Context) {+ user, errorCode, errorMessage := GetUser(c)+ if errorCode != common.CodeSuccess {+ common.ErrorWithCode(c, errorCode, errorMessage)+ return+ }imageID := c.Param("image_id")ctx := c.Request.Context()- data, err := h.documentService.GetDocumentImage(ctx, imageID)+ data, err := h.documentService.GetDocumentImage(ctx, user.ID, imageID)if err != nil {common.ResponseWithCodeData(c, common.CodeDataError, nil, "Image not found.")return}+ h.writeDocumentImage(c, data)+}- contentType := documentImageContentType(imageID, data)- c.Data(http.StatusOK, contentType, data)+func (h *DocumentHandler) GetDocumentImageForDocument(c *gin.Context) {+ user, errorCode, errorMessage := GetUser(c)+ if errorCode != common.CodeSuccess {+ common.ErrorWithCode(c, errorCode, errorMessage)+ return+ }+ data, err := h.documentService.GetDocumentImageForDocument(c.Request.Context(), user.ID, c.Param("id"), c.Param("image_id"))+ if err != nil {+ common.ResponseWithCodeData(c, common.CodeDataError, nil, "Image not found.")+ return+ }+ h.writeDocumentImage(c, data)}-func documentImageContentType(imageID string, data []byte) string {- if contentType := mime.TypeByExtension(strings.ToLower(filepath.Ext(imageID))); strings.HasPrefix(contentType, "image/") {- return contentType+func (h *DocumentHandler) GetDocumentThumbnail(c *gin.Context) {+ user, errorCode, errorMessage := GetUser(c)+ if errorCode != common.CodeSuccess {+ common.ErrorWithCode(c, errorCode, errorMessage)+ return+ }+ data, err := h.documentService.GetDocumentThumbnail(c.Request.Context(), user.ID, c.Param("id"))+ if err != nil {+ common.ResponseWithCodeData(c, common.CodeDataError, nil, "Image not found.")+ return+ }+ h.writeDocumentImage(c, data)+}++func (h *DocumentHandler) writeDocumentImage(c *gin.Context, data []byte) {+ contentType := documentImageContentType(data)+ if contentType == "" {+ common.ResponseWithCodeData(c, common.CodeDataError, nil, "Image not found.")+ return}+ c.Header("Cache-Control", "no-store")+ c.Data(http.StatusOK, contentType, data)+}++func documentImageContentType(data []byte) string {switch {case bytes.HasPrefix(data, []byte("\x89PNG\r\n\x1a\n")):return "image/png"@@ -222,7 +260,7 @@ func documentImageContentType(imageID string, data []byte) string {case bytes.HasPrefix(data, []byte("BM")):return "image/bmp"default:- return "application/octet-stream"+ return ""}}diff --git a/internal/handler/document_test.go b/internal/handler/document_test.goindex 39fbc1bf95..08c754ded3 100644--- a/internal/handler/document_test.go+++ b/internal/handler/document_test.go@@ -88,6 +88,11 @@ type fakeDocumentService struct {metadataByKBs map[string]interface{}hasActiveTasks boolhasActiveTasksErr error+ imageData []byte+ imageErr error+ imageUserID string+ imageID string+ imageDocumentID string}func TestMapDocumentListItemIncludesLatestIngestionEvent(t *testing.T) {@@ -220,8 +225,21 @@ func (f *fakeDocumentService) GetThumbnails(ctx context.Context, userID string,f.thumbnailDocIDs = append([]string(nil), docIDs...)return f.thumbnails, f.thumbnailErr}-func (f *fakeDocumentService) GetDocumentImage(ctx context.Context, imageID string) ([]byte, error) {- return nil, nil+func (f *fakeDocumentService) GetDocumentImage(ctx context.Context, userID, imageID string) ([]byte, error) {+ f.imageUserID = userID+ f.imageID = imageID+ return f.imageData, f.imageErr+}+func (f *fakeDocumentService) GetDocumentImageForDocument(ctx context.Context, userID, docID, imageID string) ([]byte, error) {+ f.imageUserID = userID+ f.imageDocumentID = docID+ f.imageID = imageID+ return f.imageData, f.imageErr+}+func (f *fakeDocumentService) GetDocumentThumbnail(ctx context.Context, userID, docID string) ([]byte, error) {+ f.imageUserID = userID+ f.imageDocumentID = docID+ return f.imageData, f.imageErr}func (f *fakeDocumentService) GetDocumentsByAuthorID(ctx context.Context, authorID, page, pageSize int) ([]*document.DocumentResponse, int64, error) {return nil, 0, nil@@ -1775,7 +1793,7 @@ 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-1": "/api/v1/documents/doc-1/thumbnail","doc-2": "",},}@@ -1805,7 +1823,7 @@ func TestGetThumbnail_Success(t *testing.T) {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" {+ if data["doc-1"] != "/api/v1/documents/doc-1/thumbnail" {t.Fatalf("unexpected thumbnail for doc-1: %v", data["doc-1"])}if data["doc-2"] != "" {@@ -1813,6 +1831,78 @@ func TestGetThumbnail_Success(t *testing.T) {}}+func TestGetDocumentImagePassesPrincipalAndDisablesCaching(t *testing.T) {+ fake := &fakeDocumentService{imageData: []byte("\x89PNG\r\n\x1a\nimage")}+ h := &DocumentHandler{documentService: fake}+ c, w := setupGinContextWithUser("GET", "/api/v1/documents/doc-1/images/imagetemps-page-1.png", "")+ c.Params = gin.Params{{Key: "id", Value: "doc-1"}, {Key: "image_id", Value: "imagetemps-page-1.png"}}++ h.GetDocumentImageForDocument(c)++ if w.Code != http.StatusOK || w.Header().Get("Content-Type") != "image/png" || w.Header().Get("Cache-Control") != "no-store" {+ t.Fatalf("unexpected response: status=%d content-type=%q cache=%q", w.Code, w.Header().Get("Content-Type"), w.Header().Get("Cache-Control"))+ }+ if fake.imageUserID != "user-1" || fake.imageDocumentID != "doc-1" || fake.imageID != "imagetemps-page-1.png" {+ t.Fatalf("service arguments: user=%q doc=%q image=%q", fake.imageUserID, fake.imageDocumentID, fake.imageID)+ }+}++func TestGetDocumentImageRequiresPrincipal(t *testing.T) {+ fake := &fakeDocumentService{imageData: []byte("\x89PNG\r\n\x1a\nimage")}+ h := &DocumentHandler{documentService: fake}+ w := httptest.NewRecorder()+ c, _ := gin.CreateTestContext(w)+ c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/documents/doc-1/images/imagetemps-page-1.png", nil)+ c.Params = gin.Params{{Key: "id", Value: "doc-1"}, {Key: "image_id", Value: "imagetemps-page-1.png"}}++ h.GetDocumentImageForDocument(c)++ var response map[string]any+ if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {+ t.Fatal(err)+ }+ if response["code"] == float64(common.CodeSuccess) || fake.imageID != "" {+ t.Fatalf("anonymous request reached image service: response=%s image=%q", w.Body.String(), fake.imageID)+ }+}++func TestGetDocumentImageRejectsNonImagePayload(t *testing.T) {+ fake := &fakeDocumentService{imageData: []byte("%PDF-1.7")}+ h := &DocumentHandler{documentService: fake}+ c, w := setupGinContextWithUser("GET", "/api/v1/documents/images/anything.png", "")+ c.Params = gin.Params{{Key: "image_id", Value: "anything.png"}}++ h.GetDocumentImage(c)++ var response map[string]any+ if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {+ t.Fatal(err)+ }+ if response["code"] != float64(common.CodeDataError) {+ t.Fatalf("unexpected response: %s", w.Body.String())+ }+}++func TestDocumentImageContentTypeSupportedFormats(t *testing.T) {+ tests := map[string]struct {+ data []byte+ want string+ }{+ "png": {[]byte("\x89PNG\r\n\x1a\n"), "image/png"},+ "jpeg": {[]byte{0xff, 0xd8, 0xff}, "image/jpeg"},+ "gif": {[]byte("GIF89a"), "image/gif"},+ "webp": {[]byte("RIFF\x00\x00\x00\x00WEBP"), "image/webp"},+ "bmp": {[]byte("BM"), "image/bmp"},+ }+ for name, test := range tests {+ t.Run(name, func(t *testing.T) {+ if got := documentImageContentType(test.data); got != test.want {+ t.Fatalf("documentImageContentType() = %q, want %q", got, test.want)+ }+ })+ }+}+func TestGetDocumentArtifact_Success(t *testing.T) {gin.SetMode(gin.TestMode)h := &DocumentHandler{diff --git a/internal/router/router.go b/internal/router/router.goindex 00a0fbab83..5dd84afce4 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -219,7 +219,6 @@ func (r *Router) Setup(engine *gin.Engine) {// Public bot endpoints (authenticated with an SDK beta token, not a session)apiBetaAuth.GET("/documents/:id/preview", r.documentHandler.GetDocumentPreview)- apiBetaAuth.GET("/documents/images/:image_id", r.documentHandler.GetDocumentImage)apiBetaAuth.GET("/thumbnails", r.documentHandler.GetThumbnail)apiBetaAuth.POST("/agents/:canvas_id/upload", r.agentHandler.UploadAgentFile)@@ -304,6 +303,9 @@ func (r *Router) Setup(engine *gin.Engine) {documents.POST("/upload", r.documentHandler.UploadInfo)documents.GET("", r.documentHandler.ListDocuments)documents.GET("/artifact/:filename", r.documentHandler.GetDocumentArtifact)+ documents.GET("/:id/thumbnail", r.documentHandler.GetDocumentThumbnail)+ documents.GET("/:id/images/:image_id", r.documentHandler.GetDocumentImageForDocument)+ documents.GET("/images/:image_id", r.documentHandler.GetDocumentImage)documents.GET("/:id", r.documentHandler.GetDocumentByID)documents.PUT("/:id", r.documentHandler.UpdateDocument)documents.DELETE("/:id", r.documentHandler.DeleteDocument)diff --git a/internal/service/document/document_artifact.go b/internal/service/document/document_artifact.goindex a9ff1dda2f..dc20234421 100644--- a/internal/service/document/document_artifact.go+++ b/internal/service/document/document_artifact.go@@ -2,30 +2,116 @@ package documentimport ("context"+ "encoding/hex"+ "errors""fmt""path/filepath""strings""ragflow/internal/common""ragflow/internal/dao"+ enginetypes "ragflow/internal/engine/types""ragflow/internal/parser/parser"+ "ragflow/internal/service""ragflow/internal/storage""ragflow/internal/utility")-// GetDocumentImage retrieves an image object from storage.-func (s *DocumentService) GetDocumentImage(ctx context.Context, imageID string) ([]byte, error) {- parts := strings.SplitN(imageID, "-", 2)- if len(parts) != 2 || parts[0] == "" || parts[1] == "" {- return nil, fmt.Errorf("image not found")+var ErrDocumentImageNotFound = errors.New("document image not found")++// GetDocumentImage serves legacy KB-scoped image IDs only when an indexed+// chunk proves which accessible document owns the exact image ID.+func (s *DocumentService) GetDocumentImage(ctx context.Context, userID, imageID string) ([]byte, error) {+ bucket, _, ok := strings.Cut(imageID, "-")+ if !ok || len(bucket) != 32 {+ return nil, ErrDocumentImageNotFound+ }+ if decoded, err := hex.DecodeString(bucket); err != nil || len(decoded) != 16 {+ return nil, ErrDocumentImageNotFound+ }+ docID, err := s.imageDocumentID(ctx, bucket, imageID, "")+ if err != nil || docID == "" {+ return nil, ErrDocumentImageNotFound+ }+ return s.GetDocumentImageForDocument(ctx, userID, docID, imageID)+}++// GetDocumentImageForDocument retrieves an image after proving that the exact+// composite image ID belongs to an indexed chunk of an accessible document.+func (s *DocumentService) GetDocumentImageForDocument(ctx context.Context, userID, docID, imageID string) ([]byte, error) {+ doc, err := s.documentDAO.GetByID(ctx, dao.DB, docID)+ if err != nil || doc == nil || !s.kbDAO.Accessible(ctx, dao.DB, doc.KbID, userID) {+ return nil, ErrDocumentImageNotFound+ }+ bucket, objectKey, ok := strings.Cut(imageID, "-")+ if !ok || bucket == "" || objectKey == "" {+ return nil, ErrDocumentImageNotFound+ }+ ownerID, err := s.imageDocumentID(ctx, doc.KbID, imageID, doc.ID)+ if err != nil || ownerID != doc.ID {+ return nil, ErrDocumentImageNotFound+ }++ storageImpl := storage.GetStorageFactory().GetStorage()+ if storageImpl == nil {+ return nil, fmt.Errorf("storage not initialized")}+ data, err := storageImpl.Get(ctx, bucket, objectKey)+ if err != nil || len(data) == 0 {+ return nil, ErrDocumentImageNotFound+ }+ return data, nil+}++// GetDocumentThumbnail resolves the storage key exclusively from authorized+// document metadata.+func (s *DocumentService) GetDocumentThumbnail(ctx context.Context, userID, docID string) ([]byte, error) {+ doc, err := s.documentDAO.GetByID(ctx, dao.DB, docID)+ if err != nil || doc == nil || !s.kbDAO.Accessible(ctx, dao.DB, doc.KbID, userID) || doc.Thumbnail == nil || *doc.Thumbnail == "" || strings.HasPrefix(*doc.Thumbnail, imgBase64Prefix) {+ return nil, ErrDocumentImageNotFound+ }storageImpl := storage.GetStorageFactory().GetStorage()if storageImpl == nil {return nil, fmt.Errorf("storage not initialized")}+ data, err := storageImpl.Get(ctx, doc.KbID, *doc.Thumbnail)+ if err != nil || len(data) == 0 {+ return nil, ErrDocumentImageNotFound+ }+ return data, nil+}- return storageImpl.Get(ctx, parts[0], parts[1])+func (s *DocumentService) imageDocumentID(ctx context.Context, kbID, imageID, docID string) (string, error) {+ if s.docEngine == nil {+ return "", ErrDocumentImageNotFound+ }+ tenantID, err := dao.GetTenantIDByKBID(ctx, dao.DB, kbID)+ if err != nil {+ return "", ErrDocumentImageNotFound+ }+ filter := map[string]any{"img_id": imageID}+ if docID != "" {+ filter["doc_id"] = docID+ }+ result, err := s.docEngine.Search(ctx, &enginetypes.SearchRequest{+ IndexNames: []string{service.IndexName(tenantID)},+ KbIDs: []string{kbID},+ Limit: 1,+ SelectFields: []string{"doc_id", "img_id"},+ Filter: filter,+ })+ if err != nil || result == nil {+ return "", ErrDocumentImageNotFound+ }+ for _, chunk := range result.Chunks {+ foundDocID, _ := chunk["doc_id"].(string)+ foundImageID, _ := chunk["img_id"].(string)+ if foundDocID != "" && foundImageID == imageID && (docID == "" || foundDocID == docID) {+ return foundDocID, nil+ }+ }+ return "", ErrDocumentImageNotFound}// GetDocumentArtifact retrieves a sandbox artifact from object storage.diff --git a/internal/service/document/document_list.go b/internal/service/document/document_list.goindex dba2d7a1be..78054b91ba 100644--- a/internal/service/document/document_list.go+++ b/internal/service/document/document_list.go@@ -70,23 +70,14 @@ func (s *DocumentService) GetThumbnails(ctx context.Context, userID string, docIreturn map[string]string{}, nil}- tenantIDs := []string{userID}- if userID != "" {- ids, err := dao.NewUserTenantDAO().GetTenantIDsByUserID(ctx, dao.DB, userID)- if err != nil {- return nil, fmt.Errorf("failed to fetch user tenants: %w", err)- }- tenantIDs = append(tenantIDs, ids...)- }-- documents, err := s.documentDAO.GetByIDsAndTenantIDs(ctx, dao.DB, docIDs, tenantIDs)+ documents, err := s.documentDAO.GetByIDs(ctx, dao.DB, docIDs)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 {+ if document == nil || !s.kbDAO.Accessible(ctx, dao.DB, document.KbID, userID) {continue}@@ -95,11 +86,7 @@ func (s *DocumentService) GetThumbnails(ctx context.Context, userID string, docIif strings.HasPrefix(*document.Thumbnail, imgBase64Prefix) {thumbnail = *document.Thumbnail} else {- thumbnail = fmt.Sprintf(- "/api/v1/documents/images/%s-%s",- document.KbID,- *document.Thumbnail,- )+ thumbnail = fmt.Sprintf("/api/v1/documents/%s/thumbnail", document.ID)}}diff --git a/internal/service/document/document_test.go b/internal/service/document/document_test.goindex 6b2351f305..6ada2f35af 100644--- a/internal/service/document/document_test.go+++ b/internal/service/document/document_test.go@@ -73,6 +73,7 @@ func (r *recordingTaskPublisher) PublishTaskMessage(subject string, msg common.Ttype fakeUploadStorage struct {objects map[string][]byteafterPut func()+ getCalls int}func newFakeUploadStorage() *fakeUploadStorage {@@ -90,12 +91,24 @@ func (f *fakeUploadStorage) Put(ctx context.Context, bucket, fnm string, binaryreturn nil}func (f *fakeUploadStorage) Get(ctx context.Context, bucket, fnm string, tenantID ...string) ([]byte, error) {+ f.getCalls++v, ok := f.objects[f.key(bucket, fnm)]if !ok {return nil, errors.New("not found")}return append([]byte(nil), v...), nil}++type imageOwnershipEngine struct {+ fakeChatDocEngine+ result *types.SearchResult+ request *types.SearchRequest+}++func (e *imageOwnershipEngine) Search(_ context.Context, req *types.SearchRequest) (*types.SearchResult, error) {+ e.request = req+ return e.result, nil+}func (f *fakeUploadStorage) Remove(ctx context.Context, bucket, fnm string, tenantID ...string) error {delete(f.objects, f.key(bucket, fnm))return nil@@ -3714,7 +3727,7 @@ func TestGetThumbnails_AlignsWithPythonFormatting(t *testing.T) {t.Fatalf("GetThumbnails failed: %v", err)}- if got["doc-file"] != "/api/v1/documents/images/kb-1-thumb.png" {+ if got["doc-file"] != "/api/v1/documents/doc-file/thumbnail" {t.Fatalf("unexpected file thumbnail: %q", got["doc-file"])}if got["doc-base64"] != base64Thumb {@@ -3728,6 +3741,102 @@ func TestGetThumbnails_AlignsWithPythonFormatting(t *testing.T) {}}+func TestGetDocumentImageRejectsUnindexedObjectBeforeStorage(t *testing.T) {+ ctx := t.Context()+ db := setupServiceTestDB(t)+ originalDB := dao.DB+ dao.DB = db+ t.Cleanup(func() { dao.DB = originalDB })++ kbID := strings.Repeat("a", 32)+ status := string(entity.StatusValid)+ if err := db.Create(&entity.Knowledgebase{ID: kbID, TenantID: "user-1", Name: "kb", EmbdID: "embd", Status: &status}).Error; err != nil {+ t.Fatal(err)+ }+ engine := &imageOwnershipEngine{result: &types.SearchResult{}}+ store := useFakeStorage(t)+ if err := store.Put(ctx, kbID, "raw.pdf", []byte("%PDF-1.7")); err != nil {+ t.Fatal(err)+ }+ svc := NewDocumentService()+ svc.docEngine = engine++ if _, err := svc.GetDocumentImage(ctx, "user-1", kbID+"-raw.pdf"); !errors.Is(err, ErrDocumentImageNotFound) {+ t.Fatalf("GetDocumentImage() error = %v", err)+ }+ if store.getCalls != 0 {+ t.Fatalf("storage Get called %d times after ownership denial", store.getCalls)+ }+}++func TestGetDocumentImageForDocumentAllowsIndexedSharedBucketImage(t *testing.T) {+ ctx := t.Context()+ db := setupServiceTestDB(t)+ originalDB := dao.DB+ dao.DB = db+ t.Cleanup(func() { dao.DB = originalDB })++ kbID := strings.Repeat("b", 32)+ status := string(entity.StatusValid)+ if err := db.Create(&entity.Knowledgebase{ID: kbID, TenantID: "user-1", Name: "kb", EmbdID: "embd", Status: &status}).Error; err != nil {+ t.Fatal(err)+ }+ if err := db.Create(&entity.Document{ID: "doc-1", KbID: kbID, CreatedBy: "user-1", ParserConfig: entity.JSONMap{}}).Error; err != nil {+ t.Fatal(err)+ }+ imageID := "imagetemps-page-1.png"+ engine := &imageOwnershipEngine{result: &types.SearchResult{Total: 1, Chunks: []map[string]any{{"doc_id": "doc-1", "img_id": imageID}}}}+ store := useFakeStorage(t)+ image := []byte("\x89PNG\r\n\x1a\nimage")+ if err := store.Put(ctx, "imagetemps", "page-1.png", image); err != nil {+ t.Fatal(err)+ }+ svc := NewDocumentService()+ svc.docEngine = engine++ got, err := svc.GetDocumentImageForDocument(ctx, "user-1", "doc-1", imageID)+ if err != nil {+ t.Fatalf("GetDocumentImageForDocument() error = %v", err)+ }+ if !bytes.Equal(got, image) {+ t.Fatalf("GetDocumentImageForDocument() = %q", got)+ }+ if engine.request == nil || engine.request.Filter["doc_id"] != "doc-1" || engine.request.Filter["img_id"] != imageID {+ t.Fatalf("ownership search = %#v", engine.request)+ }+}++func TestGetDocumentThumbnailAuthorizesBeforeStorage(t *testing.T) {+ ctx := t.Context()+ db := setupServiceTestDB(t)+ pushServiceDB(t, db)+ insertTestKB(t, "kb-thumb", "owner-1", 0, 0, 0)+ thumbnail := "thumbnail.png"+ if err := db.Create(&entity.Document{+ ID: "doc-thumb", KbID: "kb-thumb", Thumbnail: &thumbnail,+ ParserID: "naive", ParserConfig: entity.JSONMap{}, SourceType: "local", Type: "pdf", CreatedBy: "owner-1",+ }).Error; err != nil {+ t.Fatal(err)+ }+ store := useFakeStorage(t)+ image := []byte("\x89PNG\r\n\x1a\nimage")+ if err := store.Put(ctx, "kb-thumb", thumbnail, image); err != nil {+ t.Fatal(err)+ }+ svc := testDocumentService(t)++ if _, err := svc.GetDocumentThumbnail(ctx, "other-user", "doc-thumb"); !errors.Is(err, ErrDocumentImageNotFound) {+ t.Fatalf("unauthorized GetDocumentThumbnail() error = %v", err)+ }+ if store.getCalls != 0 {+ t.Fatalf("storage Get called %d times after authorization denial", store.getCalls)+ }+ got, err := svc.GetDocumentThumbnail(ctx, "owner-1", "doc-thumb")+ if err != nil || !bytes.Equal(got, image) {+ t.Fatalf("authorized GetDocumentThumbnail() = %q, %v", got, err)+ }+}+// TestStartParseDocuments_FailsBeforeClearing verifies that GetDocumentStorageAddress// runs before clearDocumentParseResults: when storage is invalid, we fail without// deleting existing parse results.diff --git a/test/testcases/restful_api/test_document_raw_routes.py b/test/testcases/restful_api/test_document_raw_routes.pyindex 254cb98d10..1fc24cc56c 100644--- a/test/testcases/restful_api/test_document_raw_routes.py+++ b/test/testcases/restful_api/test_document_raw_routes.py@@ -29,6 +29,21 @@ def test_document_image_invalid_id_contract(rest_client):assert payload["message"] == "Image not found.", payload+ "path",+ [+ "/documents/images/not-a-valid-image-id",+ "/documents/doc-1/images/imagetemps-page-1.png",+ "/documents/doc-1/thumbnail",+ ],+)+def test_private_document_images_require_auth(rest_client_noauth, path):+ res = rest_client_noauth.get(path)+ assert res.status_code == 401, res.text+ assert_auth_error(res.json(), "missing token")++@pytest.mark.p2def test_document_download_by_id_requires_auth(create_document):_dataset_id, document_id = create_document("document_raw_download_auth.txt")diff --git a/test/testcases/test_web_api/test_document_app/conftest.py b/test/testcases/test_web_api/test_document_app/conftest.pyindex ab4cbc0a57..cc67d82793 100644--- a/test/testcases/test_web_api/test_document_app/conftest.py+++ b/test/testcases/test_web_api/test_document_app/conftest.py@@ -117,6 +117,9 @@ def document_app_module(monkeypatch):deepdoc_mineru_module.MinerUParser = _StubMinerUParsermonkeypatch.setitem(sys.modules, "deepdoc.parser.mineru_parser", deepdoc_mineru_module)+ deepdoc_mistral_module = ModuleType("deepdoc.parser.mistral_parser")+ deepdoc_mistral_module.MistralParser = _StubMinerUParser+ monkeypatch.setitem(sys.modules, "deepdoc.parser.mistral_parser", deepdoc_mistral_module)deepdoc_paddleocr_module = ModuleType("deepdoc.parser.paddleocr_parser")class _StubPaddleOCRParser:@@ -125,11 +128,17 @@ def document_app_module(monkeypatch):deepdoc_paddleocr_module.PaddleOCRParser = _StubPaddleOCRParsermonkeypatch.setitem(sys.modules, "deepdoc.parser.paddleocr_parser", deepdoc_paddleocr_module)monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))+ rag_llm_cv_module = ModuleType("rag.llm.cv_model")+ rag_llm_cv_module.GptV4 = type("GptV4", (), {})+ monkeypatch.setitem(sys.modules, "rag.llm.cv_model", rag_llm_cv_module)stub_apps = ModuleType("api.apps")stub_apps.__path__ = [str(repo_root / "api" / "apps")]+ stub_apps.AUTH_JWT = "jwt"+ stub_apps.AUTH_API = "api"+ stub_apps.AUTH_BETA = "beta"stub_apps.current_user = SimpleNamespace(id="user-1")- stub_apps.login_required = lambda func: func+ stub_apps.login_required = lambda func=None, **_kwargs: (lambda wrapped: wrapped) if func is None else funcmonkeypatch.setitem(sys.modules, "api.apps", stub_apps)stub_apps_services = ModuleType("api.apps.services")@@ -197,6 +206,9 @@ def document_rest_api_module(monkeypatch):deepdoc_mineru_module.MinerUParser = _StubMinerUParsermonkeypatch.setitem(sys.modules, "deepdoc.parser.mineru_parser", deepdoc_mineru_module)+ deepdoc_mistral_module = ModuleType("deepdoc.parser.mistral_parser")+ deepdoc_mistral_module.MistralParser = _StubMinerUParser+ monkeypatch.setitem(sys.modules, "deepdoc.parser.mistral_parser", deepdoc_mistral_module)deepdoc_paddleocr_module = ModuleType("deepdoc.parser.paddleocr_parser")class _StubPaddleOCRParser:@@ -205,11 +217,17 @@ def document_rest_api_module(monkeypatch):deepdoc_paddleocr_module.PaddleOCRParser = _StubPaddleOCRParsermonkeypatch.setitem(sys.modules, "deepdoc.parser.paddleocr_parser", deepdoc_paddleocr_module)monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))+ rag_llm_cv_module = ModuleType("rag.llm.cv_model")+ rag_llm_cv_module.GptV4 = type("GptV4", (), {})+ monkeypatch.setitem(sys.modules, "rag.llm.cv_model", rag_llm_cv_module)stub_apps = ModuleType("api.apps")stub_apps.__path__ = [str(repo_root / "api" / "apps")]+ stub_apps.AUTH_JWT = "jwt"+ stub_apps.AUTH_API = "api"+ stub_apps.AUTH_BETA = "beta"stub_apps.current_user = SimpleNamespace(id="user-1")- stub_apps.login_required = lambda func: func+ stub_apps.login_required = lambda func=None, **_kwargs: (lambda wrapped: wrapped) if func is None else funcmonkeypatch.setitem(sys.modules, "api.apps", stub_apps)stub_apps_services = ModuleType("api.apps.services")diff --git a/test/testcases/test_web_api/test_document_app/test_document_metadata.py b/test/testcases/test_web_api/test_document_app/test_document_metadata.pyindex eb49e9294e..23c157a1ec 100644--- a/test/testcases/test_web_api/test_document_app/test_document_metadata.py+++ b/test/testcases/test_web_api/test_document_app/test_document_metadata.py@@ -458,82 +458,6 @@ class TestDocumentMetadataUnit:assert res["code"] == RetCode.DATA_ERRORassert "document not found" in res["message"]- @pytest.mark.p2- def test_get_document_image_content_type_from_object_extension_unit(self, document_app_module, monkeypatch):- module = document_app_module-- class _Headers(dict):- def set(self, key, value):- self[key] = value-- class _ImageResponse:- def __init__(self, data):- self.data = data- self.headers = _Headers()-- png_bytes = (- b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01"- b"\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"- b"\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01"- b"\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82"- )-- async def fake_thread_pool_exec(*_args, **_kwargs):- return png_bytes-- async def fake_make_response(data):- return _ImageResponse(data)-- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)- monkeypatch.setattr(module, "make_response", fake_make_response)- res = _run(module.get_document_image("kb1-object.png"))- assert isinstance(res, _ImageResponse)- assert res.headers["Content-Type"] == "image/png"-- @pytest.mark.p2- def test_get_document_image_content_type_from_magic_bytes_unit(self, document_app_module, monkeypatch):- module = document_app_module-- class _Headers(dict):- def set(self, key, value):- self[key] = value-- class _ImageResponse:- def __init__(self, data):- self.data = data- self.headers = _Headers()-- png_bytes = (- b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01"- b"\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"- b"\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01"- b"\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82"- )-- async def fake_thread_pool_exec(*_args, **_kwargs):- return png_bytes-- async def fake_make_response(data):- return _ImageResponse(data)-- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)- monkeypatch.setattr(module, "make_response", fake_make_response)- res = _run(module.get_document_image("kb1-a1b2c3d4e5f6"))- assert isinstance(res, _ImageResponse)- assert res.headers["Content-Type"] == "image/png"-- @pytest.mark.p2- def test_get_document_image_missing_blob_unit(self, document_app_module, monkeypatch):- module = document_app_module-- async def fake_thread_pool_exec(*_args, **_kwargs):- return None-- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)- res = _run(module.get_document_image("kb1-object-key"))- assert res["code"] == RetCode.DATA_ERROR- assert res["message"] == "Image not found."-@pytest.mark.p2def test_get_preview_missing_blob_unit(self, document_app_module, monkeypatch):module = document_app_module@@ -553,85 +477,6 @@ class TestDocumentMetadataUnit:assert res["code"] == RetCode.DATA_ERRORassert res["message"] == "This file is empty."- @pytest.mark.skip(reason="Moved to /api/v1/documents/images/<image_id>")- def test_get_image_success_and_exception_unit(self, document_app_module, monkeypatch):- module = document_app_module-- class _Headers(dict):- def set(self, key, value):- self[key] = value-- class _ImageResponse:- def __init__(self, data):- self.data = data- self.headers = _Headers()-- async def fake_thread_pool_exec(*_args, **_kwargs):- return b"image-bytes"-- async def fake_make_response(data):- return _ImageResponse(data)-- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)- monkeypatch.setattr(module, "make_response", fake_make_response)- monkeypatch.setattr(module.settings, "STORAGE_IMPL", SimpleNamespace(get=lambda *_args, **_kwargs: b"image-bytes"))- res = _run(module.get_image("bucket-name"))- assert isinstance(res, _ImageResponse)- assert res.data == b"image-bytes"- assert res.headers["Content-Type"] == "image/JPEG"-- async def raise_error(*_args, **_kwargs):- raise RuntimeError("image boom")-- monkeypatch.setattr(module, "thread_pool_exec", raise_error)- monkeypatch.setattr(module, "server_error_response", lambda e: {"code": 500, "message": str(e)})- res = _run(module.get_image("bucket-name"))- assert res["code"] == 500- assert "image boom" in res["message"]-- def test_get_document_image_hyphenated_object_key(self, document_app_module, monkeypatch):- """Hyphenated thumbnail keys are parsed with split('-', 1) and return correct MIME type."""- module = document_app_module-- class _Headers(dict):- def set(self, key, value):- self[key] = value-- class _ImageResponse:- def __init__(self, data):- self.data = data- self.headers = _Headers()-- storage_calls = []-- def _storage_get(bkt, nm):- storage_calls.append((bkt, nm))- return b"png-bytes"-- async def fake_thread_pool_exec(fn, *args, **kwargs):- return fn(*args, **kwargs)-- async def fake_make_response(data):- return _ImageResponse(data)-- monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)- monkeypatch.setattr(module, "make_response", fake_make_response)- monkeypatch.setattr(- module.settings,- "STORAGE_IMPL",- SimpleNamespace(get=_storage_get),- )-- image_id = "kb12345678901234567890123456789012-page-1.png"- res = _run(module.get_document_image(image_id))- assert isinstance(res, _ImageResponse)- assert storage_calls == [("kb12345678901234567890123456789012", "page-1.png")]- assert res.headers["Content-Type"] == "image/png"-- res = _run(module.get_document_image("only-one-part"))- assert res["code"] == RetCode.DATA_ERROR- assert "Image not found" in res["message"]-@pytest.mark.p2def test_get_artifact_denied_without_session_reference_unit(self, document_app_module, monkeypatch):module = document_app_modulediff --git a/web/src/components/floating-chat-widget-markdown.tsx b/web/src/components/floating-chat-widget-markdown.tsxindex 0a46830b49..ae6a839cf6 100644--- a/web/src/components/floating-chat-widget-markdown.tsx+++ b/web/src/components/floating-chat-widget-markdown.tsx@@ -199,12 +199,14 @@ const FloatingChatWidgetMarkdown = ({<TooltipTrigger asChild><Imageid={imageId}+ documentId={documentId}className="w-24 h-24 object-contain rounded m-1 cursor-pointer"/></TooltipTrigger><TooltipContent side="left"><Imageid={imageId}+ documentId={documentId}className="max-w-[80vw] max-h-[60vh] rounded"/></TooltipContent>@@ -292,6 +294,7 @@ const FloatingChatWidgetMarkdown = ({<Imagekey={`img-${i}`}id={imageId}+ documentId={documentId}className="block object-contain max-w-full max-h-48 rounded my-2 cursor-pointer"onClick={handleDocumentButtonClick(documentId,diff --git a/web/src/components/image/index.tsx b/web/src/components/image/index.tsxindex e8d71c85d2..0a01c7a567 100644--- a/web/src/components/image/index.tsx+++ b/web/src/components/image/index.tsx@@ -23,6 +23,7 @@ import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover';interface IImage extends React.ImgHTMLAttributes<HTMLImageElement> {id: string;+ documentId?: string;t?: string | number;label?: string;}@@ -36,7 +37,11 @@ type ImageCacheItem = {const imageCache = new Map<string, ImageCacheItem>();-export const buildDocumentImageUrl = (id: string, t?: string | number) => {+export const buildDocumentImageUrl = (+ id: string,+ documentId?: string,+ t?: string | number,+) => {const params = new URLSearchParams();if (t) {@@ -44,7 +49,10 @@ export const buildDocumentImageUrl = (id: string, t?: string | number) => {}const query = params.toString();- return `${restAPIv1}/documents/images/${id}${query ? `?${query}` : ''}`;+ const path = documentId+ ? `/documents/${encodeURIComponent(documentId)}/images/${encodeURIComponent(id)}`+ : `/documents/images/${encodeURIComponent(id)}`;+ return `${restAPIv1}${path}${query ? `?${query}` : ''}`;};const fetchDocumentImage = (url: string, authorization: string) => {@@ -114,8 +122,15 @@ const isAuthRequiredUrl = (url: string): boolean => {}};-export const useDocumentImageUrl = (id: string, t?: string | number) => {- const directUrl = useMemo(() => buildDocumentImageUrl(id, t), [id, t]);+export const useDocumentImageUrl = (+ id: string,+ documentId?: string,+ t?: string | number,+) => {+ const directUrl = useMemo(+ () => buildDocumentImageUrl(id, documentId, t),+ [documentId, id, t],+ );const [imageUrl, setImageUrl] = useState<string>('');useEffect(() => {@@ -214,10 +229,10 @@ export const AuthenticatedImg = ({};const Image = React.forwardRef<HTMLImageElement, IImage>(function Image(- { id, t, label, className, ...props },+ { id, documentId, t, label, className, ...props },ref,) {- const src = useDocumentImageUrl(id, t);+ const src = useDocumentImageUrl(id, documentId, t);const imageElement = (<img{...props}@@ -243,14 +258,28 @@ const Image = React.forwardRef<HTMLImageElement, IImage>(function Image(export default Image;-export const ImageWithPopover = ({ id }: { id: string }) => {+export const ImageWithPopover = ({+ id,+ documentId,+}: {+ id: string;+ documentId?: string;+}) => {return (<Popover><PopoverTrigger>- <Image id={id} className="max-h-[100px] inline-block"></Image>+ <Image+ id={id}+ documentId={documentId}+ className="max-h-[100px] inline-block"+ ></Image></PopoverTrigger><PopoverContent>- <Image id={id} className="max-w-[100px] object-contain"></Image>+ <Image+ id={id}+ documentId={documentId}+ className="max-w-[100px] object-contain"+ ></Image></PopoverContent></Popover>);diff --git a/web/src/components/markdown-content/image-carousel.tsx b/web/src/components/markdown-content/image-carousel.tsxindex 014029abcc..ab90ad84fe 100644--- a/web/src/components/markdown-content/image-carousel.tsx+++ b/web/src/components/markdown-content/image-carousel.tsx@@ -92,7 +92,6 @@ export const ImageCarousel = ({const handleImageClick = useCallback((- imageId: string,chunkItem: IReferenceChunk,documentId: string,fileExtension: string,@@ -127,11 +126,11 @@ export const ImageCarousel = ({<section><Imageid={imageId!}+ documentId={documentId}className="object-contain max-h-36"onClick={documentId && chunkItem? handleImageClick(- imageId!,chunkItem,documentId,fileExtension!,diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsxindex a16266c777..b9c80102c7 100644--- a/web/src/components/markdown-content/index.tsx+++ b/web/src/components/markdown-content/index.tsx@@ -253,12 +253,14 @@ const MarkdownContent = ({<HoverCardTrigger><Imageid={imageId}+ documentId={documentId}className={styles.referenceChunkImage}></Image></HoverCardTrigger><HoverCardContent><Imageid={imageId}+ documentId={documentId}className={styles.referenceImagePreview}></Image></HoverCardContent>@@ -343,7 +345,7 @@ const MarkdownContent = ({return replacedText;},- [getPopoverContent, t],+ [getPopoverContent],);const dir = getDirAttribute(content.replace(citationMarkerReg, ''));diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsxindex ff92dab51f..ffa818961d 100644--- a/web/src/components/next-markdown-content/index.tsx+++ b/web/src/components/next-markdown-content/index.tsx@@ -319,12 +319,14 @@ function MarkdownContent({<HoverCardTrigger><Imageid={imageId}+ documentId={documentId}className={styles.referenceChunkImage}></Image></HoverCardTrigger><HoverCardContent><Imageid={imageId}+ documentId={documentId}className={cn(styles.referenceImagePreview)}></Image></HoverCardContent>@@ -404,7 +406,7 @@ function MarkdownContent({return replacedText;},- [renderPopoverContent, t],+ [renderPopoverContent],);const dir = getDirAttribute(content?.replace(currentReg, ''));diff --git a/web/src/components/next-message-item/reference-image-list.tsx b/web/src/components/next-message-item/reference-image-list.tsxindex aa417fcb0f..79fce133e3 100644--- a/web/src/components/next-message-item/reference-image-list.tsx+++ b/web/src/components/next-message-item/reference-image-list.tsx@@ -26,7 +26,6 @@ import { IReferenceChunk } from '@/interfaces/database/chat';import { isPlainObject } from 'lodash';import { RotateCw, ZoomIn, ZoomOut } from 'lucide-react';import { useMemo } from 'react';-import { useTranslation } from 'react-i18next';import { PhotoProvider, PhotoView } from 'react-photo-view';import { extractNumbersFromMessageContent } from './utils';@@ -37,6 +36,7 @@ type IProps = {type ImageItem = {id: string;+ documentId: string;index: number;};@@ -51,14 +51,14 @@ const getButtonVisibilityClass = (imageCount: number) => {return map[imageCount] || (imageCount >= 6 ? '@2xl:hidden' : '');};-function ImagePhotoView({ id, index }: ImageItem) {- const src = useDocumentImageUrl(id);- const { t } = useTranslation();+function ImagePhotoView({ id, documentId, index }: ImageItem) {+ const src = useDocumentImageUrl(id, documentId);return (<PhotoView src={src}><Imageid={id}+ documentId={documentId}className="h-40 w-full"label={`[${index + 1}]`}/>@@ -99,7 +99,7 @@ function ImageCarousel({ images }: { images: ImageItem[] }) {}}><CarouselContent>- {images.map(({ id, index }) => (+ {images.map(({ id, documentId, index }) => (<CarouselItemkey={index}className="@@ -110,7 +110,11 @@ function ImageCarousel({ images }: { images: ImageItem[] }) {@2xl:basis-1/6">- <ImagePhotoView id={id} index={index}></ImagePhotoView>+ <ImagePhotoView+ id={id}+ documentId={documentId}+ index={index}+ ></ImagePhotoView></CarouselItem>))}</CarouselContent>@@ -129,15 +133,26 @@ export function ReferenceImageList({const images = useMemo(() => {if (Array.isArray(referenceChunks)) {return referenceChunks- .map((chunk, idx) => ({ id: chunk.image_id, index: idx }))- .filter((item, idx) => allChunkIndexes.includes(idx) && item.id);+ .map((chunk, idx) => ({+ id: chunk.image_id,+ documentId: chunk.document_id,+ index: idx,+ }))+ .filter(+ (item, idx) =>+ allChunkIndexes.includes(idx) && item.id && item.documentId,+ );}if (isPlainObject(referenceChunks)) {return Object.entries(referenceChunks || {}).reduce<ImageItem[]>((pre, [idx, chunk]) => {if (allChunkIndexes.includes(Number(idx)) && chunk.image_id) {- return pre.concat({ id: chunk.image_id, index: Number(idx) });+ return pre.concat({+ id: chunk.image_id,+ documentId: chunk.document_id,+ index: Number(idx),+ });}return pre;},diff --git a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-card/index.tsx b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-card/index.tsxindex 94556b6b29..48c9c00d82 100644--- a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-card/index.tsx+++ b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-card/index.tsx@@ -101,6 +101,7 @@ const ChunkCard = ({<Imaget={imageCacheKey}id={item.image_id}+ documentId={item.doc_id}className="mt-1 rounded !w-28 object-contain"/></TooltipTrigger>@@ -115,6 +116,7 @@ const ChunkCard = ({<Imaget={imageCacheKey}id={item.image_id}+ documentId={item.doc_id}className="size-full max-w-[50vw] max-h-[50vh] object-contain"/></TooltipContent>diff --git a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsxindex be51d58232..c82fe841fe 100644--- a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx+++ b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx@@ -185,6 +185,7 @@ const ChunkCreatingModal: React.FC<IModalProps<any> & kFProps> = ({{data?.data?.img_id && (<Imageid={data?.data?.img_id}+ documentId={doc_id}className="mx-auto w-auto max-w-full object-contain max-h-[800px]"/>)}diff --git a/web/src/pages/dataflow-result/components/chunk-card/index.tsx b/web/src/pages/dataflow-result/components/chunk-card/index.tsxindex 42862a042c..3a290b48a0 100644--- a/web/src/pages/dataflow-result/components/chunk-card/index.tsx+++ b/web/src/pages/dataflow-result/components/chunk-card/index.tsx@@ -77,7 +77,11 @@ const ChunkCard = ({onMouseLeave={() => setOpen(false)}><div>- <Image id={item.image_id} className={styles.image}></Image>+ <Image+ id={item.image_id}+ documentId={item.doc_id}+ className={styles.image}+ ></Image></div></PopoverTrigger><PopoverContent@@ -89,6 +93,7 @@ const ChunkCard = ({<div><Imageid={item.image_id}+ documentId={item.doc_id}className={styles.imagePreview}></Image></div>diff --git a/web/src/pages/next-search/markdown-content/index.tsx b/web/src/pages/next-search/markdown-content/index.tsxindex 8fe91119c4..4266b00955 100644--- a/web/src/pages/next-search/markdown-content/index.tsx+++ b/web/src/pages/next-search/markdown-content/index.tsx@@ -189,12 +189,14 @@ const MarkdownContent = ({<PopoverTrigger><Imageid={imageId}+ documentId={documentId}className={styles.referenceChunkImage}></Image></PopoverTrigger><PopoverContent><Imageid={imageId}+ documentId={documentId}className={styles.referenceImagePreview}></Image></PopoverContent>@@ -266,7 +268,7 @@ const MarkdownContent = ({return replacedText;},- [getPopoverContent, t],+ [getPopoverContent],);const dir = getDirAttribute(content.replace(citationMarkerReg, ''));diff --git a/web/src/pages/next-search/search-view.tsx b/web/src/pages/next-search/search-view.tsxindex 3d7d983c17..147f950199 100644--- a/web/src/pages/next-search/search-view.tsx+++ b/web/src/pages/next-search/search-view.tsx@@ -254,6 +254,7 @@ export default function SearchingView({{chunk.image_id && (<ImageWithPopoverid={chunk.image_id}+ documentId={chunk.document_id}></ImageWithPopover>)}<div