diff --git a/backend/app/services/logistics_service.py b/backend/app/services/logistics_service.py index 1ce26d7..ef3d6e9 100644 --- a/backend/app/services/logistics_service.py +++ b/backend/app/services/logistics_service.py @@ -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")