File size: 2,511 Bytes
9e22989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"]
        }