Spaces:
Paused
Paused
Update src/app.py
Browse files- src/app.py +21 -2
src/app.py
CHANGED
|
@@ -90,7 +90,14 @@ async def startup_event():
|
|
| 90 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin"), exist_ok=True)
|
| 91 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin/js"), exist_ok=True)
|
| 92 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin/css"), exist_ok=True)
|
|
|
|
|
|
|
| 93 |
os.makedirs(Config.IMAGE_SAVE_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# 输出图片保存目录的信息
|
| 96 |
logger.info(f"图片保存目录: {Config.IMAGE_SAVE_DIR}")
|
|
@@ -116,6 +123,13 @@ async def shutdown_event():
|
|
| 116 |
# 挂载静态文件目录
|
| 117 |
app.mount("/static", StaticFiles(directory=Config.STATIC_DIR), name="static")
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# 通用图片访问路由 - 支持多种路径格式
|
| 120 |
@app.get("/images/{filename}")
|
| 121 |
@app.get("/static/images/{filename}")
|
|
@@ -126,8 +140,13 @@ async def get_image(filename: str):
|
|
| 126 |
if os.path.exists(file_path):
|
| 127 |
return FileResponse(file_path)
|
| 128 |
else:
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# 添加静态文件路径前缀的兼容路由
|
| 133 |
if Config.STATIC_PATH_PREFIX:
|
|
|
|
| 90 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin"), exist_ok=True)
|
| 91 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin/js"), exist_ok=True)
|
| 92 |
os.makedirs(os.path.join(Config.STATIC_DIR, "admin/css"), exist_ok=True)
|
| 93 |
+
|
| 94 |
+
# 确保图片保存目录存在
|
| 95 |
os.makedirs(Config.IMAGE_SAVE_DIR, exist_ok=True)
|
| 96 |
+
logger.info(f"已确保图片保存目录存在: {Config.IMAGE_SAVE_DIR}")
|
| 97 |
+
|
| 98 |
+
# 确保/tmp目录的api_keys.json父目录存在
|
| 99 |
+
os.makedirs(os.path.dirname(Config.KEYS_STORAGE_FILE), exist_ok=True)
|
| 100 |
+
logger.info(f"已确保API密钥存储目录存在: {os.path.dirname(Config.KEYS_STORAGE_FILE)}")
|
| 101 |
|
| 102 |
# 输出图片保存目录的信息
|
| 103 |
logger.info(f"图片保存目录: {Config.IMAGE_SAVE_DIR}")
|
|
|
|
| 123 |
# 挂载静态文件目录
|
| 124 |
app.mount("/static", StaticFiles(directory=Config.STATIC_DIR), name="static")
|
| 125 |
|
| 126 |
+
# 挂载图片目录,确保能访问/tmp/images中的图片
|
| 127 |
+
try:
|
| 128 |
+
app.mount("/images", StaticFiles(directory=Config.IMAGE_SAVE_DIR), name="images")
|
| 129 |
+
logger.info(f"已挂载图片目录: {Config.IMAGE_SAVE_DIR} -> /images")
|
| 130 |
+
except Exception as e:
|
| 131 |
+
logger.error(f"挂载图片目录失败: {str(e)}")
|
| 132 |
+
|
| 133 |
# 通用图片访问路由 - 支持多种路径格式
|
| 134 |
@app.get("/images/{filename}")
|
| 135 |
@app.get("/static/images/{filename}")
|
|
|
|
| 140 |
if os.path.exists(file_path):
|
| 141 |
return FileResponse(file_path)
|
| 142 |
else:
|
| 143 |
+
# 尝试从静态目录获取(向后兼容)
|
| 144 |
+
alt_path = os.path.join(Config.STATIC_DIR, "images", filename)
|
| 145 |
+
if os.path.exists(alt_path):
|
| 146 |
+
return FileResponse(alt_path)
|
| 147 |
+
else:
|
| 148 |
+
logger.warning(f"请求的图片不存在: {file_path} 或 {alt_path}")
|
| 149 |
+
raise HTTPException(status_code=404, detail="图片不存在")
|
| 150 |
|
| 151 |
# 添加静态文件路径前缀的兼容路由
|
| 152 |
if Config.STATIC_PATH_PREFIX:
|