Upload database.py
Browse files- src/core/database.py +60 -47
src/core/database.py
CHANGED
|
@@ -935,23 +935,32 @@ class Database:
|
|
| 935 |
|
| 936 |
async def get_generation_config(self) -> Optional[GenerationConfig]:
|
| 937 |
"""Get generation configuration"""
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 945 |
|
| 946 |
async def update_generation_config(self, image_timeout: int, video_timeout: int):
|
| 947 |
"""Update generation configuration"""
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 955 |
|
| 956 |
# Request log operations
|
| 957 |
async def add_request_log(self, log: RequestLog):
|
|
@@ -1046,39 +1055,43 @@ class Database:
|
|
| 1046 |
- Generation config (image_timeout, video_timeout)
|
| 1047 |
- Proxy config will be handled by ProxyManager
|
| 1048 |
"""
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
|
| 1060 |
-
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
-
|
| 1064 |
-
|
| 1065 |
-
|
| 1066 |
-
|
| 1067 |
-
|
| 1068 |
-
|
| 1069 |
-
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
| 1081 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1082 |
|
| 1083 |
# Cache config operations
|
| 1084 |
async def get_cache_config(self) -> CacheConfig:
|
|
|
|
| 935 |
|
| 936 |
async def get_generation_config(self) -> Optional[GenerationConfig]:
|
| 937 |
"""Get generation configuration"""
|
| 938 |
+
try:
|
| 939 |
+
async with aiosqlite.connect(self.db_path) as db:
|
| 940 |
+
db.row_factory = aiosqlite.Row
|
| 941 |
+
cursor = await db.execute("SELECT * FROM generation_config WHERE id = 1")
|
| 942 |
+
row = await cursor.fetchone()
|
| 943 |
+
if row:
|
| 944 |
+
return GenerationConfig(**dict(row))
|
| 945 |
+
return None
|
| 946 |
+
except Exception as e:
|
| 947 |
+
print(f"Error getting generation config: {e}")
|
| 948 |
+
# 返回默认配置,避免异常传播
|
| 949 |
+
return GenerationConfig(image_timeout=300, video_timeout=1500)
|
| 950 |
|
| 951 |
async def update_generation_config(self, image_timeout: int, video_timeout: int):
|
| 952 |
"""Update generation configuration"""
|
| 953 |
+
try:
|
| 954 |
+
async with aiosqlite.connect(self.db_path) as db:
|
| 955 |
+
await db.execute("""
|
| 956 |
+
UPDATE generation_config
|
| 957 |
+
SET image_timeout = ?, video_timeout = ?, updated_at = CURRENT_TIMESTAMP
|
| 958 |
+
WHERE id = 1
|
| 959 |
+
""", (image_timeout, video_timeout))
|
| 960 |
+
await db.commit()
|
| 961 |
+
except Exception as e:
|
| 962 |
+
print(f"Error updating generation config: {e}")
|
| 963 |
+
raise
|
| 964 |
|
| 965 |
# Request log operations
|
| 966 |
async def add_request_log(self, log: RequestLog):
|
|
|
|
| 1055 |
- Generation config (image_timeout, video_timeout)
|
| 1056 |
- Proxy config will be handled by ProxyManager
|
| 1057 |
"""
|
| 1058 |
+
try:
|
| 1059 |
+
from .config import config
|
| 1060 |
+
|
| 1061 |
+
# Reload admin config
|
| 1062 |
+
admin_config = await self.get_admin_config()
|
| 1063 |
+
if admin_config:
|
| 1064 |
+
config.set_admin_username_from_db(admin_config.username)
|
| 1065 |
+
config.set_admin_password_from_db(admin_config.password)
|
| 1066 |
+
config.api_key = admin_config.api_key
|
| 1067 |
+
|
| 1068 |
+
# Reload cache config
|
| 1069 |
+
cache_config = await self.get_cache_config()
|
| 1070 |
+
if cache_config:
|
| 1071 |
+
config.set_cache_enabled(cache_config.cache_enabled)
|
| 1072 |
+
config.set_cache_timeout(cache_config.cache_timeout)
|
| 1073 |
+
config.set_cache_base_url(cache_config.cache_base_url or "")
|
| 1074 |
+
|
| 1075 |
+
# Reload generation config
|
| 1076 |
+
generation_config = await self.get_generation_config()
|
| 1077 |
+
if generation_config:
|
| 1078 |
+
config.set_image_timeout(generation_config.image_timeout)
|
| 1079 |
+
config.set_video_timeout(generation_config.video_timeout)
|
| 1080 |
+
|
| 1081 |
+
# Reload debug config
|
| 1082 |
+
debug_config = await self.get_debug_config()
|
| 1083 |
+
if debug_config:
|
| 1084 |
+
config.set_debug_enabled(debug_config.enabled)
|
| 1085 |
+
|
| 1086 |
+
# Reload captcha config
|
| 1087 |
+
captcha_config = await self.get_captcha_config()
|
| 1088 |
+
if captcha_config:
|
| 1089 |
+
config.set_captcha_method(captcha_config.captcha_method)
|
| 1090 |
+
config.set_yescaptcha_api_key(captcha_config.yescaptcha_api_key)
|
| 1091 |
+
config.set_yescaptcha_base_url(captcha_config.yescaptcha_base_url)
|
| 1092 |
+
except Exception as e:
|
| 1093 |
+
print(f"Error reloading config to memory: {e}")
|
| 1094 |
+
# 不要让异常传播,避免返回 500 错误
|
| 1095 |
|
| 1096 |
# Cache config operations
|
| 1097 |
async def get_cache_config(self) -> CacheConfig:
|