fix 文件路径包含特殊字符时转义处理

This commit is contained in:
thsrite
2024-06-22 17:30:12 +08:00
parent 779196a8fa
commit c11f5008af
3 changed files with 28 additions and 5 deletions

View File

@@ -33,7 +33,7 @@ MoviePilot三方插件市场https://github.com/thsrite/MoviePilot-Plugins/
- [自定义命令 v1.7](docs%2FCustomCommand.md)
- docker自定义任务 v1.3
- 插件彻底卸载 v1.0
- 实时软连接 v1.6
- 实时软连接 v1.7
- 订阅规则自动填充 v2.7
- Emby元数据刷新 v1.1
- Emby媒体标签 v1.2

View File

@@ -378,11 +378,12 @@
"name": "实时软连接",
"description": "监控目录文件变化,媒体文件软连接,其他文件可选复制。",
"labels": "文件管理",
"version": "1.6",
"version": "1.7",
"icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/softlink.png",
"author": "thsrite",
"level": 1,
"history": {
"v1.7": "文件路径包含特殊字符时转义处理",
"v1.6": "bug修复",
"v1.5": "优化性能,提高处理速度",
"v1.4": "支持自定义视频格式",

View File

@@ -52,7 +52,7 @@ class FileSoftLink(_PluginBase):
# 插件图标
plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/softlink.png"
# 插件版本
plugin_version = "1.6"
plugin_version = "1.7"
# 插件作者
plugin_author = "thsrite"
# 作者主页
@@ -318,7 +318,14 @@ class FileSoftLink(_PluginBase):
# 查询转移目的目录
target: Path = self._dirconf.get(mon_path)
target_file = str(file_path).replace(str(mon_path), str(target))
# 去除文件名中的特殊字符
sanitized_file_name = self.remove_special_chars(file_path.name)
sanitized_file = file_path.with_name(sanitized_file_name)
target_file = str(sanitized_file).replace(str(mon_path), str(target))
# 转义原路径
file_path = self.escape_path(str(file_path))
# 如果是文件夹
if Path(target_file).is_dir():
@@ -327,7 +334,11 @@ class FileSoftLink(_PluginBase):
os.makedirs(target_file)
return
else:
# 文件
# 未开启复制非媒体文件且文件类型不在媒体类型列表中,跳过处理
if not self._copy_files and Path(target_file).suffix.lower() not in [ext.strip() for ext in
self._rmt_mediaext.split(",")]:
return
# 文件
if Path(target_file).exists():
logger.info(f"目标文件 {target_file} 已存在")
return
@@ -349,6 +360,17 @@ class FileSoftLink(_PluginBase):
except Exception as e:
logger.error("软连接发生错误:%s - %s" % (str(e), traceback.format_exc()))
@staticmethod
def remove_special_chars(filename):
"""去除字符串中的特殊字符,只保留字母、数字、点、下划线和横线。"""
return re.sub(r'[^a-zA-Z0-9._-]', '', filename)
@staticmethod
def escape_path(path):
"""转义路径中的空格和特殊字符。"""
return path.replace(' ', '\\ ').replace('(', '\\(').replace(')', '\\)').replace('\'', '\\\'').replace('"',
'\\"')
def get_state(self) -> bool:
return self._enabled