Просмотр исходного кода

fix: backup docx files alongside bank.db on import and metadata update

nonvioce 3 месяцев назад
Родитель
Сommit
1ae26ded4c
2 измененных файлов с 18 добавлено и 5 удалено
  1. 4 5
      tiku-server/app/routers/import_.py
  2. 14 0
      tiku-server/app/routers/problems.py

+ 4 - 5
tiku-server/app/routers/import_.py

@@ -82,6 +82,7 @@ def _process_import(task_id: str, tmp_path: Path, import_type: str, source_name:
         imported = []
         skipped = []
         errors = []
+        imported_files = []  # 记录导入的 docx 文件路径,用于备份
 
         for i, q in enumerate(questions):
             update_import_task(conn, task_id, completed=i + 1, message=f"处理第 {i + 1}/{total} 题...")
@@ -118,9 +119,7 @@ def _process_import(task_id: str, tmp_path: Path, import_type: str, source_name:
                         body = extract_plain_text(docx_path)
                         update_fts_body(conn2, pid, body)
                         imported.append({"num": q["question_num"], "id": pid, "type": info["question_type"]})
-
-                        # 标记需要备份的文件
-                        # (简化处理:操作后统一备份)
+                        imported_files.append(str(docx_path))
                 finally:
                     conn2.close()
 
@@ -141,9 +140,9 @@ def _process_import(task_id: str, tmp_path: Path, import_type: str, source_name:
 
         update_import_task(conn, task_id, status="done", message="导入完成", result=result)
 
-        # 自动备份
+        # 自动备份(bank.db + 导入的 docx)
         try:
-            auto_backup(config)
+            auto_backup(config, changed_files=imported_files if imported_files else None)
         except Exception:
             pass
 

+ 14 - 0
tiku-server/app/routers/problems.py

@@ -234,6 +234,13 @@ def update_metadata(request: Request, problem_id: str, body: dict, _=Depends(ver
         if docx_path.exists():
             _update_docx_props(docx_path, body)
 
+        # 自动备份
+        try:
+            from app.services.backup import auto_backup
+            auto_backup(config, changed_files=[str(config.problems_dir / f"{problem_id}.docx")])
+        except Exception:
+            pass
+
         return {"ok": True}
     finally:
         conn.close()
@@ -264,6 +271,13 @@ def update_knowledge(request: Request, problem_id: str, body: dict, _=Depends(ve
         from app.services.indexer import incremental_update
         incremental_update(config)
 
+        # 自动备份
+        try:
+            from app.services.backup import auto_backup
+            auto_backup(config, changed_files=[str(config.problems_dir / f"{problem_id}.docx")])
+        except Exception:
+            pass
+
         return {"ok": True}
     finally:
         conn.close()