Spaces:
Paused
Paused
Commit Β·
de4a536
1
Parent(s): 89e28e1
fix: ensure FastAPI and Bot run concurrently on the same event loop
Browse files
main.py
CHANGED
|
@@ -32,35 +32,6 @@ if API_ID and API_HASH and BOT_TOKEN:
|
|
| 32 |
in_memory=True
|
| 33 |
)
|
| 34 |
|
| 35 |
-
@asynccontextmanager
|
| 36 |
-
async def lifespan(app: FastAPI):
|
| 37 |
-
# Startup: Start Bot within the same event loop as FastAPI
|
| 38 |
-
if bot:
|
| 39 |
-
await bot.start()
|
| 40 |
-
print("π High-Speed Telegram Bot Started!")
|
| 41 |
-
yield
|
| 42 |
-
# Shutdown: Stop Bot
|
| 43 |
-
if bot:
|
| 44 |
-
await bot.stop()
|
| 45 |
-
print("π Telegram Bot Stopped!")
|
| 46 |
-
|
| 47 |
-
app = FastAPI(title="High-Speed File Sharing", lifespan=lifespan)
|
| 48 |
-
|
| 49 |
-
# --- CLEANUP TASK ---
|
| 50 |
-
def cleanup_old_files():
|
| 51 |
-
while True:
|
| 52 |
-
now = time.time()
|
| 53 |
-
for filename in os.listdir(UPLOAD_DIR):
|
| 54 |
-
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 55 |
-
if os.path.isfile(file_path):
|
| 56 |
-
if now - os.path.getmtime(file_path) > 86400: # 24h
|
| 57 |
-
try:
|
| 58 |
-
os.remove(file_path)
|
| 59 |
-
except: pass
|
| 60 |
-
time.sleep(3600)
|
| 61 |
-
|
| 62 |
-
threading.Thread(target=cleanup_old_files, daemon=True).start()
|
| 63 |
-
|
| 64 |
# --- BOT HANDLERS ---
|
| 65 |
if bot:
|
| 66 |
def progress_bar(current, total):
|
|
@@ -100,7 +71,7 @@ if bot:
|
|
| 100 |
@bot.on_message(filters.private & (filters.document | filters.video | filters.audio | filters.photo))
|
| 101 |
async def handle_uploads(client, message: Message):
|
| 102 |
media = message.document or message.video or message.audio or message.photo
|
| 103 |
-
if isinstance(media, list): media = media[-1]
|
| 104 |
|
| 105 |
file_name = getattr(media, 'file_name', f"file_{int(time.time())}")
|
| 106 |
if message.photo: file_name = f"img_{int(time.time())}.jpg"
|
|
@@ -115,7 +86,7 @@ if bot:
|
|
| 115 |
|
| 116 |
async def progress(current, total):
|
| 117 |
nonlocal last_update_time
|
| 118 |
-
if time.time() - last_update_time > 4:
|
| 119 |
try:
|
| 120 |
await status.edit_text(
|
| 121 |
f"β‘ **Downloading and Saving to Disk...**\n\n"
|
|
@@ -142,6 +113,34 @@ if bot:
|
|
| 142 |
except Exception as e:
|
| 143 |
await status.edit_text(f"β **Upload Failed:** {str(e)}")
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
# --- FASTAPI ENDPOINTS ---
|
| 146 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 147 |
|
|
@@ -159,7 +158,6 @@ async def upload_files(
|
|
| 159 |
request: Request = None
|
| 160 |
):
|
| 161 |
user_id = x_user_id or "cli-anonymous"
|
| 162 |
-
|
| 163 |
if request.method == "PUT" or (not files and not file):
|
| 164 |
content = await request.body()
|
| 165 |
if not content: raise HTTPException(status_code=400)
|
|
@@ -176,7 +174,6 @@ async def upload_files(
|
|
| 176 |
with open(os.path.join(UPLOAD_DIR, name), "wb") as b:
|
| 177 |
shutil.copyfileobj(f.file, b)
|
| 178 |
urls.append(f"{BASE_URL}/download/{name}")
|
| 179 |
-
|
| 180 |
return urls[0] if len(target) == 1 and not x_user_id else {"urls": urls}
|
| 181 |
|
| 182 |
@app.get("/files")
|
|
@@ -202,4 +199,4 @@ async def delete_file(filename: str, x_user_id: str = Header(None)):
|
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
import uvicorn
|
| 205 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 32 |
in_memory=True
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# --- BOT HANDLERS ---
|
| 36 |
if bot:
|
| 37 |
def progress_bar(current, total):
|
|
|
|
| 71 |
@bot.on_message(filters.private & (filters.document | filters.video | filters.audio | filters.photo))
|
| 72 |
async def handle_uploads(client, message: Message):
|
| 73 |
media = message.document or message.video or message.audio or message.photo
|
| 74 |
+
if isinstance(media, list): media = media[-1]
|
| 75 |
|
| 76 |
file_name = getattr(media, 'file_name', f"file_{int(time.time())}")
|
| 77 |
if message.photo: file_name = f"img_{int(time.time())}.jpg"
|
|
|
|
| 86 |
|
| 87 |
async def progress(current, total):
|
| 88 |
nonlocal last_update_time
|
| 89 |
+
if time.time() - last_update_time > 4:
|
| 90 |
try:
|
| 91 |
await status.edit_text(
|
| 92 |
f"β‘ **Downloading and Saving to Disk...**\n\n"
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
await status.edit_text(f"β **Upload Failed:** {str(e)}")
|
| 115 |
|
| 116 |
+
@asynccontextmanager
|
| 117 |
+
async def lifespan(app: FastAPI):
|
| 118 |
+
# Start bot in a non-blocking way
|
| 119 |
+
if bot:
|
| 120 |
+
await bot.start()
|
| 121 |
+
print("π High-Speed Telegram Bot Started!")
|
| 122 |
+
yield
|
| 123 |
+
if bot:
|
| 124 |
+
await bot.stop()
|
| 125 |
+
print("π Telegram Bot Stopped!")
|
| 126 |
+
|
| 127 |
+
app = FastAPI(title="High-Speed File Sharing", lifespan=lifespan)
|
| 128 |
+
|
| 129 |
+
# --- CLEANUP TASK ---
|
| 130 |
+
def cleanup_old_files():
|
| 131 |
+
while True:
|
| 132 |
+
now = time.time()
|
| 133 |
+
for filename in os.listdir(UPLOAD_DIR):
|
| 134 |
+
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 135 |
+
if os.path.isfile(file_path):
|
| 136 |
+
if now - os.path.getmtime(file_path) > 86400:
|
| 137 |
+
try:
|
| 138 |
+
os.remove(file_path)
|
| 139 |
+
except: pass
|
| 140 |
+
time.sleep(3600)
|
| 141 |
+
|
| 142 |
+
threading.Thread(target=cleanup_old_files, daemon=True).start()
|
| 143 |
+
|
| 144 |
# --- FASTAPI ENDPOINTS ---
|
| 145 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 146 |
|
|
|
|
| 158 |
request: Request = None
|
| 159 |
):
|
| 160 |
user_id = x_user_id or "cli-anonymous"
|
|
|
|
| 161 |
if request.method == "PUT" or (not files and not file):
|
| 162 |
content = await request.body()
|
| 163 |
if not content: raise HTTPException(status_code=400)
|
|
|
|
| 174 |
with open(os.path.join(UPLOAD_DIR, name), "wb") as b:
|
| 175 |
shutil.copyfileobj(f.file, b)
|
| 176 |
urls.append(f"{BASE_URL}/download/{name}")
|
|
|
|
| 177 |
return urls[0] if len(target) == 1 and not x_user_id else {"urls": urls}
|
| 178 |
|
| 179 |
@app.get("/files")
|
|
|
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
import uvicorn
|
| 202 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, loop="asyncio")
|