Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public PagedResponse<DatasetFile> getDatasetFilesWithDirectory(String datasetId,
}

// 使用 Path API 安全地构建路径
Path basePath = Paths.get(datasetPath);
Path basePath = Paths.get(datasetPath).normalize();
Path queryPath = prefix.isEmpty() ? basePath : basePath.resolve(prefix).normalize();

// 确保查询路径在数据集路径下(防止路径遍历)
Expand All @@ -149,7 +149,7 @@ public PagedResponse<DatasetFile> getDatasetFilesWithDirectory(String datasetId,

try (Stream<Path> pathStream = Files.list(queryPath)) {
List<Path> allFiles = pathStream
.filter(path -> path.toString().startsWith(datasetPath))
.filter(path -> path.normalize().startsWith(basePath))
.sorted(Comparator
.comparing((Path path) -> !Files.isDirectory(path))
.thenComparing(path -> path.getFileName().toString()))
Expand Down Expand Up @@ -694,7 +694,7 @@ public void createDirectory(String datasetId, CreateDirectoryRequest req) {
throw BusinessException.of(CommonErrorCode.PARAM_ERROR);
}

Path basePath = Paths.get(datasetPath);
Path basePath = Paths.get(datasetPath).normalize();
Path targetPath = parentPrefix.isEmpty()
? basePath.resolve(directoryName)
: basePath.resolve(parentPrefix).resolve(directoryName);
Expand Down Expand Up @@ -1149,19 +1149,29 @@ private void addFile(String sourPath, String targetPath, boolean softAdd) {

private static DatasetFile getDatasetFileForAdd(AddFilesRequest req, AddFilesRequest.FileRequest file,
Dataset dataset, ObjectMapper objectMapper) throws JsonProcessingException {
Path sourcePath = Paths.get(file.getFilePath());
Path sourcePath = Paths.get(file.getFilePath()).normalize();
File sourceFile = sourcePath.toFile();
file.getMetadata().put("softAdd", req.isSoftAdd());
LocalDateTime currentTime = LocalDateTime.now();
String fileName = sourcePath.getFileName().toString();

Path datasetPath = Paths.get(dataset.getPath()).normalize();
String filePath;
if (sourcePath.startsWith(datasetPath)) {
// 源文件已在数据集目录内,直接使用源文件路径,不复制
filePath = sourcePath.toString();
} else {
// 源文件在数据集目录外,构造目标路径
filePath = Paths.get(dataset.getPath(), req.getPrefix(), fileName).toString();
}

return DatasetFile.builder()
.id(UUID.randomUUID().toString())
.datasetId(dataset.getId())
.fileName(fileName)
.fileType(AnalyzerUtils.getExtension(fileName))
.fileSize(sourceFile.length())
.filePath(Paths.get(dataset.getPath(), req.getPrefix(), fileName).toString())
.filePath(filePath)
.uploadTime(currentTime)
.lastAccessTime(currentTime)
.metadata(objectMapper.writeValueAsString(file.getMetadata()))
Expand Down
Loading