|
@@ -47,6 +47,7 @@ async def upload_image_for_card(
|
|
|
file_extension = os.path.splitext(image.filename)[1]
|
|
file_extension = os.path.splitext(image.filename)[1]
|
|
|
unique_filename = f"{uuid.uuid4()}{file_extension}"
|
|
unique_filename = f"{uuid.uuid4()}{file_extension}"
|
|
|
image_path = settings.DATA_DIR / unique_filename
|
|
image_path = settings.DATA_DIR / unique_filename
|
|
|
|
|
+ relative_path = f"/{image_path.parent.name}/{image_path.name}"
|
|
|
try:
|
|
try:
|
|
|
with open(image_path, "wb") as buffer:
|
|
with open(image_path, "wb") as buffer:
|
|
|
buffer.write(await image.read())
|
|
buffer.write(await image.read())
|
|
@@ -69,7 +70,7 @@ async def upload_image_for_card(
|
|
|
"VALUES (%s, %s, %s, %s, %s)"
|
|
"VALUES (%s, %s, %s, %s, %s)"
|
|
|
)
|
|
)
|
|
|
params = (
|
|
params = (
|
|
|
- card_id, image_type.value, image_name, str(image_path), json.dumps(detection_json, ensure_ascii=False))
|
|
|
|
|
|
|
+ card_id, image_type.value, image_name, relative_path, json.dumps(detection_json, ensure_ascii=False))
|
|
|
|
|
|
|
|
cursor.execute(query_insert_image, params)
|
|
cursor.execute(query_insert_image, params)
|
|
|
new_id = cursor.lastrowid
|
|
new_id = cursor.lastrowid
|
|
@@ -171,11 +172,13 @@ def get_image_file(id: int, db_conn: PooledMySQLConnection = db_dependency):
|
|
|
raise HTTPException(status_code=404, detail=f"ID为 {id} 的记录未找到。")
|
|
raise HTTPException(status_code=404, detail=f"ID为 {id} 的记录未找到。")
|
|
|
|
|
|
|
|
image_path = result[0]
|
|
image_path = result[0]
|
|
|
- if not os.path.exists(image_path):
|
|
|
|
|
- logger.error(f"文件在服务器上未找到: {image_path} (数据库ID: {id})")
|
|
|
|
|
|
|
+ absolute_path = settings.BASE_PATH / image_path.lstrip('/\\')
|
|
|
|
|
+
|
|
|
|
|
+ if not os.path.exists(absolute_path):
|
|
|
|
|
+ logger.error(f"文件在服务器上未找到: {absolute_path} (数据库ID: {id})")
|
|
|
raise HTTPException(status_code=404, detail="图片文件在服务器上不存在。")
|
|
raise HTTPException(status_code=404, detail="图片文件在服务器上不存在。")
|
|
|
|
|
|
|
|
- return FileResponse(image_path)
|
|
|
|
|
|
|
+ return FileResponse(absolute_path)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error(f"获取图片失败 ({id}): {e}")
|
|
logger.error(f"获取图片失败 ({id}): {e}")
|
|
|
if isinstance(e, HTTPException): raise e
|
|
if isinstance(e, HTTPException): raise e
|