fix: 本地上传图片OCR时直接读取本地文件,避免HTTP 404
本地上传的图片URL(/uploads/路径)直接从local_upload_dir读取, 不再通过公网域名下载,解决后端无法访问自己域名导致404的问题。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
11edb1de94
commit
2ded5d21e9
@ -825,7 +825,8 @@ class LogisticsService:
|
||||
"""
|
||||
import base64 as _b64
|
||||
import hashlib as _md5
|
||||
from urllib.parse import urlencode
|
||||
from pathlib import Path as _Path, PurePosixPath as _URLPath
|
||||
from urllib.parse import urlencode, urlparse
|
||||
|
||||
image_url = (payload.get("image_url") or "").strip()
|
||||
if not image_url:
|
||||
@ -842,11 +843,18 @@ class LogisticsService:
|
||||
# 1. 下载图片并 base64 编码
|
||||
logger.info("[recognize_waybill] 下载图片: %s", image_url)
|
||||
try:
|
||||
with request.urlopen(image_url, timeout=15) as resp:
|
||||
image_bytes = resp.read()
|
||||
logger.info("[recognize_waybill] 图片下载成功, 大小: %d bytes", len(image_bytes))
|
||||
url_path = _URLPath(urlparse(image_url).path)
|
||||
if len(url_path.parts) >= 2 and url_path.parts[1] == "uploads":
|
||||
# 本地上传的文件,直接从本地文件系统读取
|
||||
local_file = _Path(settings.local_upload_dir) / url_path.name
|
||||
logger.info("[recognize_waybill] 本地文件读取: %s", local_file)
|
||||
image_bytes = local_file.read_bytes()
|
||||
else:
|
||||
with request.urlopen(image_url, timeout=15) as resp:
|
||||
image_bytes = resp.read()
|
||||
logger.info("[recognize_waybill] 图片读取成功, 大小: %d bytes", len(image_bytes))
|
||||
except Exception as img_exc:
|
||||
logger.warning("[recognize_waybill] 图片下载失败: %s", img_exc)
|
||||
logger.warning("[recognize_waybill] 图片读取失败: %s", img_exc)
|
||||
raise
|
||||
image_b64 = _b64.b64encode(image_bytes).decode("utf-8")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user