Skip to content

Commit d6c5edf

Browse files
committed
fix: resolve record component file path from url or file
1 parent 4398947 commit d6c5edf

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

astrbot/core/message/components.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,26 @@ async def convert_to_file_path(self) -> str:
146146
str: 语音的本地路径,以绝对路径表示。
147147
148148
"""
149-
if not self.file:
150-
raise Exception(f"not a valid file: {self.file}")
151-
if self.file.startswith("file:///"):
152-
return self.file[8:]
153-
if self.file.startswith("http"):
154-
file_path = await download_image_by_url(self.file)
149+
url = self.url or self.file
150+
if not url:
151+
raise Exception(f"not a valid file: {url}")
152+
if url.startswith("file:///"):
153+
return url[8:]
154+
if url.startswith("http"):
155+
file_path = await download_image_by_url(url)
155156
return os.path.abspath(file_path)
156-
if self.file.startswith("base64://"):
157-
bs64_data = self.file.removeprefix("base64://")
157+
if url.startswith("base64://"):
158+
bs64_data = url.removeprefix("base64://")
158159
image_bytes = base64.b64decode(bs64_data)
159160
file_path = os.path.join(
160-
get_astrbot_temp_path(), f"recordseg_{uuid.uuid4()}.jpg"
161+
get_astrbot_temp_path(), f"recordseg_{uuid.uuid4()}.amr"
161162
)
162163
with open(file_path, "wb") as f:
163164
f.write(image_bytes)
164165
return os.path.abspath(file_path)
165-
if os.path.exists(self.file):
166-
return os.path.abspath(self.file)
167-
raise Exception(f"not a valid file: {self.file}")
166+
if os.path.exists(url):
167+
return os.path.abspath(url)
168+
raise Exception(f"not a valid file: {url}")
168169

169170
async def convert_to_base64(self) -> str:
170171
"""将语音统一转换为 base64 编码。这个方法避免了手动判断语音数据类型,直接返回语音数据的 base64 编码。
@@ -173,20 +174,20 @@ async def convert_to_base64(self) -> str:
173174
str: 语音的 base64 编码,不以 base64:// 或者 data:image/jpeg;base64, 开头。
174175
175176
"""
176-
# convert to base64
177-
if not self.file:
178-
raise Exception(f"not a valid file: {self.file}")
179-
if self.file.startswith("file:///"):
180-
bs64_data = file_to_base64(self.file[8:])
181-
elif self.file.startswith("http"):
182-
file_path = await download_image_by_url(self.file)
177+
url = self.url or self.file
178+
if not url:
179+
raise Exception(f"not a valid file: {url}")
180+
if url.startswith("file:///"):
181+
bs64_data = file_to_base64(url[8:])
182+
elif url.startswith("http"):
183+
file_path = await download_image_by_url(url)
183184
bs64_data = file_to_base64(file_path)
184-
elif self.file.startswith("base64://"):
185-
bs64_data = self.file
186-
elif os.path.exists(self.file):
187-
bs64_data = file_to_base64(self.file)
185+
elif url.startswith("base64://"):
186+
bs64_data = url
187+
elif os.path.exists(url):
188+
bs64_data = file_to_base64(url)
188189
else:
189-
raise Exception(f"not a valid file: {self.file}")
190+
raise Exception(f"not a valid file: {url}")
190191
bs64_data = bs64_data.removeprefix("base64://")
191192
return bs64_data
192193

0 commit comments

Comments
 (0)