Spaces:
Runtime error
Runtime error
Update config.py
Browse files12345678910111213141516171819202122232425262 unmodified lines293031323334353637383940414243444515 unmodified lines6162636465# filename: config.pyimport osimport refrom dotenv import load_dotenv# Load environment variables from a .env file for local development# In production (like Docker/Hugging Face), these will be set directly.load_dotenv()# --- Helper function to cast environment variables ---def get_env(key, default=None, cast_to=str):"""Fetches an environment variable and casts it to a specified type."""value = os.getenv(key, default)if value is not None:try:# Handle boolean strings explicitlyif cast_to == bool:return value.lower() in ('true', '1', 't', 'y', 'yes')return cast_to(value)except (ValueError, TypeError):return defaultreturn default# --- Telegram Credentials (Mandatory) ---API_ID = get_env("API_ID", cast_to=int)2 unmodified linesOWNER_ID = get_env("OWNER_ID", cast_to=int)if not all([API_ID, API_HASH, BOT_TOKEN, OWNER_ID]):raise ValueError("Error: One or more mandatory Telegram credentials are not set in the environment.")# --- Database Configuration (Mandatory) ---# This logic automatically finds all MONGODB_URL_n secrets you've set.mongo_vars = {}for key, value in os.environ.items():if match := re.match(r"MONGODB_URL_(\d+)", key):if value:mongo_vars[int(match.group(1))] = value# Sort the found URLs by their number (1, 2, 3...) to create the final listMONGODB_URIS = [mongo_vars[key] for key in sorted(mongo_vars.keys())]if not MONGODB_URIS:15 unmodified lines_free_limit_mb = get_env("FREE_USER_FILE_SIZE_LIMIT", 50, cast_to=int)FREE_USER_FILE_SIZE_LIMIT_BYTES = _free_limit_mb * 1024 * 1024# Toggle for enabling FFMPEG features like thumbnailing and remuxingENABLE_FFMPEG = get_env("ENABLE_FFMPEG", True, cast_to=bool)1234567891011121314151617181920212223242526272829303132333435363738392 unmodified lines42434445464748495051525354555615 unmodified lines727374757677# filename: config.py (Corrected Version)import osimport refrom dotenv import load_dotenv# Load environment variables from a .env file for local developmentload_dotenv()# --- Helper function to cast environment variables (CORRECTED) ---def get_env(key, default=None, cast_to=str):"""Fetches an environment variable, casts it to a specified type,and correctly handles non-string default values."""value = os.getenv(key, default)# If no value is found at all, return Noneif value is None:return None# --- The Fix ---# If the retrieved value is already the type we want (e.g., default=True was used),# return it directly without trying to process it as a string.if isinstance(value, cast_to):return value# If we have a string from the environment, process it.if cast_to == bool:return value.lower() in ('true', '1', 't', 'y', 'yes')try:return cast_to(value)except (ValueError, TypeError):return default# --- Telegram Credentials (Mandatory) ---API_ID = get_env("API_ID", cast_to=int)2 unmodified linesOWNER_ID = get_env("OWNER_ID", cast_to=int)if not all([API_ID, API_HASH, BOT_TOKEN, OWNER_ID]):raise ValueError("Error: One or more mandatory Telegram credentials are not set.")# --- Database Configuration (Mandatory) ---mongo_vars = {}for key, value in os.environ.items():if match := re.match(r"MONGODB_URL_(\d+)", key):if value:mongo_vars[int(match.group(1))] = valueMONGODB_URIS = [mongo_vars[key] for key in sorted(mongo_vars.keys())]if not MONGODB_URIS:15 unmodified lines_free_limit_mb = get_env("FREE_USER_FILE_SIZE_LIMIT", 50, cast_to=int)FREE_USER_FILE_SIZE_LIMIT_BYTES = _free_limit_mb * 1024 * 1024# This line now works correctly with the fixed helper function.ENABLE_FFMPEG = get_env("ENABLE_FFMPEG", True, cast_to=bool)
config.py+29-17