fix(设置): 恢复上传提交失败前的公司标志文件
数据库提交失败时恢复被覆盖的文件,或移除本次新文件。 文件恢复失败保留原始异常,并补充三种扩展名场景的回归测试。
This commit is contained in:
1 parent
67adecbf1b
commit
c684a44753
2 files changed
+46
-7
No files matched your search
@@ -126,14 +126,23 @@ def save_logo(
|
||||
setting = get_setting(session)
|
||||
old_path = setting.logo_path
|
||||
|
||||
# 先写新文件、commit 成功后再清旧扩展名(若把清理放在 commit 之前,
|
||||
# commit 失败会让库指旧路径而旧文件已删——断链无补偿);同扩展名
|
||||
# 由写入覆盖,无需也不会误删新文件
|
||||
# 保留同名旧文件内容,避免覆盖后数据库提交失败造成 logo 断链;
|
||||
# 不同扩展名的新文件在提交失败时删除,不改变当前生效文件。
|
||||
target = _logo_dir() / f"logo.{ext}"
|
||||
target.write_bytes(content)
|
||||
|
||||
setting.logo_path = f"logo/logo.{ext}"
|
||||
session.commit()
|
||||
previous_content = target.read_bytes() if target.exists() else None
|
||||
try:
|
||||
target.write_bytes(content)
|
||||
setting.logo_path = f"logo/logo.{ext}"
|
||||
session.commit()
|
||||
except Exception:
|
||||
try:
|
||||
if previous_content is None:
|
||||
target.unlink(missing_ok=True)
|
||||
else:
|
||||
target.write_bytes(previous_content)
|
||||
except OSError:
|
||||
logger.warning("logo 数据库回滚后的文件恢复失败", exc_info=True)
|
||||
raise
|
||||
|
||||
old_ext = Path(old_path).suffix.lstrip(".") if old_path else None
|
||||
if old_ext and old_ext != ext:
|
||||
|
||||
Reference in new issue
Block a user