4040from astrbot .core .utils .io import download_file , download_image_by_url , file_to_base64
4141
4242
43+ def _absolute_path (path : str ) -> str :
44+ return os .path .abspath (path )
45+
46+
47+ def _absolute_path_if_exists (path : str | None ) -> str | None :
48+ if not path or not os .path .exists (path ):
49+ return None
50+ return os .path .abspath (path )
51+
52+
4353class ComponentType (StrEnum ):
4454 # Basic Segment Types
4555 Plain = "Plain" # plain text message
@@ -159,17 +169,18 @@ async def convert_to_file_path(self) -> str:
159169 return self .file [8 :]
160170 if self .file .startswith ("http" ):
161171 file_path = await download_image_by_url (self .file )
162- return await asyncio .to_thread (os . path . abspath , file_path )
172+ return await asyncio .to_thread (_absolute_path , file_path )
163173 if self .file .startswith ("base64://" ):
164174 bs64_data = self .file .removeprefix ("base64://" )
165175 image_bytes = base64 .b64decode (bs64_data )
166176 file_path = os .path .join (
167177 get_astrbot_temp_path (), f"recordseg_{ uuid .uuid4 ()} .jpg"
168178 )
169179 await asyncio .to_thread (Path (file_path ).write_bytes , image_bytes )
170- return await asyncio .to_thread (os .path .abspath , file_path )
171- if await asyncio .to_thread (os .path .exists , self .file ):
172- return await asyncio .to_thread (os .path .abspath , self .file )
180+ return await asyncio .to_thread (_absolute_path , file_path )
181+ local_path = await asyncio .to_thread (_absolute_path_if_exists , self .file )
182+ if local_path :
183+ return local_path
173184 raise Exception (f"not a valid file: { self .file } " )
174185
175186 async def convert_to_base64 (self ) -> str :
@@ -189,10 +200,11 @@ async def convert_to_base64(self) -> str:
189200 bs64_data = await file_to_base64 (file_path )
190201 elif self .file .startswith ("base64://" ):
191202 bs64_data = self .file
192- elif await asyncio .to_thread (os .path .exists , self .file ):
193- bs64_data = await file_to_base64 (self .file )
194203 else :
195- raise Exception (f"not a valid file: { self .file } " )
204+ try :
205+ bs64_data = await file_to_base64 (self .file )
206+ except OSError as exc :
207+ raise Exception (f"not a valid file: { self .file } " ) from exc
196208 bs64_data = bs64_data .removeprefix ("base64://" )
197209 return bs64_data
198210
@@ -256,11 +268,15 @@ async def convert_to_file_path(self) -> str:
256268 get_astrbot_temp_path (), f"videoseg_{ uuid .uuid4 ().hex } "
257269 )
258270 await download_file (url , video_file_path )
259- if await asyncio .to_thread (os .path .exists , video_file_path ):
260- return await asyncio .to_thread (os .path .abspath , video_file_path )
271+ local_path = await asyncio .to_thread (
272+ _absolute_path_if_exists , video_file_path
273+ )
274+ if local_path :
275+ return local_path
261276 raise Exception (f"download failed: { url } " )
262- if await asyncio .to_thread (os .path .exists , url ):
263- return await asyncio .to_thread (os .path .abspath , url )
277+ local_path = await asyncio .to_thread (_absolute_path_if_exists , url )
278+ if local_path :
279+ return local_path
264280 raise Exception (f"not a valid file: { url } " )
265281
266282 async def register_to_file_service (self ) -> str :
@@ -449,17 +465,18 @@ async def convert_to_file_path(self) -> str:
449465 return url [8 :]
450466 if url .startswith ("http" ):
451467 image_file_path = await download_image_by_url (url )
452- return await asyncio .to_thread (os . path . abspath , image_file_path )
468+ return await asyncio .to_thread (_absolute_path , image_file_path )
453469 if url .startswith ("base64://" ):
454470 bs64_data = url .removeprefix ("base64://" )
455471 image_bytes = base64 .b64decode (bs64_data )
456472 image_file_path = os .path .join (
457473 get_astrbot_temp_path (), f"imgseg_{ uuid .uuid4 ()} .jpg"
458474 )
459475 await asyncio .to_thread (Path (image_file_path ).write_bytes , image_bytes )
460- return await asyncio .to_thread (os .path .abspath , image_file_path )
461- if await asyncio .to_thread (os .path .exists , url ):
462- return await asyncio .to_thread (os .path .abspath , url )
476+ return await asyncio .to_thread (_absolute_path , image_file_path )
477+ local_path = await asyncio .to_thread (_absolute_path_if_exists , url )
478+ if local_path :
479+ return local_path
463480 raise Exception (f"not a valid file: { url } " )
464481
465482 async def convert_to_base64 (self ) -> str :
@@ -480,10 +497,11 @@ async def convert_to_base64(self) -> str:
480497 bs64_data = await file_to_base64 (image_file_path )
481498 elif url .startswith ("base64://" ):
482499 bs64_data = url
483- elif await asyncio .to_thread (os .path .exists , url ):
484- bs64_data = await file_to_base64 (url )
485500 else :
486- raise Exception (f"not a valid file: { url } " )
501+ try :
502+ bs64_data = await file_to_base64 (url )
503+ except OSError as exc :
504+ raise Exception (f"not a valid file: { url } " ) from exc
487505 bs64_data = bs64_data .removeprefix ("base64://" )
488506 return bs64_data
489507
@@ -760,8 +778,9 @@ async def get_file(self, allow_return_url: bool = False) -> str:
760778 ):
761779 path = path [1 :]
762780
763- if await asyncio .to_thread (os .path .exists , path ):
764- return await asyncio .to_thread (os .path .abspath , path )
781+ local_path = await asyncio .to_thread (_absolute_path_if_exists , path )
782+ if local_path :
783+ return local_path
765784
766785 if self .url :
767786 await self ._download_file ()
@@ -776,7 +795,7 @@ async def get_file(self, allow_return_url: bool = False) -> str:
776795 and path [2 ] == ":"
777796 ):
778797 path = path [1 :]
779- return await asyncio .to_thread (os . path . abspath , path )
798+ return await asyncio .to_thread (_absolute_path , path )
780799
781800 return ""
782801
@@ -792,7 +811,7 @@ async def _download_file(self) -> None:
792811 filename = f"fileseg_{ uuid .uuid4 ().hex } "
793812 file_path = os .path .join (download_dir , filename )
794813 await download_file (self .url , file_path )
795- self .file_ = await asyncio .to_thread (os . path . abspath , file_path )
814+ self .file_ = await asyncio .to_thread (_absolute_path , file_path )
796815
797816 async def register_to_file_service (self ) -> str :
798817 """将文件注册到文件服务。
0 commit comments