Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(config): handle repeated UTF-8 BOM

harry07031339ee87c633
diff --git a/app/config/config.py b/app/config/config.py
index 4a22ebf3..ba5cefbf 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -23,6 +23,7 @@ _pending_config_save_requested = False
_pending_config_flush_scheduled = False
_MISSING = object()
_DELETE = object()
+_UTF8_BOM = "\ufeff"
class _SynchronizedConfig(dict):
@@ -419,6 +420,43 @@ def get_default_ollama_base_url() -> str:
return f"http://{_DOCKER_HOST_GATEWAY_NAME}:11434/v1"
+def _load_toml_config(config_path: str):
+ """
+ 加载 TOML,并兼容 Windows 编辑器可能写入的重复 UTF-8 BOM。
+
+ ``utf-8-sig`` 只会移除文件开头的一个 BOM。部分 Windows 编辑器或
+ 解压、保存流程可能再次写入 BOM,导致第二个不可见字符进入 TOML
+ 解析器并在第一行报错。这里仅在标准解析失败后做一次只读归一化,
+ 不回写原文件,避免意外覆盖用户已经填写的 API Key。
+ """
+ try:
+ return toml.load(config_path)
+ except (toml.TomlDecodeError, UnicodeDecodeError) as exc:
+ logger.warning(
+ "load config failed, retry with UTF-8 BOM compatibility: "
+ f"path={config_path}, error={type(exc).__name__}: {exc}"
+ )
+
+ try:
+ with open(config_path, mode="r", encoding="utf-8-sig") as fp:
+ config_content = fp.read()
+
+ normalized_content = config_content.lstrip(_UTF8_BOM)
+ removed_bom_count = len(config_content) - len(normalized_content)
+ if removed_bom_count:
+ logger.warning(
+ "removed repeated UTF-8 BOM characters while loading config: "
+ f"path={config_path}, count={removed_bom_count}"
+ )
+ return toml.loads(normalized_content)
+ except (toml.TomlDecodeError, UnicodeDecodeError) as exc:
+ logger.error(
+ "config file is not valid TOML after UTF-8 BOM normalization: "
+ f"path={config_path}, error={type(exc).__name__}: {exc}"
+ )
+ raise
+
+
def load_config():
# fix: IsADirectoryError: [Errno 21] Is a directory: '/MoneyPrinterTurbo/config.toml'
if os.path.isdir(config_file):
@@ -432,14 +470,7 @@ def load_config():
logger.info(f"load config from file: {config_file}")
- try:
- _config_ = toml.load(config_file)
- except Exception as e:
- logger.warning(f"load config failed: {str(e)}, try to load as utf-8-sig")
- with open(config_file, mode="r", encoding="utf-8-sig") as fp:
- _cfg_content = fp.read()
- _config_ = toml.loads(_cfg_content)
- return _config_
+ return _load_toml_config(config_file)
def save_config():
diff --git a/test/services/test_config.py b/test/services/test_config.py
index 2397cf52..1dc235db 100644
--- a/test/services/test_config.py
+++ b/test/services/test_config.py
@@ -6,6 +6,8 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
+import toml
+
from app.config import config
from app.models.llm_provider import LLM_PROVIDER_REGISTRY, get_llm_provider
@@ -65,6 +67,40 @@ class TestConfigPersistence:
for field in provider.extra_fields:
assert provider.config_key(field.config_suffix) in app_config
+ def test_load_config_accepts_repeated_utf8_bom_without_rewriting_file(self):
+ """重复 BOM 不应阻止 Windows 用户启动,也不能改写已有配置。"""
+ with TemporaryDirectory() as temp_dir:
+ config_path = Path(temp_dir) / "config.toml"
+ original_content = b"\xef\xbb\xbf\xef\xbb\xbf[app]\nvideo_source = \"pexels\"\n"
+ config_path.write_bytes(original_content)
+
+ with patch.object(config, "config_file", str(config_path)):
+ loaded_config = config.load_config()
+
+ assert loaded_config["app"]["video_source"] == "pexels"
+ assert config_path.read_bytes() == original_content
+
+ def test_load_config_still_rejects_invalid_toml_after_bom_normalization(self):
+ """BOM 兼容不能掩盖真实语法错误,失败时应保留明确诊断日志。"""
+ with TemporaryDirectory() as temp_dir:
+ config_path = Path(temp_dir) / "config.toml"
+ config_path.write_text("[app\nvideo_source = \"pexels\"\n", encoding="utf-8")
+
+ with (
+ patch.object(config, "config_file", str(config_path)),
+ patch.object(config.logger, "error") as error_mock,
+ ):
+ try:
+ config.load_config()
+ except toml.TomlDecodeError:
+ pass
+ else:
+ raise AssertionError("expected invalid TOML to be rejected")
+
+ error_message = str(error_mock.call_args.args[0])
+ assert str(config_path) in error_message
+ assert "TomlDecodeError" in error_message
+
def test_kimi_uses_current_default_model(self):
"""Kimi 未配置模型覆盖值时,应使用当前发布版本的默认模型。"""
provider = get_llm_provider("moonshot")