Update main.py
Browse files
main.py
CHANGED
|
@@ -5,6 +5,7 @@ from telethon.sessions import StringSession
|
|
| 5 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 6 |
from fastapi.responses import FileResponse
|
| 7 |
import uvicorn
|
|
|
|
| 8 |
|
| 9 |
# --- Configuration from environment variables ---
|
| 10 |
# Your Telegram session string
|
|
@@ -17,10 +18,9 @@ API_HASH = os.environ.get('API_HASH')
|
|
| 17 |
# The Telegram channel you want to upload files to
|
| 18 |
TARGET_CHANNEL = 'https://t.me/TestNAI01'
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 24 |
|
| 25 |
app = FastAPI()
|
| 26 |
|
|
@@ -31,19 +31,19 @@ async def upload_to_telegram(file_path: str):
|
|
| 31 |
if not all([SESSION_STRING, API_ID, API_HASH]):
|
| 32 |
raise ValueError("SESSION_STRING, API_ID, and API_HASH must be set as environment variables.")
|
| 33 |
|
| 34 |
-
# Initialize the client
|
| 35 |
async with TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH) as client:
|
| 36 |
try:
|
| 37 |
await client.send_file(TARGET_CHANNEL, file_path)
|
| 38 |
except Exception as e:
|
| 39 |
-
# Provide a more specific error message if upload fails
|
| 40 |
raise HTTPException(status_code=500, detail=f"Failed to upload to Telegram: {e}")
|
| 41 |
|
| 42 |
@app.post("/v1/upload")
|
| 43 |
async def upload_file(file: UploadFile = File(...)):
|
| 44 |
"""
|
| 45 |
-
Receives a file and uploads it to the Telegram channel.
|
| 46 |
"""
|
|
|
|
| 47 |
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 48 |
try:
|
| 49 |
# Save the uploaded file temporarily
|
|
@@ -61,18 +61,17 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 61 |
@app.get("/v1/download/{filename}")
|
| 62 |
async def download_file(filename: str):
|
| 63 |
"""
|
| 64 |
-
Serves a file for download
|
| 65 |
-
NOTE: This is
|
| 66 |
-
|
| 67 |
-
|
| 68 |
"""
|
| 69 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 70 |
if os.path.exists(file_path):
|
| 71 |
return FileResponse(file_path)
|
| 72 |
-
raise HTTPException(status_code=404, detail="File not found
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
# This block allows running the script directly for local testing (without Docker)
|
| 76 |
if not all([SESSION_STRING, API_ID, API_HASH]):
|
| 77 |
print("FATAL ERROR: The environment variables SESSION_STRING, API_ID, and API_HASH must be set.")
|
| 78 |
else:
|
|
|
|
| 5 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 6 |
from fastapi.responses import FileResponse
|
| 7 |
import uvicorn
|
| 8 |
+
import tempfile # Import the tempfile module
|
| 9 |
|
| 10 |
# --- Configuration from environment variables ---
|
| 11 |
# Your Telegram session string
|
|
|
|
| 18 |
# The Telegram channel you want to upload files to
|
| 19 |
TARGET_CHANNEL = 'https://t.me/TestNAI01'
|
| 20 |
|
| 21 |
+
# --- FIX: Use the system's temporary directory ---
|
| 22 |
+
# This directory is guaranteed to be writable and avoids permission errors.
|
| 23 |
+
UPLOAD_DIR = tempfile.gettempdir()
|
|
|
|
| 24 |
|
| 25 |
app = FastAPI()
|
| 26 |
|
|
|
|
| 31 |
if not all([SESSION_STRING, API_ID, API_HASH]):
|
| 32 |
raise ValueError("SESSION_STRING, API_ID, and API_HASH must be set as environment variables.")
|
| 33 |
|
| 34 |
+
# Initialize the client
|
| 35 |
async with TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH) as client:
|
| 36 |
try:
|
| 37 |
await client.send_file(TARGET_CHANNEL, file_path)
|
| 38 |
except Exception as e:
|
|
|
|
| 39 |
raise HTTPException(status_code=500, detail=f"Failed to upload to Telegram: {e}")
|
| 40 |
|
| 41 |
@app.post("/v1/upload")
|
| 42 |
async def upload_file(file: UploadFile = File(...)):
|
| 43 |
"""
|
| 44 |
+
Receives a file, saves it to the temp directory, and uploads it to the Telegram channel.
|
| 45 |
"""
|
| 46 |
+
# Create a secure path in the temporary directory
|
| 47 |
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 48 |
try:
|
| 49 |
# Save the uploaded file temporarily
|
|
|
|
| 61 |
@app.get("/v1/download/{filename}")
|
| 62 |
async def download_file(filename: str):
|
| 63 |
"""
|
| 64 |
+
Serves a file for download.
|
| 65 |
+
NOTE: This endpoint is for demonstration. It looks for files in the temp
|
| 66 |
+
directory, but files uploaded in previous requests will have been deleted.
|
| 67 |
+
A production system would need a proper way to retrieve files from Telegram.
|
| 68 |
"""
|
| 69 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 70 |
if os.path.exists(file_path):
|
| 71 |
return FileResponse(file_path)
|
| 72 |
+
raise HTTPException(status_code=404, detail="File not found. Files are only stored temporarily during upload.")
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 75 |
if not all([SESSION_STRING, API_ID, API_HASH]):
|
| 76 |
print("FATAL ERROR: The environment variables SESSION_STRING, API_ID, and API_HASH must be set.")
|
| 77 |
else:
|