|
1 | 1 | import logging |
2 | | -from pathlib import Path |
3 | 2 |
|
4 | 3 | from fastapi import APIRouter, Depends, HTTPException |
5 | | -from fastapi.responses import FileResponse, JSONResponse |
| 4 | +from fastapi.responses import JSONResponse |
6 | 5 | from sqlalchemy.util.concurrency import asyncio |
7 | 6 |
|
8 | 7 | from module.database import Database, engine |
9 | 8 | from module.manager import BangumiManager |
10 | 9 | from module.models import APIResponse, Bangumi, BangumiUpdate, ResponseModel |
11 | | -from module.network import load_image |
12 | 10 | from module.security.api import get_current_user |
13 | 11 |
|
14 | 12 | from .response import u_response |
|
17 | 15 | logger = logging.getLogger(__name__) |
18 | 16 |
|
19 | 17 |
|
20 | | -# def str_to_list(data: Bangumi): |
21 | | -# data.exclude_filter = data.exclude_filter.split(",") if data.exclude_filter else [] |
22 | | -# data.include_filter = data.include_filter.split(",") if data.include_filter else [] |
23 | | -# data.rss_link = data.rss_link.split(",") if data.rss_link else [] |
24 | | -# return data |
25 | 18 |
|
26 | 19 |
|
27 | 20 | @router.get( |
@@ -233,46 +226,3 @@ async def reset_all(): |
233 | 226 | ) |
234 | 227 |
|
235 | 228 |
|
236 | | -@router.get("/poster/{path:path}", dependencies=[Depends(get_current_user)]) |
237 | | -async def get_poster(path: str): |
238 | | - """ |
239 | | - 安全的poster图片访问端点 |
240 | | - - 添加了用户鉴权 |
241 | | - - 防止路径遍历攻击 |
242 | | - - 限制只能访问posters目录下的文件 |
243 | | - """ |
244 | | - # 验证路径安全性 - 阻止路径遍历 |
245 | | - if ".." in path or path.startswith("/") or "\\" in path: |
246 | | - logger.warning(f"[Poster] Blocked path traversal attempt: {path}") |
247 | | - raise HTTPException(status_code=400, detail="Invalid path") |
248 | | - |
249 | | - # 构建安全的文件路径 |
250 | | - poster_dir = Path("data/posters") |
251 | | - post_path = poster_dir / Path(path) |
252 | | - |
253 | | - # 确保解析后的路径仍在预期目录内 |
254 | | - try: |
255 | | - post_path.resolve().relative_to(poster_dir.resolve()) |
256 | | - except ValueError: |
257 | | - logger.warning(f"[Poster] Path outside allowed directory: {path}") |
258 | | - raise HTTPException(status_code=400, detail="Path outside allowed directory") |
259 | | - |
260 | | - logger.debug(f"[Poster] Accessing poster: {post_path}") |
261 | | - |
262 | | - # 如果文件不存在,尝试下载 |
263 | | - if not post_path.exists(): |
264 | | - try: |
265 | | - await load_image(path) |
266 | | - except Exception as e: |
267 | | - logger.warning(f"[Poster] Failed to load image {path}: {e}") |
268 | | - |
269 | | - # 返回文件 |
270 | | - if post_path.exists() and post_path.is_file(): |
271 | | - return FileResponse( |
272 | | - post_path, |
273 | | - media_type="image/jpeg", |
274 | | - headers={"Cache-Control": "public, max-age=86400"}, # 缓存1天 |
275 | | - ) |
276 | | - else: |
277 | | - logger.warning(f"[Poster] File not found: {post_path}") |
278 | | - raise HTTPException(status_code=404, detail="Poster not found") |
0 commit comments