fix: bugs-04

This commit is contained in:
2026-03-17 22:39:05 +08:00
parent 143a1b4507
commit cdc2c4ead6
50 changed files with 1694 additions and 1916 deletions

View File

@@ -15,11 +15,13 @@ from app.schemas.image import (
ImageOut,
ImageDetailOut,
OcrFieldCorrection,
OcrBlockUpdateIn,
CaseOcrStartIn,
CaseImagesDeleteIn,
)
from app.utils.hash import sha256_file
from app.utils.file_storage import save_upload
from app.models.ocr_block import OcrBlock
router = APIRouter()
@@ -149,6 +151,50 @@ async def correct_ocr(
return {"message": "修正已保存", "corrections": len(corrections)}
@router.patch("/images/{image_id}/ocr-blocks")
async def update_ocr_blocks(
image_id: UUID,
blocks: list[OcrBlockUpdateIn],
db: AsyncSession = Depends(get_db),
):
repo = ImageRepository(db)
image = await repo.get(image_id)
if not image:
raise HTTPException(404, "截图不存在")
existing = sorted(list(image.ocr_blocks), key=lambda b: b.seq_order)
total = len(blocks)
if total == 0:
return {"message": "未提交可保存内容", "updated": 0}
# Update existing blocks first.
min_len = min(len(existing), total)
for idx in range(min_len):
existing[idx].content = blocks[idx].content
existing[idx].confidence = blocks[idx].confidence
existing[idx].seq_order = idx
# Create extra blocks if payload has more.
for idx in range(min_len, total):
db.add(
OcrBlock(
image_id=image_id,
content=blocks[idx].content,
bbox={},
seq_order=idx,
confidence=blocks[idx].confidence,
)
)
# Remove extra old blocks if payload has fewer.
for idx in range(total, len(existing)):
await db.delete(existing[idx])
await db.flush()
await db.commit()
return {"message": f"已保存 {total} 条OCR内容", "updated": total}
@router.get("/images/{image_id}/file")
async def get_image_file(image_id: UUID, db: AsyncSession = Depends(get_db)):
repo = ImageRepository(db)