| import os |
|
|
| from astrbot.core import logger |
|
|
|
|
| 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: |
| |
| logger.warning(f"路径映射规则错误: {mapping}") |
| continue |
| |
| elif os.path.exists(rule[0] + ":" + rule[1]): |
| |
| from_ = rule[0] + ":" + rule[1] |
| if len(rule) == 3: |
| to_ = rule[2] |
| else: |
| to_ = rule[2] + ":" + rule[3] |
| else: |
| |
| 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("\\") |
| |
|
|
| url = srcPath.removeprefix("file://") |
| if url.startswith(from_): |
| srcPath = url.replace(from_, to_, 1) |
| if ":" in srcPath: |
| |
| srcPath = srcPath.replace("/", "\\") |
| else: |
| has_replaced_processed = False |
| if srcPath.startswith("."): |
| |
| 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: |
| |
| srcPath = srcPath.replace("\\", "/") |
| logger.info(f"路径映射: {url} -> {srcPath}") |
| return srcPath |
| return srcPath |
|
|