Spaces:
Sleeping
Sleeping
import json | |
import hashlib | |
import os | |
import pytz | |
from datetime import datetime | |
from dotenv import load_dotenv | |
load_dotenv(override=True) | |
UTILS_FOLDER_PATH = os.path.dirname(os.path.abspath(__file__)) | |
CONFIG_FILE = os.path.join(UTILS_FOLDER_PATH, "app_config.json") | |
UPLOAD_FOLDER = os.path.join(UTILS_FOLDER_PATH, "uploaded_files") | |
DB_FOLDER = os.path.join(UTILS_FOLDER_PATH, "database") | |
DEFAULT_PASSWORD = os.getenv('APP_PASSWORD') | |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') | |
DUBAI_TZ = pytz.timezone('Asia/Dubai') | |
COLLECTION_NAME = "vartur_collection" | |
class AppConfig: | |
def __init__(self): | |
self.config_path = CONFIG_FILE | |
self.default_config = { | |
"indexed_files": {}, | |
"last_updated": None, | |
"password_hash": hashlib.sha256(DEFAULT_PASSWORD.encode()).hexdigest() | |
} | |
self.load_config() | |
def load_config(self): | |
try: | |
if os.path.exists(self.config_path): | |
with open(self.config_path, 'r') as f: | |
self.config = json.load(f) | |
else: | |
self.config = self.default_config | |
self.save_config() | |
except Exception as e: | |
print(f"Error loading config: {e}") | |
self.config = self.default_config | |
self.save_config() | |
def save_config(self): | |
try: | |
os.makedirs(os.path.dirname(self.config_path), exist_ok=True) | |
with open(self.config_path, 'w') as f: | |
json.dump(self.config, f, indent=4) | |
except Exception as e: | |
print(f"Error saving config: {e}") | |
def update_indexed_files(self, filename: str, file_hash: str): | |
self.config["indexed_files"][filename] = { | |
"hash": file_hash, | |
"timestamp": datetime.now().isoformat() | |
} | |
self.config["last_updated"] = datetime.now().isoformat() | |
self.save_config() | |
def remove_indexed_file(self, filename: str): | |
if filename in self.config["indexed_files"]: | |
del self.config["indexed_files"][filename] | |
self.save_config() | |
def verify_password(self, password: str) -> bool: | |
if not password: | |
return False | |
password_hash = hashlib.sha256(password.encode()).hexdigest() | |
return password_hash == self.config["password_hash"] | |
def get_stats(self): | |
return { | |
"total_files": len(self.config["indexed_files"]), | |
"last_updated": self.config["last_updated"] | |
} | |