common.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from app.core.logger import logger
  2. from fastapi import APIRouter, Response, Query, Depends, UploadFile, File, Request
  3. from fastapi.responses import RedirectResponse
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from app.core.database import get_db
  6. from app.utils.response import success
  7. from app.schemas.common import ApiResponse
  8. from app.schemas.http_session import HttpSessionCreate, HttpSessionOut
  9. from app.schemas.llm import ParseUserInputsOut, ParseUserInputsPayload, ParseTutorialOut, ParseTutorialPayload
  10. from app.schemas.resource import FileUploadOut
  11. from app.schemas.short_url import ShortUrlCreate, ShortUrlOut
  12. from app.services.http_session_service import HttpSessionService
  13. from app.services.llm_service import LlmService
  14. from typing import Optional
  15. from redis.asyncio import Redis
  16. from app.core.redis import get_redis_client
  17. from app.core.auth import get_current_user
  18. from app.models.user import VasUser
  19. from app.core.biz_exception import BizLogicError
  20. from app.services.r2_storage_service import R2StorageService
  21. from app.services.seaweedfs_service import SeaweedFSService
  22. from app.services.short_url_service import ShortUrlService
  23. public_router = APIRouter()
  24. protected_router = APIRouter()
  25. admin_required_router = APIRouter()
  26. @admin_required_router.get("/ping", summary="心跳检测", tags=["测试接口"])
  27. async def ping():
  28. return {"message": "pong"}
  29. @admin_required_router.post("/http-session", summary="创建http session", tags=["会话管理"], response_model=ApiResponse[HttpSessionOut])
  30. async def http_session_create(
  31. data: HttpSessionCreate,
  32. db: AsyncSession = Depends(get_db)
  33. ):
  34. logger.info(f"[Create HttpSession] sid={data.session_id}")
  35. obj = await HttpSessionService.create(db, data)
  36. return success(data=obj)
  37. @admin_required_router.get("/http-session", summary="读取http session", tags=["会话管理"], response_model=ApiResponse[HttpSessionOut])
  38. async def http_session_get_by_sid(
  39. session_id: str = Query(...),
  40. db: AsyncSession = Depends(get_db)
  41. ):
  42. logger.info(f"[Get HttpSession] sid={session_id}")
  43. obj = await HttpSessionService.get_by_sid(db, session_id)
  44. return success(data=obj)
  45. @protected_router.post("/resource/upload_file", summary="上传文件(需登录鉴权)", tags=["文件管理"], response_model=ApiResponse[FileUploadOut])
  46. async def resource_upload_file(
  47. request: Request,
  48. file: UploadFile = File(...),
  49. current_user: VasUser = Depends(get_current_user),
  50. redis_client: Redis = Depends(get_redis_client),
  51. ):
  52. # 提取真实客户端 IP
  53. client_ip = (
  54. request.headers.get("cf-connecting-ip")
  55. or (request.headers.get("x-forwarded-for", "").split(",")[0].strip() or None)
  56. or (request.client.host if request.client else "unknown")
  57. )
  58. # 防刷机制 1:单 IP 频次限制(单个 IP 每分钟最多允许 3 次上传)
  59. ip_rate_key = f"rate:seaweedfs_upload:ip:{client_ip}"
  60. ip_count = await redis_client.incr(ip_rate_key)
  61. if ip_count == 1:
  62. await redis_client.expire(ip_rate_key, 60)
  63. elif ip_count > 3:
  64. raise BizLogicError("Too many upload requests from this IP address. Please try again later.")
  65. # 防刷机制 2:单用户频次限制(单个账号每分钟最多允许 3 次上传)
  66. user_rate_key = f"rate:seaweedfs_upload:user:{current_user.id}"
  67. user_count = await redis_client.incr(user_rate_key)
  68. if user_count == 1:
  69. await redis_client.expire(user_rate_key, 60)
  70. elif user_count > 3:
  71. raise BizLogicError("Too many upload requests for this account. Please try again later.")
  72. result = await SeaweedFSService.upload(file)
  73. return success(data=result)
  74. @public_router.get("/resource/download_file", summary="下载文件", tags=["文件管理"])
  75. async def resource_get_file(
  76. request: Request,
  77. fid: str = Query(...),
  78. redis_client: Redis = Depends(get_redis_client),
  79. ):
  80. # 下载防刷限制:单 IP 每分钟最多 60 次下载请求,防止高频消耗服务器下行带宽
  81. client_ip = (
  82. request.headers.get("cf-connecting-ip")
  83. or (request.headers.get("x-forwarded-for", "").split(",")[0].strip() or None)
  84. or (request.client.host if request.client else "unknown")
  85. )
  86. dl_rate_key = f"rate:seaweedfs_download:ip:{client_ip}"
  87. dl_count = await redis_client.incr(dl_rate_key)
  88. if dl_count == 1:
  89. await redis_client.expire(dl_rate_key, 60)
  90. elif dl_count > 60:
  91. raise BizLogicError("Download frequency limit exceeded. Please try again later.")
  92. data = await SeaweedFSService.get(fid)
  93. if not data:
  94. return Response(content=b"File not found", status_code=404, media_type="text/plain")
  95. content, mime = data
  96. return Response(content=content, media_type=mime)
  97. @protected_router.post("/r2/upload", summary="上传文件至 Cloudflare R2(需登录鉴权)", tags=["R2 对象存储"], response_model=ApiResponse[FileUploadOut])
  98. async def r2_upload_file(
  99. request: Request,
  100. file: UploadFile = File(...),
  101. bucket: Optional[str] = Query(None, description="目标存储桶名称(可选,留空使用环境变量默认配置)"),
  102. prefix: str = Query("uploads", description="存储目录前缀"),
  103. current_user: VasUser = Depends(get_current_user),
  104. redis_client: Redis = Depends(get_redis_client),
  105. ):
  106. # 提取真实客户端 IP(优先 Cloudflare CDN 提供的 cf-connecting-ip,其次 x-forwarded-for)
  107. client_ip = (
  108. request.headers.get("cf-connecting-ip")
  109. or (request.headers.get("x-forwarded-for", "").split(",")[0].strip() or None)
  110. or (request.client.host if request.client else "unknown")
  111. )
  112. # 防刷机制 1:单 IP 频次限制(单个 IP 每分钟最多允许 15 次上传,防止多账号集中在同个 IP 恶意刷量)
  113. ip_rate_key = f"rate:r2_upload:ip:{client_ip}"
  114. ip_count = await redis_client.incr(ip_rate_key)
  115. if ip_count == 1:
  116. await redis_client.expire(ip_rate_key, 60)
  117. elif ip_count > 3:
  118. raise BizLogicError("Too many upload requests from this IP address. Please try again later.")
  119. # 防刷机制 2:单用户频次限制(单个账号每分钟最多允许 10 次上传)
  120. user_rate_key = f"rate:r2_upload:user:{current_user.id}"
  121. user_count = await redis_client.incr(user_rate_key)
  122. if user_count == 1:
  123. await redis_client.expire(user_rate_key, 60)
  124. elif user_count > 3:
  125. raise BizLogicError("Too many upload requests for this account. Please try again later.")
  126. result = await R2StorageService.upload(file=file, bucket=bucket, prefix=prefix)
  127. return success(data=FileUploadOut(fid=result["fid"], url=result["url"]))
  128. @public_router.get("/r2/file", summary="从 Cloudflare R2 下载/预览文件", tags=["R2 对象存储"])
  129. async def r2_get_file(
  130. request: Request,
  131. key: str = Query(..., description="文件存储 key"),
  132. bucket: Optional[str] = Query(None, description="存储桶名称(可选)"),
  133. redis_client: Redis = Depends(get_redis_client),
  134. ):
  135. # 下载防刷限制:单 IP 每分钟最多 60 次下载请求,防止黑客对文件下载进行放大攻击
  136. client_ip = (
  137. request.headers.get("cf-connecting-ip")
  138. or (request.headers.get("x-forwarded-for", "").split(",")[0].strip() or None)
  139. or (request.client.host if request.client else "unknown")
  140. )
  141. dl_rate_key = f"rate:r2_download:ip:{client_ip}"
  142. dl_count = await redis_client.incr(dl_rate_key)
  143. if dl_count == 1:
  144. await redis_client.expire(dl_rate_key, 60)
  145. elif dl_count > 60:
  146. raise BizLogicError("Download frequency limit exceeded. Please try again later.")
  147. data = await R2StorageService.get_file(key=key, bucket=bucket)
  148. if not data:
  149. return Response(content=b"File not found", status_code=404, media_type="text/plain")
  150. content, mime = data
  151. return Response(
  152. content=content,
  153. media_type=mime,
  154. headers={"Cache-Control": "public, max-age=86400"}
  155. )
  156. @admin_required_router.post("/s/generate", summary="生成短链接地址<压缩地址长度>", tags=["Short URL"], response_model=ApiResponse[ShortUrlOut])
  157. async def short_url_generate(
  158. data: ShortUrlCreate,
  159. db: AsyncSession = Depends(get_db),
  160. ):
  161. """生成短链接"""
  162. record = await ShortUrlService.create_short_url(db, data.long_url)
  163. return success(data=record)
  164. @public_router.get("/s/{short_key}", summary="访问短链接地址<自动重定向到真实链接地址>", tags=["Short URL"])
  165. async def short_url_request(short_key: str, db: AsyncSession = Depends(get_db)):
  166. """访问短链接自动重定向"""
  167. long_url = await ShortUrlService.get_long_url(db, short_key)
  168. return RedirectResponse(url=long_url, status_code=302)
  169. @admin_required_router.post("/vas/llm/data_parsing", summary="llm数据解析", tags=["textskin"], response_model=ApiResponse[ParseUserInputsOut])
  170. async def vas_llm_data_parsing(
  171. payload: ParseUserInputsPayload,
  172. db: AsyncSession = Depends(get_db)
  173. ):
  174. out = await LlmService.handle_parse(db, payload)
  175. return success(data=out)
  176. @admin_required_router.post("/vas/llm/parse_tutorial", summary="AI解析用户教程与操作指引配置", tags=["textskin"], response_model=ApiResponse[ParseTutorialOut])
  177. async def vas_llm_parse_tutorial(
  178. payload: ParseTutorialPayload,
  179. ):
  180. if not payload.prompt or not payload.prompt.strip():
  181. raise BizLogicError("Please provide a valid natural language tutorial description")
  182. parsed_config = await LlmService.parse_tutorial_async(
  183. prompt=payload.prompt,
  184. model=payload.model,
  185. system_prompt=payload.system_prompt,
  186. )
  187. return success(data=ParseTutorialOut(tutorial_config=parsed_config))