feat(export): 添加逻辑路径构建功能支持文件管理

- 在导出服务中实现_build_logical_path方法用于构建相对路径
- 更新数据集文件记录以包含logical_path字段
- 在比率任务服务中实现build_logical_path静态方法
- 将逻辑路径信息添加到数据集文件记录中
- 规范化路径处理并替换反斜杠为正斜杠
- 添加无效路径验证防止目录遍历安全问题
This commit is contained in:
2026-02-06 18:46:44 +08:00
parent 05752678cc
commit e862925a06
2 changed files with 22 additions and 0 deletions

View File

@@ -74,6 +74,7 @@ class SynthesisDatasetExporter:
file_path = os.path.join(base_path, archived_file_name)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
self._write_jsonl(file_path, records)
logical_path = self._build_logical_path(base_path, file_path)
# 计算文件大小
try:
@@ -85,6 +86,7 @@ class SynthesisDatasetExporter:
dataset_id=dataset.id,
file_name=archived_file_name,
file_path=file_path,
logical_path=logical_path,
file_type="jsonl",
file_size=file_size,
last_access_time=datetime.datetime.now(),
@@ -158,3 +160,12 @@ class SynthesisDatasetExporter:
raise SynthesisExportError("Dataset path is empty")
os.makedirs(dataset.path, exist_ok=True)
return dataset.path
@staticmethod
def _build_logical_path(dataset_path: str, file_path: str) -> str:
normalized_dataset_path = os.path.abspath(dataset_path)
normalized_file_path = os.path.abspath(file_path)
relative_path = os.path.relpath(normalized_file_path, normalized_dataset_path).replace("\\", "/").strip()
if relative_path in ("", ".") or relative_path.startswith("../"):
raise SynthesisExportError(f"Invalid logical path generated for file: {file_path}")
return relative_path