dragg2 commited on
Commit
ff63b85
·
verified ·
1 Parent(s): 4309b82

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +28 -1
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import json
4
 
5
  from fastapi import FastAPI, Request
6
- from fastapi.responses import Response, StreamingResponse
7
  import httpx
8
 
9
  # 内部真实 Bot API Server(telegram-bot-api)监听端口
@@ -11,6 +11,8 @@ import httpx
11
  UPSTREAM = f"http://127.0.0.1:{os.environ.get('TELEGRAM_UPSTREAM_PORT', '8081')}"
12
  app = FastAPI()
13
 
 
 
14
  def _normalize_bot_api_file_path(raw_fp: str | None, token_enc: str | None) -> str:
15
  if not raw_fp:
16
  return ""
@@ -65,6 +67,31 @@ async def proxy(path: str, request: Request):
65
  if fixed:
66
  path_for_upstream = f"file/bot{token_enc}/{fixed}"
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  url = f"{UPSTREAM}/{path_for_upstream}"
69
 
70
  params = list(request.query_params.multi_items())
 
3
  import json
4
 
5
  from fastapi import FastAPI, Request
6
+ from fastapi.responses import Response, StreamingResponse, FileResponse
7
  import httpx
8
 
9
  # 内部真实 Bot API Server(telegram-bot-api)监听端口
 
11
  UPSTREAM = f"http://127.0.0.1:{os.environ.get('TELEGRAM_UPSTREAM_PORT', '8081')}"
12
  app = FastAPI()
13
 
14
+ WORK_DIR = os.environ.get("TELEGRAM_WORK_DIR", "/tmp/telegram-bot-api-data")
15
+
16
  def _normalize_bot_api_file_path(raw_fp: str | None, token_enc: str | None) -> str:
17
  if not raw_fp:
18
  return ""
 
67
  if fixed:
68
  path_for_upstream = f"file/bot{token_enc}/{fixed}"
69
 
70
+ # 官方/通用做法(更稳):--local 场景 getFile 可能返回“本地路径”,所以 /file 下载最好不要再依赖 bot-api 自己的 /file 端点
71
+ # 直接从容器的 WORK_DIR 里读文件返回给浏览器。
72
+ # 这样就算 bot-api 的 /file 行为有差异,也不会影响 CloudPaste 预览下载。
73
+ if path_for_upstream.startswith("file/"):
74
+ rest = path_for_upstream[len("file/") :]
75
+ token_enc = None
76
+ rel = ""
77
+ if rest.startswith("bot"):
78
+ token_enc = rest[3:].split("/", 1)[0] or None
79
+ rel = rest.split("/", 1)[1] if "/" in rest else ""
80
+ rel_fixed = _normalize_bot_api_file_path(rel, token_enc)
81
+
82
+ candidates = []
83
+ if token_enc and rel_fixed:
84
+ candidates.append(os.path.join(WORK_DIR, token_enc, rel_fixed))
85
+ if rel_fixed:
86
+ candidates.append(os.path.join(WORK_DIR, rel_fixed))
87
+
88
+ for p in candidates:
89
+ try:
90
+ if p and os.path.isfile(p):
91
+ return FileResponse(p)
92
+ except Exception:
93
+ pass
94
+
95
  url = f"{UPSTREAM}/{path_for_upstream}"
96
 
97
  params = list(request.query_params.multi_items())