mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-04 23:00:30 +08:00
### What problem does this PR solve? Fixes #15117. Chunk images are stored with `img_id = f"{bucket}-{objname}"` in `image2id()` (`rag/utils/base64_image.py`). When loading via `id2image()`, the code used `image_id.split("-")` and required exactly two segments. Object keys that contain hyphens (e.g. `page-1.jpg`) produce more than two segments, so `id2image` returns `None` and chunk image previews fail even though the blob exists. This is the same parsing issue as #15115 (HTTP thumbnail route); this PR fixes the indexing/retrieval path. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [ ] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe): ### Test plan - [x] `pytest test/unit_test/rag/utils/test_base64_image.py` - [ ] Manual: index a chunk with an `objname` containing hyphens and confirm `img_id` resolves to an image in retrieval Fixes #15117.
This commit is contained in:
@@ -93,13 +93,41 @@ async def image2id(d: dict, storage_put_func: partial, objname: str, bucket: str
|
||||
del d["image"]
|
||||
|
||||
|
||||
def parse_storage_composite_id(composite_id: str) -> tuple[str, str] | None:
|
||||
"""Split a ``{bucket}-{object_key}`` storage ID on the first hyphen only.
|
||||
|
||||
``image2id`` stores ``img_id`` as ``f"{bucket}-{objname}"``. The object key
|
||||
may contain additional hyphens (e.g. ``page-1.jpg``).
|
||||
|
||||
Args:
|
||||
composite_id: Composite storage identifier.
|
||||
|
||||
Returns:
|
||||
``(bucket, object_key)`` when valid, otherwise ``None``.
|
||||
"""
|
||||
parts = composite_id.split("-", 1)
|
||||
if len(parts) != 2 or not parts[0] or not parts[1] or composite_id.endswith("-"):
|
||||
return None
|
||||
return parts[0], parts[1]
|
||||
|
||||
|
||||
def id2image(image_id: str | None, storage_get_func: partial):
|
||||
"""Load a PIL image from storage using a composite ``img_id``.
|
||||
|
||||
Args:
|
||||
image_id: Value produced by ``image2id`` (``{bucket}-{object_key}``).
|
||||
storage_get_func: Callable ``(bucket=, fnm=)`` returning raw bytes.
|
||||
|
||||
Returns:
|
||||
A PIL ``Image`` instance, or ``None`` when the ID is invalid or load fails.
|
||||
"""
|
||||
if not image_id:
|
||||
return
|
||||
arr = image_id.split("-")
|
||||
if len(arr) != 2:
|
||||
parsed = parse_storage_composite_id(image_id)
|
||||
if not parsed:
|
||||
logging.debug("Invalid image_id composite format: %s", image_id)
|
||||
return
|
||||
bkt, nm = image_id.split("-")
|
||||
bkt, nm = parsed
|
||||
try:
|
||||
blob = storage_get_func(bucket=bkt, fnm=nm)
|
||||
if not blob:
|
||||
|
||||
Reference in New Issue
Block a user