77- 版本匹配时也需要用户确认
88"""
99
10+ import asyncio
1011import json
1112import os
1213import shutil
@@ -364,7 +365,7 @@ async def import_all(
364365 """
365366 result = ImportResult ()
366367
367- if not os .path .exists ( zip_path ):
368+ if not await asyncio . to_thread ( os .path .exists , zip_path ):
368369 result .add_error (f"备份文件不存在: { zip_path } " )
369370 return result
370371
@@ -446,12 +447,13 @@ async def import_all(
446447 try :
447448 config_content = zf .read ("config/cmd_config.json" )
448449 # 备份现有配置
449- if os .path .exists ( self .config_path ):
450+ if await asyncio . to_thread ( os .path .exists , self .config_path ):
450451 backup_path = f"{ self .config_path } .bak"
451452 shutil .copy2 (self .config_path , backup_path )
452453
453- with open (self .config_path , "wb" ) as f :
454- f .write (config_content )
454+ await asyncio .to_thread (
455+ Path (self .config_path ).write_bytes , config_content
456+ )
455457 result .imported_files ["config" ] = 1
456458 except Exception as e :
457459 result .add_warning (f"导入配置文件失败: { e } " )
@@ -753,8 +755,8 @@ async def _import_knowledge_bases(
753755 if faiss_path in zf .namelist ():
754756 try :
755757 target_path = kb_dir / "index.faiss"
756- with zf .open (faiss_path ) as src , open ( target_path , "wb" ) as dst :
757- dst . write ( src .read ())
758+ with zf .open (faiss_path ) as src :
759+ await asyncio . to_thread ( target_path . write_bytes , src .read ())
758760 except Exception as e :
759761 result .add_warning (f"导入知识库 { kb_id } 的 FAISS 索引失败: { e } " )
760762
@@ -766,8 +768,8 @@ async def _import_knowledge_bases(
766768 rel_path = name [len (media_prefix ) :]
767769 target_path = kb_dir / rel_path
768770 target_path .parent .mkdir (parents = True , exist_ok = True )
769- with zf .open (name ) as src , open ( target_path , "wb" ) as dst :
770- dst . write ( src .read ())
771+ with zf .open (name ) as src :
772+ await asyncio . to_thread ( target_path . write_bytes , src .read ())
771773 except Exception as e :
772774 result .add_warning (f"导入媒体文件 { name } 失败: { e } " )
773775
@@ -828,8 +830,8 @@ async def _import_attachments(
828830 target_path = attachments_dir / os .path .basename (name )
829831
830832 target_path .parent .mkdir (parents = True , exist_ok = True )
831- with zf .open (name ) as src , open ( target_path , "wb" ) as dst :
832- dst . write ( src .read ())
833+ with zf .open (name ) as src :
834+ await asyncio . to_thread ( target_path . write_bytes , src .read ())
833835 count += 1
834836 except Exception as e :
835837 logger .warning (f"导入附件 { name } 失败: { e } " )
@@ -885,15 +887,15 @@ async def _import_directories(
885887 continue
886888
887889 # 备份现有目录(如果存在)
888- if target_dir .exists ( ):
890+ if await asyncio . to_thread ( target_dir .exists ):
889891 backup_path = Path (f"{ target_dir } .bak" )
890- if backup_path .exists ( ):
892+ if await asyncio . to_thread ( backup_path .exists ):
891893 shutil .rmtree (backup_path )
892894 shutil .move (str (target_dir ), str (backup_path ))
893895 logger .debug (f"已备份现有目录 { target_dir } 到 { backup_path } " )
894896
895897 # 创建目标目录
896- target_dir .mkdir ( parents = True , exist_ok = True )
898+ await asyncio . to_thread ( target_dir .mkdir , parents = True , exist_ok = True )
897899
898900 # 解压文件
899901 for name in dir_files :
@@ -906,8 +908,8 @@ async def _import_directories(
906908 target_path = target_dir / rel_path
907909 target_path .parent .mkdir (parents = True , exist_ok = True )
908910
909- with zf .open (name ) as src , open ( target_path , "wb" ) as dst :
910- dst . write ( src .read ())
911+ with zf .open (name ) as src :
912+ await asyncio . to_thread ( target_path . write_bytes , src .read ())
911913 file_count += 1
912914 except Exception as e :
913915 result .add_warning (f"导入文件 { name } 失败: { e } " )
0 commit comments