Add offline mode: Allow website to work without Telegram clients
Browse files- main.py +20 -1
- utils/clients.py +14 -3
- utils/directoryHandler.py +30 -8
main.py
CHANGED
|
@@ -25,7 +25,13 @@ async def lifespan(app: FastAPI):
|
|
| 25 |
reset_cache_dir()
|
| 26 |
|
| 27 |
# Initialize the clients
|
| 28 |
-
await initialize_clients()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Start the website auto ping task
|
| 31 |
asyncio.create_task(auto_ping_website())
|
|
@@ -60,7 +66,11 @@ async def static_files(file_path):
|
|
| 60 |
@app.get("/file")
|
| 61 |
async def dl_file(request: Request):
|
| 62 |
from utils.directoryHandler import DRIVE_DATA
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
path = request.query_params["path"]
|
| 65 |
file = DRIVE_DATA.get_file(path)
|
| 66 |
return await media_streamer(STORAGE_CHANNEL, file.file_id, file.name, request)
|
|
@@ -156,10 +166,14 @@ async def upload_file(
|
|
| 156 |
total_size: str = Form(...),
|
| 157 |
):
|
| 158 |
global SAVE_PROGRESS
|
|
|
|
| 159 |
|
| 160 |
if password != ADMIN_PASSWORD:
|
| 161 |
return JSONResponse({"status": "Invalid password"})
|
| 162 |
|
|
|
|
|
|
|
|
|
|
| 163 |
total_size = int(total_size)
|
| 164 |
SAVE_PROGRESS[id] = ("running", 0, total_size)
|
| 165 |
|
|
@@ -304,11 +318,16 @@ async def getFileInfoFromUrl(request: Request):
|
|
| 304 |
|
| 305 |
@app.post("/api/startFileDownloadFromUrl")
|
| 306 |
async def startFileDownloadFromUrl(request: Request):
|
|
|
|
|
|
|
| 307 |
data = await request.json()
|
| 308 |
|
| 309 |
if data["password"] != ADMIN_PASSWORD:
|
| 310 |
return JSONResponse({"status": "Invalid password"})
|
| 311 |
|
|
|
|
|
|
|
|
|
|
| 312 |
logger.info(f"startFileDownloadFromUrl {data}")
|
| 313 |
try:
|
| 314 |
id = getRandomID()
|
|
|
|
| 25 |
reset_cache_dir()
|
| 26 |
|
| 27 |
# Initialize the clients
|
| 28 |
+
clients_initialized = await initialize_clients()
|
| 29 |
+
|
| 30 |
+
# If clients failed to initialize, initialize DRIVE_DATA in offline mode
|
| 31 |
+
if not clients_initialized:
|
| 32 |
+
from utils.directoryHandler import initDriveDataWithoutClients
|
| 33 |
+
await initDriveDataWithoutClients()
|
| 34 |
+
logger.warning("Website is running in OFFLINE MODE - Telegram features are disabled")
|
| 35 |
|
| 36 |
# Start the website auto ping task
|
| 37 |
asyncio.create_task(auto_ping_website())
|
|
|
|
| 66 |
@app.get("/file")
|
| 67 |
async def dl_file(request: Request):
|
| 68 |
from utils.directoryHandler import DRIVE_DATA
|
| 69 |
+
from utils.clients import has_clients
|
| 70 |
|
| 71 |
+
if not has_clients():
|
| 72 |
+
raise HTTPException(status_code=503, detail="Telegram clients not available - Service running in offline mode")
|
| 73 |
+
|
| 74 |
path = request.query_params["path"]
|
| 75 |
file = DRIVE_DATA.get_file(path)
|
| 76 |
return await media_streamer(STORAGE_CHANNEL, file.file_id, file.name, request)
|
|
|
|
| 166 |
total_size: str = Form(...),
|
| 167 |
):
|
| 168 |
global SAVE_PROGRESS
|
| 169 |
+
from utils.clients import has_clients
|
| 170 |
|
| 171 |
if password != ADMIN_PASSWORD:
|
| 172 |
return JSONResponse({"status": "Invalid password"})
|
| 173 |
|
| 174 |
+
if not has_clients():
|
| 175 |
+
return JSONResponse({"status": "Telegram clients not available - Service running in offline mode"})
|
| 176 |
+
|
| 177 |
total_size = int(total_size)
|
| 178 |
SAVE_PROGRESS[id] = ("running", 0, total_size)
|
| 179 |
|
|
|
|
| 318 |
|
| 319 |
@app.post("/api/startFileDownloadFromUrl")
|
| 320 |
async def startFileDownloadFromUrl(request: Request):
|
| 321 |
+
from utils.clients import has_clients
|
| 322 |
+
|
| 323 |
data = await request.json()
|
| 324 |
|
| 325 |
if data["password"] != ADMIN_PASSWORD:
|
| 326 |
return JSONResponse({"status": "Invalid password"})
|
| 327 |
|
| 328 |
+
if not has_clients():
|
| 329 |
+
return JSONResponse({"status": "Telegram clients not available - Service running in offline mode"})
|
| 330 |
+
|
| 331 |
logger.info(f"startFileDownloadFromUrl {data}")
|
| 332 |
try:
|
| 333 |
id = getRandomID()
|
utils/clients.py
CHANGED
|
@@ -84,9 +84,12 @@ async def initialize_clients():
|
|
| 84 |
]
|
| 85 |
)
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if len(premium_clients) == 0:
|
| 92 |
logger.info("No Premium Clients Were Initialized")
|
|
@@ -98,6 +101,14 @@ async def initialize_clients():
|
|
| 98 |
|
| 99 |
# Start the backup drive data task
|
| 100 |
asyncio.create_task(backup_drive_data())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
def get_client(premium_required=False) -> Client:
|
|
|
|
| 84 |
]
|
| 85 |
)
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
clients_initialized = len(multi_clients) > 0
|
| 89 |
+
|
| 90 |
+
if not clients_initialized:
|
| 91 |
+
logger.warning("No Clients Were Initialized - Website will run in offline mode")
|
| 92 |
+
return False
|
| 93 |
|
| 94 |
if len(premium_clients) == 0:
|
| 95 |
logger.info("No Premium Clients Were Initialized")
|
|
|
|
| 101 |
|
| 102 |
# Start the backup drive data task
|
| 103 |
asyncio.create_task(backup_drive_data())
|
| 104 |
+
|
| 105 |
+
return True
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def has_clients():
|
| 109 |
+
"""Check if any clients are available"""
|
| 110 |
+
global multi_clients
|
| 111 |
+
return len(multi_clients) > 0
|
| 112 |
|
| 113 |
|
| 114 |
def get_client(premium_required=False) -> Client:
|
utils/directoryHandler.py
CHANGED
|
@@ -345,23 +345,19 @@ async def loadDriveData():
|
|
| 345 |
logger.info("Loading drive data.")
|
| 346 |
from utils.clients import get_client
|
| 347 |
|
| 348 |
-
client = get_client()
|
| 349 |
try:
|
|
|
|
| 350 |
try:
|
| 351 |
msg: Message = await client.get_messages(
|
| 352 |
config.STORAGE_CHANNEL, config.DATABASE_BACKUP_MSG_ID
|
| 353 |
)
|
| 354 |
except Exception as e:
|
| 355 |
logger.error(f"Error fetching backup message: {e}")
|
| 356 |
-
|
| 357 |
-
# Forcefully terminates the program immediately
|
| 358 |
-
os.kill(os.getpid(), signal.SIGKILL)
|
| 359 |
|
| 360 |
if not msg.document:
|
| 361 |
-
logger.error(
|
| 362 |
-
|
| 363 |
-
# Forcefully terminates the program immediately
|
| 364 |
-
os.kill(os.getpid(), signal.SIGKILL)
|
| 365 |
|
| 366 |
if msg.document.file_name == "drive.data":
|
| 367 |
dl_path = await msg.download()
|
|
@@ -385,3 +381,29 @@ async def loadDriveData():
|
|
| 385 |
BOT_MODE = NewBotMode(DRIVE_DATA)
|
| 386 |
await start_bot_mode(DRIVE_DATA, BOT_MODE)
|
| 387 |
logger.info("Bot mode started.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
logger.info("Loading drive data.")
|
| 346 |
from utils.clients import get_client
|
| 347 |
|
|
|
|
| 348 |
try:
|
| 349 |
+
client = get_client()
|
| 350 |
try:
|
| 351 |
msg: Message = await client.get_messages(
|
| 352 |
config.STORAGE_CHANNEL, config.DATABASE_BACKUP_MSG_ID
|
| 353 |
)
|
| 354 |
except Exception as e:
|
| 355 |
logger.error(f"Error fetching backup message: {e}")
|
| 356 |
+
raise
|
|
|
|
|
|
|
| 357 |
|
| 358 |
if not msg.document:
|
| 359 |
+
logger.error("Backup message does not contain a document")
|
| 360 |
+
raise Exception("Backup message does not contain a document")
|
|
|
|
|
|
|
| 361 |
|
| 362 |
if msg.document.file_name == "drive.data":
|
| 363 |
dl_path = await msg.download()
|
|
|
|
| 381 |
BOT_MODE = NewBotMode(DRIVE_DATA)
|
| 382 |
await start_bot_mode(DRIVE_DATA, BOT_MODE)
|
| 383 |
logger.info("Bot mode started.")
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
async def initDriveDataWithoutClients():
|
| 387 |
+
"""Initialize DRIVE_DATA without requiring Telegram clients (offline mode)"""
|
| 388 |
+
global DRIVE_DATA, BOT_MODE
|
| 389 |
+
|
| 390 |
+
logger.info("Initializing drive data in offline mode (without Telegram clients).")
|
| 391 |
+
|
| 392 |
+
# Try to load from local cache if it exists
|
| 393 |
+
if drive_cache_path.exists():
|
| 394 |
+
try:
|
| 395 |
+
with open(drive_cache_path, "rb") as f:
|
| 396 |
+
DRIVE_DATA = dill.load(f)
|
| 397 |
+
logger.info("Drive data loaded from local cache.")
|
| 398 |
+
except Exception as e:
|
| 399 |
+
logger.warning(f"Failed to load from local cache: {e}")
|
| 400 |
+
logger.info("Creating new drive.data file.")
|
| 401 |
+
DRIVE_DATA = NewDriveData({"/": Folder("/", "/")}, [])
|
| 402 |
+
DRIVE_DATA.save()
|
| 403 |
+
else:
|
| 404 |
+
logger.info("No local cache found. Creating new drive.data file.")
|
| 405 |
+
DRIVE_DATA = NewDriveData({"/": Folder("/", "/")}, [])
|
| 406 |
+
DRIVE_DATA.save()
|
| 407 |
+
|
| 408 |
+
await init_drive_data()
|
| 409 |
+
logger.info("Drive data initialized in offline mode.")
|