Spaces:
Running
Running
File size: 3,125 Bytes
89e8242 74f8847 89e8242 74f8847 89e8242 74f8847 89e8242 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import os
from motor.motor_asyncio import AsyncIOMotorClient
import certifi
import asyncio
# Load from environment variable for production readiness
# Load from environment variable for security
MONGO_URL = os.getenv("MONGO_URL")
class DummyCollection:
"""Mock collection to prevent crashes when MongoDB is unreachable."""
def __init__(self, name):
self.name = name
async def create_index(self, *args, **kwargs): return None
async def insert_one(self, *args, **kwargs): return type('obj', (), {'inserted_id': 'offline_id'})
async def find_one(self, *args, **kwargs): return None
async def update_one(self, *args, **kwargs): return type('obj', (), {'modified_count': 1})
async def count_documents(self, *args, **kwargs): return 0
def find(self, *args, **kwargs):
class DummyCursor:
async def to_list(self, *args, **kwargs): return []
def sort(self, *args, **kwargs): return self
def limit(self, *args, **kwargs): return self
return DummyCursor()
# Global state
client = None
db = None
users_collection = DummyCollection("users")
video_results_collection = DummyCollection("video_forensics")
audio_results_collection = DummyCollection("audio_forensics")
image_results_collection = DummyCollection("image_forensics")
text_results_collection = DummyCollection("text_forensics")
try:
# Create the Async MongoDB Client with a short timeout and certifi SSL bundle
client = AsyncIOMotorClient(
MONGO_URL,
serverSelectionTimeoutMS=5000,
tlsCAFile=certifi.where()
)
db = client.fakeshield_db
# Real collections (proxies for Atlas)
users_collection = db.get_collection("users")
video_results_collection = db.get_collection("video_forensics")
audio_results_collection = db.get_collection("audio_forensics")
image_results_collection = db.get_collection("image_forensics")
text_results_collection = db.get_collection("text_forensics")
print("[DB] MongoDB Client Initialized (Proxied).")
except Exception as e:
print(f"[DB] Initial setup error: {e}")
async def init_db():
"""Initializes indexes. Gracefully handles Atlas timeouts."""
if client is None:
print("[DB] Skipping Index Initialization (Offline Mode)")
return
try:
# Check connection - if this fails, we stay in Dummy mode
await client.admin.command('ping')
collections = [
users_collection, video_results_collection,
audio_results_collection, image_results_collection,
text_results_collection
]
for col in collections:
if col.name == "users":
await col.create_index("email", unique=True)
else:
await col.create_index("user_email")
await col.create_index([("created_at", -1)])
print("[DB] MongoDB Atlas Online: Indexes Initialized!")
except Exception as e:
print(f"[DB] Atlas Reachability Check Failed: {e}")
print("[DB] Operating in Reduced Functional Mode (No Data Persistence).")
|