Skip to content

Commit 147c99b

Browse files
authored
fix: upload task display error (#464)
1 parent 75eb503 commit 147c99b

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

backend/services/data-management-service/src/main/java/com/datamate/datamanagement/application/DatasetFileApplicationService.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,41 @@ public PagedResponse<DatasetFile> getDatasetFilesWithDirectory(String datasetId,
112112
return PagedResponse.of(new Page<>(page, size));
113113
}
114114
String datasetPath = dataset.getPath();
115-
Path queryPath = Path.of(dataset.getPath() + File.separator + prefix);
115+
116+
// 规范化 prefix:去掉首尾斜杠,处理空字符串
117+
prefix = Optional.ofNullable(prefix).orElse("").trim();
118+
prefix = prefix.replace("\\", "/");
119+
while (prefix.startsWith("/")) {
120+
prefix = prefix.substring(1);
121+
}
122+
while (prefix.endsWith("/")) {
123+
prefix = prefix.substring(0, prefix.length() - 1);
124+
}
125+
126+
// 使用 Path API 安全地构建路径
127+
Path basePath = Paths.get(datasetPath);
128+
Path queryPath = prefix.isEmpty() ? basePath : basePath.resolve(prefix).normalize();
129+
130+
// 确保查询路径在数据集路径下(防止路径遍历)
131+
if (!queryPath.startsWith(basePath)) {
132+
log.warn("Invalid prefix path: datasetId={}, prefix={}, queryPath={}", datasetId, prefix, queryPath);
133+
return PagedResponse.of(new Page<>(page, size));
134+
}
135+
116136
Map<String, DatasetFile> datasetFilesMap = datasetFileRepository.findAllByDatasetId(datasetId)
117137
.stream().collect(Collectors.toMap(DatasetFile::getFilePath, Function.identity()));
138+
139+
// 检查路径是否存在
140+
if (!Files.exists(queryPath)) {
141+
log.warn("Dataset path does not exist: datasetId={}, path={}, queryPath={}", datasetId, datasetPath, queryPath);
142+
return PagedResponse.of(new Page<>(page, size));
143+
}
144+
145+
if (!Files.isDirectory(queryPath)) {
146+
log.warn("Dataset path is not a directory: datasetId={}, queryPath={}", datasetId, queryPath);
147+
return PagedResponse.of(new Page<>(page, size));
148+
}
149+
118150
try (Stream<Path> pathStream = Files.list(queryPath)) {
119151
List<Path> allFiles = pathStream
120152
.filter(path -> path.toString().startsWith(datasetPath))
@@ -140,7 +172,7 @@ public PagedResponse<DatasetFile> getDatasetFilesWithDirectory(String datasetId,
140172

141173
return PagedResponse.of(page, size, total, totalPages, datasetFiles);
142174
} catch (IOException e) {
143-
log.warn("list dataset path error");
175+
log.error("Failed to list dataset files: datasetId={}, queryPath={}", datasetId, queryPath, e);
144176
return PagedResponse.of(new Page<>(page, size));
145177
}
146178
}

frontend/src/hooks/useSliceUpload.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ export function useFileSliceUpload(
2525
const { dataset } = detail;
2626
const title = `${t('hooks.sliceUpload.uploadDataset')}: ${dataset.name} `;
2727
const controller = new AbortController();
28+
// 使用 dataset.id + 时间戳 + 随机数确保每个上传任务有唯一的 key
29+
const uniqueKey = `${dataset.id}_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
2830
const task: TaskItem = {
29-
key: dataset.id,
31+
key: uniqueKey,
32+
datasetId: dataset.id, // 保存原始 datasetId 用于 API 调用
3033
title,
3134
percent: 0,
3235
reqId: -1,
@@ -92,7 +95,7 @@ export function useFileSliceUpload(
9295
if (!task) {
9396
return;
9497
}
95-
const { reqId, key } = task;
98+
const { reqId, key, datasetId } = task;
9699
const { loaded, i, j, files, totalSize } = fileInfo;
97100
const formData = await buildFormData({
98101
file: files[i],
@@ -102,7 +105,8 @@ export function useFileSliceUpload(
102105
});
103106

104107
let newTask = { ...task };
105-
await uploadChunk(key, formData, {
108+
// 使用 datasetId 调用 API,key 用于唯一标识任务
109+
await uploadChunk(datasetId || key, formData, {
106110
onUploadProgress: (e) => {
107111
const loadedSize = loaded + e.loaded;
108112
const curPercent = Number((loadedSize / totalSize) * 100).toFixed(2);
@@ -120,10 +124,10 @@ export function useFileSliceUpload(
120124

121125
async function uploadFile({ task, files, totalSize }) {
122126
console.log('[useSliceUpload] Calling preUpload with prefix:', task.prefix);
123-
const { data: reqId } = await preUpload(task.key, {
127+
const { data: reqId } = await preUpload(task.datasetId || task.key, {
124128
totalFileNum: files.length,
125129
totalSize,
126-
datasetId: task.key,
130+
datasetId: task.datasetId || task.key,
127131
hasArchive: task.hasArchive,
128132
prefix: task.prefix,
129133
});

frontend/src/pages/DataManagement/dataset.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface DatasetTask {
9595

9696
export interface TaskItem {
9797
key: string;
98+
datasetId?: string; // 数据集 ID(用于 API 调用)
9899
title: string;
99100
percent: number;
100101
reqId: number;
@@ -104,4 +105,5 @@ export interface TaskItem {
104105
updateEvent?: string;
105106
size?: number;
106107
hasArchive?: boolean;
108+
prefix?: string; // 当前路径前缀
107109
}

0 commit comments

Comments
 (0)