Files
AstrBot/astrbot/core/utils/path_util.py
Weilong Liao 7c366a708b fix: unify media reference handling (#8764)
* fix: unify media reference handling

* fix: accept bare base64 record media refs

* chore: update agents.md

* fix: unify file URI handling across media components and utilities

* fix: unify media reference type handling with MediaRefStr alias

* Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Update astrbot/core/platform/sources/discord/discord_platform_adapter.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* fix: unify media handling and improve base64 decoding across components

* fix: simplify client_kwargs type definition and enhance media message handling in platform adapter

* fix: unify media utility documentation and enhance function descriptions

* perf: drop "pilk" requirement, improve audio outbound for tencent-related IM apps which using silk

* fix: unify Tencent Silk audio handling and enhance media resolver functionality

---

- Centralize media reference materialization and base64 resolution for local paths, http(s), base64://, data URIs, and legacy bare base64 payloads.
- Normalize incoming Record audio to wav and Image media to temporary jpg during preprocess, with event-scoped cleanup.
- Reuse the shared media resolver across OpenAI, Gemini, Anthropic, MiMo, DeerFlow, STT, and platform media paths while sanitizing logs and cleaning temporary conversion outputs.
- Ensure generated TTS audio is tracked for cleanup after the event finishes.

fix #8676
fix #8543
fix #7588
fix #7580
fix #8030
fix #8034
fix #7461
fix #7565
fix #6509
fix #7144
fix #7795



---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2026-06-14 10:37:16 +08:00

73 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from astrbot.core import logger
from astrbot.core.utils.media_utils import file_uri_to_path, is_file_uri
def path_Mapping(mappings, srcPath: str) -> str:
"""路径映射处理函数。尝试支援 Windows 和 Linux 的路径映射。
Args:
mappings: 映射规则列表
srcPath: 原路径
Returns:
str: 处理后的路径
"""
for mapping in mappings:
rule = mapping.split(":")
if len(rule) == 2:
from_, to_ = mapping.split(":")
elif len(rule) > 4 or len(rule) == 1:
# 切割后大于4个项目或者只有1个项目那肯定是错误的只能是234个项目
logger.warning(f"路径映射规则错误: {mapping}")
continue
# rule.len == 3 or 4
elif os.path.exists(rule[0] + ":" + rule[1]):
# 前面两个项目合并路径存在说明是本地Window路径。后面一个或两个项目组成的路径本地大概率无法解析直接拼接
from_ = rule[0] + ":" + rule[1]
if len(rule) == 3:
to_ = rule[2]
else:
to_ = rule[2] + ":" + rule[3]
else:
# 前面两个项目合并路径不存在说明第一个项目是本地Linux路径后面一个或两个项目直接拼接。
from_ = rule[0]
if len(rule) == 3:
to_ = rule[1] + ":" + rule[2]
else:
# 这种情况下存在四个项目,说明规则也是错误的
logger.warning(f"路径映射规则错误: {mapping}")
continue
from_ = from_.removesuffix("/")
from_ = from_.removesuffix("\\")
to_ = to_.removesuffix("/")
to_ = to_.removesuffix("\\")
# logger.debug(f"\t路径映射-规则(处理): {from_} -> {to_}")
url = file_uri_to_path(srcPath) if is_file_uri(srcPath) else srcPath
if url.startswith(from_):
srcPath = url.replace(from_, to_, 1)
if ":" in srcPath:
# Windows路径处理
srcPath = srcPath.replace("/", "\\")
else:
has_replaced_processed = False
if srcPath.startswith("."):
# 相对路径处理。如果是相对路径可能是Linux路径也可能是Windows路径
sign = srcPath[1]
# 处理两个点的情况
if sign == ".":
sign = srcPath[2]
if sign == "/":
srcPath = srcPath.replace("\\", "/")
has_replaced_processed = True
elif sign == "\\":
srcPath = srcPath.replace("/", "\\")
has_replaced_processed = True
if not has_replaced_processed:
# 如果不是相对路径或不能处理默认按照Linux路径处理
srcPath = srcPath.replace("\\", "/")
logger.info(f"路径映射: {url} -> {srcPath}")
return srcPath
return srcPath