You've already forked DataMate
Compare commits
13 Commits
88b1383653
...
lsf
| Author | SHA1 | Date | |
|---|---|---|---|
| cda22a720c | |||
| 394e2bda18 | |||
| 4220284f5a | |||
| 8415166949 | |||
| 078f303f57 | |||
| 50f2da5503 | |||
| 3af1daf8b6 | |||
| 7c7729434b | |||
| 17a62cd3c2 | |||
| f381d641ab | |||
| c8611d29ff | |||
| 147beb1ec7 | |||
| 699031dae7 |
@@ -470,6 +470,23 @@ paths:
|
|||||||
'200':
|
'200':
|
||||||
description: 上传成功
|
description: 上传成功
|
||||||
|
|
||||||
|
/data-management/datasets/upload/cancel-upload/{reqId}:
|
||||||
|
put:
|
||||||
|
tags: [ DatasetFile ]
|
||||||
|
operationId: cancelUpload
|
||||||
|
summary: 取消上传
|
||||||
|
description: 取消预上传请求并清理临时分片
|
||||||
|
parameters:
|
||||||
|
- name: reqId
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
description: 预上传请求ID
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: 取消成功
|
||||||
|
|
||||||
/data-management/dataset-types:
|
/data-management/dataset-types:
|
||||||
get:
|
get:
|
||||||
operationId: getDatasetTypes
|
operationId: getDatasetTypes
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.datamate.datamanagement.application;
|
package com.datamate.datamanagement.application;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.datamate.common.domain.utils.ChunksSaver;
|
import com.datamate.common.domain.utils.ChunksSaver;
|
||||||
@@ -101,6 +102,7 @@ public class DatasetApplicationService {
|
|||||||
public Dataset updateDataset(String datasetId, UpdateDatasetRequest updateDatasetRequest) {
|
public Dataset updateDataset(String datasetId, UpdateDatasetRequest updateDatasetRequest) {
|
||||||
Dataset dataset = datasetRepository.getById(datasetId);
|
Dataset dataset = datasetRepository.getById(datasetId);
|
||||||
BusinessAssert.notNull(dataset, DataManagementErrorCode.DATASET_NOT_FOUND);
|
BusinessAssert.notNull(dataset, DataManagementErrorCode.DATASET_NOT_FOUND);
|
||||||
|
|
||||||
if (StringUtils.hasText(updateDatasetRequest.getName())) {
|
if (StringUtils.hasText(updateDatasetRequest.getName())) {
|
||||||
dataset.setName(updateDatasetRequest.getName());
|
dataset.setName(updateDatasetRequest.getName());
|
||||||
}
|
}
|
||||||
@@ -113,14 +115,31 @@ public class DatasetApplicationService {
|
|||||||
if (Objects.nonNull(updateDatasetRequest.getStatus())) {
|
if (Objects.nonNull(updateDatasetRequest.getStatus())) {
|
||||||
dataset.setStatus(updateDatasetRequest.getStatus());
|
dataset.setStatus(updateDatasetRequest.getStatus());
|
||||||
}
|
}
|
||||||
// 处理父数据集变更:始终调用 handleParentChange,以支持设置新的关联或清除关联
|
if (updateDatasetRequest.isParentDatasetIdProvided()) {
|
||||||
|
// 保存原始的 parentDatasetId 值,用于比较是否发生了变化
|
||||||
|
String originalParentDatasetId = dataset.getParentDatasetId();
|
||||||
|
|
||||||
|
// 处理父数据集变更:仅当请求显式包含 parentDatasetId 时处理
|
||||||
// handleParentChange 内部通过 normalizeParentId 方法将空字符串和 null 都转换为 null
|
// handleParentChange 内部通过 normalizeParentId 方法将空字符串和 null 都转换为 null
|
||||||
// 这样既支持设置新的父数据集,也支持清除关联
|
// 这样既支持设置新的父数据集,也支持清除关联
|
||||||
handleParentChange(dataset, updateDatasetRequest.getParentDatasetId());
|
handleParentChange(dataset, updateDatasetRequest.getParentDatasetId());
|
||||||
|
|
||||||
|
// 检查 parentDatasetId 是否发生了变化
|
||||||
|
if (!Objects.equals(originalParentDatasetId, dataset.getParentDatasetId())) {
|
||||||
|
// 使用 LambdaUpdateWrapper 显式地更新 parentDatasetId 字段
|
||||||
|
// 这样即使值为 null 也能被正确更新到数据库
|
||||||
|
datasetRepository.update(null, new LambdaUpdateWrapper<Dataset>()
|
||||||
|
.eq(Dataset::getId, datasetId)
|
||||||
|
.set(Dataset::getParentDatasetId, dataset.getParentDatasetId()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (StringUtils.hasText(updateDatasetRequest.getDataSource())) {
|
if (StringUtils.hasText(updateDatasetRequest.getDataSource())) {
|
||||||
// 数据源id不为空,使用异步线程进行文件扫盘落库
|
// 数据源id不为空,使用异步线程进行文件扫盘落库
|
||||||
processDataSourceAsync(dataset.getId(), updateDatasetRequest.getDataSource());
|
processDataSourceAsync(dataset.getId(), updateDatasetRequest.getDataSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新其他字段(不包括 parentDatasetId,因为它已经在上面的代码中更新了)
|
||||||
datasetRepository.updateById(dataset);
|
datasetRepository.updateById(dataset);
|
||||||
return dataset;
|
return dataset;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -505,6 +505,14 @@ public class DatasetFileApplicationService {
|
|||||||
saveFileInfoToDb(uploadResult, datasetId);
|
saveFileInfoToDb(uploadResult, datasetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消上传
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public void cancelUpload(String reqId) {
|
||||||
|
fileService.cancelUpload(reqId);
|
||||||
|
}
|
||||||
|
|
||||||
private void saveFileInfoToDb(FileUploadResult fileUploadResult, String datasetId) {
|
private void saveFileInfoToDb(FileUploadResult fileUploadResult, String datasetId) {
|
||||||
if (Objects.isNull(fileUploadResult.getSavedFile())) {
|
if (Objects.isNull(fileUploadResult.getSavedFile())) {
|
||||||
// 文件切片上传没有完成
|
// 文件切片上传没有完成
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.datamate.datamanagement.domain.model.dataset;
|
package com.datamate.datamanagement.domain.model.dataset;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||||
@@ -32,7 +31,6 @@ public class Dataset extends BaseEntity<String> {
|
|||||||
/**
|
/**
|
||||||
* 父数据集ID
|
* 父数据集ID
|
||||||
*/
|
*/
|
||||||
@TableField(updateStrategy = FieldStrategy.IGNORED)
|
|
||||||
private String parentDatasetId;
|
private String parentDatasetId;
|
||||||
/**
|
/**
|
||||||
* 数据集类型
|
* 数据集类型
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.datamate.datamanagement.interfaces.dto;
|
package com.datamate.datamanagement.interfaces.dto;
|
||||||
|
|
||||||
import com.datamate.datamanagement.common.enums.DatasetStatusType;
|
import com.datamate.datamanagement.common.enums.DatasetStatusType;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
import lombok.AccessLevel;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@@ -24,9 +26,18 @@ public class UpdateDatasetRequest {
|
|||||||
/** 归集任务id */
|
/** 归集任务id */
|
||||||
private String dataSource;
|
private String dataSource;
|
||||||
/** 父数据集ID */
|
/** 父数据集ID */
|
||||||
|
@Setter(AccessLevel.NONE)
|
||||||
private String parentDatasetId;
|
private String parentDatasetId;
|
||||||
|
@JsonIgnore
|
||||||
|
@Setter(AccessLevel.NONE)
|
||||||
|
private boolean parentDatasetIdProvided;
|
||||||
/** 标签列表 */
|
/** 标签列表 */
|
||||||
private List<String> tags;
|
private List<String> tags;
|
||||||
/** 数据集状态 */
|
/** 数据集状态 */
|
||||||
private DatasetStatusType status;
|
private DatasetStatusType status;
|
||||||
|
|
||||||
|
public void setParentDatasetId(String parentDatasetId) {
|
||||||
|
this.parentDatasetIdProvided = true;
|
||||||
|
this.parentDatasetId = parentDatasetId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.datamate.datamanagement.interfaces.rest;
|
||||||
|
|
||||||
|
import com.datamate.datamanagement.application.DatasetFileApplicationService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据集上传控制器
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RequestMapping("/data-management/datasets/upload")
|
||||||
|
public class DatasetUploadController {
|
||||||
|
|
||||||
|
private final DatasetFileApplicationService datasetFileApplicationService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消上传
|
||||||
|
*
|
||||||
|
* @param reqId 预上传请求ID
|
||||||
|
*/
|
||||||
|
@PutMapping("/cancel-upload/{reqId}")
|
||||||
|
public ResponseEntity<Void> cancelUpload(@PathVariable("reqId") String reqId) {
|
||||||
|
datasetFileApplicationService.cancelUpload(reqId);
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -74,6 +74,26 @@ public class FileService {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消上传
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public void cancelUpload(String reqId) {
|
||||||
|
if (reqId == null || reqId.isBlank()) {
|
||||||
|
throw BusinessException.of(CommonErrorCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
ChunkUploadPreRequest preRequest = chunkUploadRequestMapper.findById(reqId);
|
||||||
|
if (preRequest == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String uploadPath = preRequest.getUploadPath();
|
||||||
|
if (uploadPath != null && !uploadPath.isBlank()) {
|
||||||
|
File tempDir = new File(uploadPath, String.format(ChunksSaver.TEMP_DIR_NAME_FORMAT, preRequest.getId()));
|
||||||
|
ChunksSaver.deleteFolder(tempDir.getPath());
|
||||||
|
}
|
||||||
|
chunkUploadRequestMapper.deleteById(reqId);
|
||||||
|
}
|
||||||
|
|
||||||
private File uploadFile(ChunkUploadRequest fileUploadRequest, ChunkUploadPreRequest preRequest) {
|
private File uploadFile(ChunkUploadRequest fileUploadRequest, ChunkUploadPreRequest preRequest) {
|
||||||
File savedFile = ChunksSaver.saveFile(fileUploadRequest, preRequest);
|
File savedFile = ChunksSaver.saveFile(fileUploadRequest, preRequest);
|
||||||
preRequest.setTimeout(LocalDateTime.now().plusSeconds(DEFAULT_TIMEOUT));
|
preRequest.setTimeout(LocalDateTime.now().plusSeconds(DEFAULT_TIMEOUT));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { TaskItem } from "@/pages/DataManagement/dataset.model";
|
import { TaskItem } from "@/pages/DataManagement/dataset.model";
|
||||||
import { calculateSHA256, checkIsFilesExist } from "@/utils/file.util";
|
import { calculateSHA256, checkIsFilesExist, streamSplitAndUpload, StreamUploadResult } from "@/utils/file.util";
|
||||||
import { App } from "antd";
|
import { App } from "antd";
|
||||||
import { useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
|
|
||||||
@@ -9,17 +9,18 @@ export function useFileSliceUpload(
|
|||||||
uploadChunk,
|
uploadChunk,
|
||||||
cancelUpload,
|
cancelUpload,
|
||||||
}: {
|
}: {
|
||||||
preUpload: (id: string, params: any) => Promise<{ data: number }>;
|
preUpload: (id: string, params: Record<string, unknown>) => Promise<{ data: number }>;
|
||||||
uploadChunk: (id: string, formData: FormData, config: any) => Promise<any>;
|
uploadChunk: (id: string, formData: FormData, config: Record<string, unknown>) => Promise<unknown>;
|
||||||
cancelUpload: ((reqId: number) => Promise<any>) | null;
|
cancelUpload: ((reqId: number) => Promise<unknown>) | null;
|
||||||
},
|
},
|
||||||
showTaskCenter = true // 上传时是否显示任务中心
|
showTaskCenter = true, // 上传时是否显示任务中心
|
||||||
|
enableStreamUpload = true // 是否启用流式分割上传
|
||||||
) {
|
) {
|
||||||
const { message } = App.useApp();
|
const { message } = App.useApp();
|
||||||
const [taskList, setTaskList] = useState<TaskItem[]>([]);
|
const [taskList, setTaskList] = useState<TaskItem[]>([]);
|
||||||
const taskListRef = useRef<TaskItem[]>([]); // 用于固定任务顺序
|
const taskListRef = useRef<TaskItem[]>([]); // 用于固定任务顺序
|
||||||
|
|
||||||
const createTask = (detail: any = {}) => {
|
const createTask = (detail: Record<string, unknown> = {}) => {
|
||||||
const { dataset } = detail;
|
const { dataset } = detail;
|
||||||
const title = `上传数据集: ${dataset.name} `;
|
const title = `上传数据集: ${dataset.name} `;
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
@@ -68,7 +69,7 @@ export function useFileSliceUpload(
|
|||||||
// 携带前缀信息,便于刷新后仍停留在当前目录
|
// 携带前缀信息,便于刷新后仍停留在当前目录
|
||||||
window.dispatchEvent(
|
window.dispatchEvent(
|
||||||
new CustomEvent(task.updateEvent, {
|
new CustomEvent(task.updateEvent, {
|
||||||
detail: { prefix: (task as any).prefix },
|
detail: { prefix: task.prefix },
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -79,7 +80,7 @@ export function useFileSliceUpload(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function buildFormData({ file, reqId, i, j }) {
|
async function buildFormData({ file, reqId, i, j }: { file: { slices: Blob[]; name: string; size: number }; reqId: number; i: number; j: number }) {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
const { slices, name, size } = file;
|
const { slices, name, size } = file;
|
||||||
const checkSum = await calculateSHA256(slices[j]);
|
const checkSum = await calculateSHA256(slices[j]);
|
||||||
@@ -94,12 +95,18 @@ export function useFileSliceUpload(
|
|||||||
return formData;
|
return formData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadSlice(task: TaskItem, fileInfo) {
|
async function uploadSlice(task: TaskItem, fileInfo: { loaded: number; i: number; j: number; files: { slices: Blob[]; name: string; size: number }[]; totalSize: number }) {
|
||||||
if (!task) {
|
if (!task) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { reqId, key } = task;
|
const { reqId, key, controller } = task;
|
||||||
const { loaded, i, j, files, totalSize } = fileInfo;
|
const { loaded, i, j, files, totalSize } = fileInfo;
|
||||||
|
|
||||||
|
// 检查是否已取消
|
||||||
|
if (controller.signal.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
const formData = await buildFormData({
|
const formData = await buildFormData({
|
||||||
file: files[i],
|
file: files[i],
|
||||||
i,
|
i,
|
||||||
@@ -109,6 +116,7 @@ export function useFileSliceUpload(
|
|||||||
|
|
||||||
let newTask = { ...task };
|
let newTask = { ...task };
|
||||||
await uploadChunk(key, formData, {
|
await uploadChunk(key, formData, {
|
||||||
|
signal: controller.signal,
|
||||||
onUploadProgress: (e) => {
|
onUploadProgress: (e) => {
|
||||||
const loadedSize = loaded + e.loaded;
|
const loadedSize = loaded + e.loaded;
|
||||||
const curPercent = Number((loadedSize / totalSize) * 100).toFixed(2);
|
const curPercent = Number((loadedSize / totalSize) * 100).toFixed(2);
|
||||||
@@ -124,7 +132,7 @@ export function useFileSliceUpload(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadFile({ task, files, totalSize }) {
|
async function uploadFile({ task, files, totalSize }: { task: TaskItem; files: { slices: Blob[]; name: string; size: number; originFile: Blob }[]; totalSize: number }) {
|
||||||
console.log('[useSliceUpload] Calling preUpload with prefix:', task.prefix);
|
console.log('[useSliceUpload] Calling preUpload with prefix:', task.prefix);
|
||||||
const { data: reqId } = await preUpload(task.key, {
|
const { data: reqId } = await preUpload(task.key, {
|
||||||
totalFileNum: files.length,
|
totalFileNum: files.length,
|
||||||
@@ -140,9 +148,10 @@ export function useFileSliceUpload(
|
|||||||
reqId,
|
reqId,
|
||||||
isCancel: false,
|
isCancel: false,
|
||||||
cancelFn: () => {
|
cancelFn: () => {
|
||||||
task.controller.abort();
|
// 使用 newTask 的 controller 确保一致性
|
||||||
|
newTask.controller.abort();
|
||||||
cancelUpload?.(reqId);
|
cancelUpload?.(reqId);
|
||||||
if (task.updateEvent) window.dispatchEvent(new Event(task.updateEvent));
|
if (newTask.updateEvent) window.dispatchEvent(new Event(newTask.updateEvent));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
updateTaskList(newTask);
|
updateTaskList(newTask);
|
||||||
@@ -152,8 +161,16 @@ export function useFileSliceUpload(
|
|||||||
|
|
||||||
let loaded = 0;
|
let loaded = 0;
|
||||||
for (let i = 0; i < files.length; i++) {
|
for (let i = 0; i < files.length; i++) {
|
||||||
|
// 检查是否已取消
|
||||||
|
if (newTask.controller.signal.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
const { slices } = files[i];
|
const { slices } = files[i];
|
||||||
for (let j = 0; j < slices.length; j++) {
|
for (let j = 0; j < slices.length; j++) {
|
||||||
|
// 检查是否已取消
|
||||||
|
if (newTask.controller.signal.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
await uploadSlice(newTask, {
|
await uploadSlice(newTask, {
|
||||||
loaded,
|
loaded,
|
||||||
i,
|
i,
|
||||||
@@ -167,7 +184,7 @@ export function useFileSliceUpload(
|
|||||||
removeTask(newTask);
|
removeTask(newTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleUpload = async ({ task, files }) => {
|
const handleUpload = async ({ task, files }: { task: TaskItem; files: { slices: Blob[]; name: string; size: number; originFile: Blob }[] }) => {
|
||||||
const isErrorFile = await checkIsFilesExist(files);
|
const isErrorFile = await checkIsFilesExist(files);
|
||||||
if (isErrorFile) {
|
if (isErrorFile) {
|
||||||
message.error("文件被修改或删除,请重新选择文件上传");
|
message.error("文件被修改或删除,请重新选择文件上传");
|
||||||
@@ -193,10 +210,174 @@ export function useFileSliceUpload(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流式分割上传处理
|
||||||
|
* 用于大文件按行分割并立即上传的场景
|
||||||
|
*/
|
||||||
|
const handleStreamUpload = async ({ task, files }: { task: TaskItem; files: File[] }) => {
|
||||||
|
try {
|
||||||
|
console.log('[useSliceUpload] Starting stream upload for', files.length, 'files');
|
||||||
|
|
||||||
|
const totalSize = files.reduce((acc, file) => acc + file.size, 0);
|
||||||
|
|
||||||
|
// 存储所有文件的 reqId,用于取消上传
|
||||||
|
const reqIds: number[] = [];
|
||||||
|
|
||||||
|
const newTask: TaskItem = {
|
||||||
|
...task,
|
||||||
|
reqId: -1,
|
||||||
|
isCancel: false,
|
||||||
|
cancelFn: () => {
|
||||||
|
// 使用 newTask 的 controller 确保一致性
|
||||||
|
newTask.controller.abort();
|
||||||
|
// 取消所有文件的预上传请求
|
||||||
|
reqIds.forEach(id => cancelUpload?.(id));
|
||||||
|
if (newTask.updateEvent) window.dispatchEvent(new Event(newTask.updateEvent));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
updateTaskList(newTask);
|
||||||
|
|
||||||
|
let totalUploadedLines = 0;
|
||||||
|
let totalProcessedBytes = 0;
|
||||||
|
const results: StreamUploadResult[] = [];
|
||||||
|
|
||||||
|
// 逐个处理文件,每个文件单独调用 preUpload
|
||||||
|
for (let i = 0; i < files.length; i++) {
|
||||||
|
// 检查是否已取消
|
||||||
|
if (newTask.controller.signal.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
const file = files[i];
|
||||||
|
console.log(`[useSliceUpload] Processing file ${i + 1}/${files.length}: ${file.name}`);
|
||||||
|
|
||||||
|
const result = await streamSplitAndUpload(
|
||||||
|
file,
|
||||||
|
(formData, config) => uploadChunk(task.key, formData, {
|
||||||
|
...config,
|
||||||
|
signal: newTask.controller.signal,
|
||||||
|
}),
|
||||||
|
(currentBytes, totalBytes, uploadedLines) => {
|
||||||
|
// 检查是否已取消
|
||||||
|
if (newTask.controller.signal.aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新进度
|
||||||
|
const overallBytes = totalProcessedBytes + currentBytes;
|
||||||
|
const curPercent = Number((overallBytes / totalSize) * 100).toFixed(2);
|
||||||
|
|
||||||
|
const updatedTask: TaskItem = {
|
||||||
|
...newTask,
|
||||||
|
...taskListRef.current.find((item) => item.key === task.key),
|
||||||
|
size: overallBytes,
|
||||||
|
percent: curPercent >= 100 ? 99.99 : curPercent,
|
||||||
|
streamUploadInfo: {
|
||||||
|
currentFile: file.name,
|
||||||
|
fileIndex: i + 1,
|
||||||
|
totalFiles: files.length,
|
||||||
|
uploadedLines: totalUploadedLines + uploadedLines,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
updateTaskList(updatedTask);
|
||||||
|
},
|
||||||
|
1024 * 1024, // 1MB chunk size
|
||||||
|
{
|
||||||
|
resolveReqId: async ({ totalFileNum, totalSize }) => {
|
||||||
|
const { data: reqId } = await preUpload(task.key, {
|
||||||
|
totalFileNum,
|
||||||
|
totalSize,
|
||||||
|
datasetId: task.key,
|
||||||
|
hasArchive: task.hasArchive,
|
||||||
|
prefix: task.prefix,
|
||||||
|
});
|
||||||
|
console.log(`[useSliceUpload] File ${file.name} preUpload response reqId:`, reqId);
|
||||||
|
reqIds.push(reqId);
|
||||||
|
return reqId;
|
||||||
|
},
|
||||||
|
hasArchive: newTask.hasArchive,
|
||||||
|
prefix: newTask.prefix,
|
||||||
|
signal: newTask.controller.signal,
|
||||||
|
maxConcurrency: 3,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
results.push(result);
|
||||||
|
totalUploadedLines += result.uploadedCount;
|
||||||
|
totalProcessedBytes += file.size;
|
||||||
|
|
||||||
|
console.log(`[useSliceUpload] File ${file.name} processed, uploaded ${result.uploadedCount} lines`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[useSliceUpload] Stream upload completed, total lines:', totalUploadedLines);
|
||||||
|
removeTask(newTask);
|
||||||
|
|
||||||
|
message.success(`成功上传 ${totalUploadedLines} 个文件(按行分割)`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[useSliceUpload] Stream upload error:', err);
|
||||||
|
if (err.message === "Upload cancelled") {
|
||||||
|
message.info("上传已取消");
|
||||||
|
} else {
|
||||||
|
message.error("文件上传失败,请稍后重试");
|
||||||
|
}
|
||||||
|
removeTask({
|
||||||
|
...task,
|
||||||
|
isCancel: true,
|
||||||
|
...taskListRef.current.find((item) => item.key === task.key),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册流式上传事件监听
|
||||||
|
* 返回注销函数
|
||||||
|
*/
|
||||||
|
const registerStreamUploadListener = () => {
|
||||||
|
if (!enableStreamUpload) return () => {};
|
||||||
|
|
||||||
|
const streamUploadHandler = async (e: Event) => {
|
||||||
|
const customEvent = e as CustomEvent;
|
||||||
|
const { dataset, files, updateEvent, hasArchive, prefix } = customEvent.detail;
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const task: TaskItem = {
|
||||||
|
key: dataset.id,
|
||||||
|
title: `上传数据集: ${dataset.name} (按行分割)`,
|
||||||
|
percent: 0,
|
||||||
|
reqId: -1,
|
||||||
|
controller,
|
||||||
|
size: 0,
|
||||||
|
updateEvent,
|
||||||
|
hasArchive,
|
||||||
|
prefix,
|
||||||
|
};
|
||||||
|
|
||||||
|
taskListRef.current = [task, ...taskListRef.current];
|
||||||
|
setTaskList(taskListRef.current);
|
||||||
|
|
||||||
|
// 显示任务中心
|
||||||
|
if (showTaskCenter) {
|
||||||
|
window.dispatchEvent(
|
||||||
|
new CustomEvent("show:task-popover", { detail: { show: true } })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await handleStreamUpload({ task, files });
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("upload:dataset-stream", streamUploadHandler);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("upload:dataset-stream", streamUploadHandler);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
taskList,
|
taskList,
|
||||||
createTask,
|
createTask,
|
||||||
removeTask,
|
removeTask,
|
||||||
handleUpload,
|
handleUpload,
|
||||||
|
handleStreamUpload,
|
||||||
|
registerStreamUploadListener,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { useNavigate, useParams } from "react-router";
|
|||||||
import {
|
import {
|
||||||
getEditorProjectInfoUsingGet,
|
getEditorProjectInfoUsingGet,
|
||||||
getEditorTaskUsingGet,
|
getEditorTaskUsingGet,
|
||||||
|
getEditorTaskSegmentsUsingGet,
|
||||||
listEditorTasksUsingGet,
|
listEditorTasksUsingGet,
|
||||||
upsertEditorAnnotationUsingPut,
|
upsertEditorAnnotationUsingPut,
|
||||||
} from "../annotation.api";
|
} from "../annotation.api";
|
||||||
@@ -38,9 +39,6 @@ type LsfMessage = {
|
|||||||
|
|
||||||
type SegmentInfo = {
|
type SegmentInfo = {
|
||||||
idx: number;
|
idx: number;
|
||||||
text: string;
|
|
||||||
start: number;
|
|
||||||
end: number;
|
|
||||||
hasAnnotation: boolean;
|
hasAnnotation: boolean;
|
||||||
lineIndex: number;
|
lineIndex: number;
|
||||||
chunkIndex: number;
|
chunkIndex: number;
|
||||||
@@ -66,10 +64,16 @@ type EditorTaskPayload = {
|
|||||||
type EditorTaskResponse = {
|
type EditorTaskResponse = {
|
||||||
task?: EditorTaskPayload;
|
task?: EditorTaskPayload;
|
||||||
segmented?: boolean;
|
segmented?: boolean;
|
||||||
segments?: SegmentInfo[];
|
totalSegments?: number;
|
||||||
currentSegmentIndex?: number;
|
currentSegmentIndex?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type EditorTaskSegmentsResponse = {
|
||||||
|
segmented?: boolean;
|
||||||
|
segments?: SegmentInfo[];
|
||||||
|
totalSegments?: number;
|
||||||
|
};
|
||||||
|
|
||||||
type EditorTaskListResponse = {
|
type EditorTaskListResponse = {
|
||||||
content?: EditorTaskListItem[];
|
content?: EditorTaskListItem[];
|
||||||
totalElements?: number;
|
totalElements?: number;
|
||||||
@@ -288,6 +292,7 @@ export default function LabelStudioTextEditor() {
|
|||||||
const segmentStatsCacheRef = useRef<Record<string, SegmentStats>>({});
|
const segmentStatsCacheRef = useRef<Record<string, SegmentStats>>({});
|
||||||
const segmentStatsSeqRef = useRef(0);
|
const segmentStatsSeqRef = useRef(0);
|
||||||
const segmentStatsLoadingRef = useRef<Set<string>>(new Set());
|
const segmentStatsLoadingRef = useRef<Set<string>>(new Set());
|
||||||
|
const segmentSummaryFileRef = useRef<string>("");
|
||||||
|
|
||||||
const [loadingProject, setLoadingProject] = useState(true);
|
const [loadingProject, setLoadingProject] = useState(true);
|
||||||
const [loadingTasks, setLoadingTasks] = useState(false);
|
const [loadingTasks, setLoadingTasks] = useState(false);
|
||||||
@@ -358,9 +363,7 @@ export default function LabelStudioTextEditor() {
|
|||||||
if (segmentStatsCacheRef.current[fileId] || segmentStatsLoadingRef.current.has(fileId)) return;
|
if (segmentStatsCacheRef.current[fileId] || segmentStatsLoadingRef.current.has(fileId)) return;
|
||||||
segmentStatsLoadingRef.current.add(fileId);
|
segmentStatsLoadingRef.current.add(fileId);
|
||||||
try {
|
try {
|
||||||
const resp = (await getEditorTaskUsingGet(projectId, fileId, {
|
const resp = (await getEditorTaskSegmentsUsingGet(projectId, fileId)) as ApiResponse<EditorTaskSegmentsResponse>;
|
||||||
segmentIndex: 0,
|
|
||||||
})) as ApiResponse<EditorTaskResponse>;
|
|
||||||
if (segmentStatsSeqRef.current !== seq) return;
|
if (segmentStatsSeqRef.current !== seq) return;
|
||||||
const data = resp?.data;
|
const data = resp?.data;
|
||||||
if (!data?.segmented) return;
|
if (!data?.segmented) return;
|
||||||
@@ -591,20 +594,38 @@ export default function LabelStudioTextEditor() {
|
|||||||
if (seq !== initSeqRef.current) return;
|
if (seq !== initSeqRef.current) return;
|
||||||
|
|
||||||
// 更新分段状态
|
// 更新分段状态
|
||||||
const segmentIndex = data?.segmented
|
const isSegmented = !!data?.segmented;
|
||||||
|
const segmentIndex = isSegmented
|
||||||
? resolveSegmentIndex(data.currentSegmentIndex) ?? 0
|
? resolveSegmentIndex(data.currentSegmentIndex) ?? 0
|
||||||
: undefined;
|
: undefined;
|
||||||
if (data?.segmented) {
|
if (isSegmented) {
|
||||||
const stats = buildSegmentStats(data.segments);
|
let nextSegments: SegmentInfo[] = [];
|
||||||
|
if (segmentSummaryFileRef.current === fileId && segments.length > 0) {
|
||||||
|
nextSegments = segments;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const segmentResp = (await getEditorTaskSegmentsUsingGet(projectId, fileId)) as ApiResponse<EditorTaskSegmentsResponse>;
|
||||||
|
if (seq !== initSeqRef.current) return;
|
||||||
|
const segmentData = segmentResp?.data;
|
||||||
|
if (segmentData?.segmented) {
|
||||||
|
nextSegments = Array.isArray(segmentData.segments) ? segmentData.segments : [];
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const stats = buildSegmentStats(nextSegments);
|
||||||
setSegmented(true);
|
setSegmented(true);
|
||||||
setSegments(data.segments || []);
|
setSegments(nextSegments);
|
||||||
setCurrentSegmentIndex(segmentIndex ?? 0);
|
setCurrentSegmentIndex(segmentIndex ?? 0);
|
||||||
updateSegmentStatsCache(fileId, stats);
|
updateSegmentStatsCache(fileId, stats);
|
||||||
|
segmentSummaryFileRef.current = fileId;
|
||||||
} else {
|
} else {
|
||||||
setSegmented(false);
|
setSegmented(false);
|
||||||
setSegments([]);
|
setSegments([]);
|
||||||
setCurrentSegmentIndex(0);
|
setCurrentSegmentIndex(0);
|
||||||
updateSegmentStatsCache(fileId, null);
|
updateSegmentStatsCache(fileId, null);
|
||||||
|
segmentSummaryFileRef.current = fileId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const taskData = {
|
const taskData = {
|
||||||
@@ -664,7 +685,7 @@ export default function LabelStudioTextEditor() {
|
|||||||
} finally {
|
} finally {
|
||||||
if (seq === initSeqRef.current) setLoadingTaskDetail(false);
|
if (seq === initSeqRef.current) setLoadingTaskDetail(false);
|
||||||
}
|
}
|
||||||
}, [iframeReady, message, postToIframe, project, projectId, updateSegmentStatsCache]);
|
}, [iframeReady, message, postToIframe, project, projectId, segments, updateSegmentStatsCache]);
|
||||||
|
|
||||||
const advanceAfterSave = useCallback(async (fileId: string, segmentIndex?: number) => {
|
const advanceAfterSave = useCallback(async (fileId: string, segmentIndex?: number) => {
|
||||||
if (!fileId) return;
|
if (!fileId) return;
|
||||||
@@ -979,6 +1000,7 @@ export default function LabelStudioTextEditor() {
|
|||||||
setSegmented(false);
|
setSegmented(false);
|
||||||
setSegments([]);
|
setSegments([]);
|
||||||
setCurrentSegmentIndex(0);
|
setCurrentSegmentIndex(0);
|
||||||
|
segmentSummaryFileRef.current = "";
|
||||||
savedSnapshotsRef.current = {};
|
savedSnapshotsRef.current = {};
|
||||||
segmentStatsSeqRef.current += 1;
|
segmentStatsSeqRef.current += 1;
|
||||||
segmentStatsCacheRef.current = {};
|
segmentStatsCacheRef.current = {};
|
||||||
|
|||||||
@@ -3,16 +3,19 @@ import { get, post, put, del, download } from "@/utils/request";
|
|||||||
// 导出格式类型
|
// 导出格式类型
|
||||||
export type ExportFormat = "json" | "jsonl" | "csv" | "coco" | "yolo";
|
export type ExportFormat = "json" | "jsonl" | "csv" | "coco" | "yolo";
|
||||||
|
|
||||||
|
type RequestParams = Record<string, unknown>;
|
||||||
|
type RequestPayload = Record<string, unknown>;
|
||||||
|
|
||||||
// 标注任务管理相关接口
|
// 标注任务管理相关接口
|
||||||
export function queryAnnotationTasksUsingGet(params?: any) {
|
export function queryAnnotationTasksUsingGet(params?: RequestParams) {
|
||||||
return get("/api/annotation/project", params);
|
return get("/api/annotation/project", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createAnnotationTaskUsingPost(data: any) {
|
export function createAnnotationTaskUsingPost(data: RequestPayload) {
|
||||||
return post("/api/annotation/project", data);
|
return post("/api/annotation/project", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function syncAnnotationTaskUsingPost(data: any) {
|
export function syncAnnotationTaskUsingPost(data: RequestPayload) {
|
||||||
return post(`/api/annotation/task/sync`, data);
|
return post(`/api/annotation/task/sync`, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +28,7 @@ export function getAnnotationTaskByIdUsingGet(taskId: string) {
|
|||||||
return get(`/api/annotation/project/${taskId}`);
|
return get(`/api/annotation/project/${taskId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateAnnotationTaskByIdUsingPut(taskId: string, data: any) {
|
export function updateAnnotationTaskByIdUsingPut(taskId: string, data: RequestPayload) {
|
||||||
return put(`/api/annotation/project/${taskId}`, data);
|
return put(`/api/annotation/project/${taskId}`, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,17 +38,17 @@ export function getTagConfigUsingGet() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 标注模板管理
|
// 标注模板管理
|
||||||
export function queryAnnotationTemplatesUsingGet(params?: any) {
|
export function queryAnnotationTemplatesUsingGet(params?: RequestParams) {
|
||||||
return get("/api/annotation/template", params);
|
return get("/api/annotation/template", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createAnnotationTemplateUsingPost(data: any) {
|
export function createAnnotationTemplateUsingPost(data: RequestPayload) {
|
||||||
return post("/api/annotation/template", data);
|
return post("/api/annotation/template", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateAnnotationTemplateByIdUsingPut(
|
export function updateAnnotationTemplateByIdUsingPut(
|
||||||
templateId: string | number,
|
templateId: string | number,
|
||||||
data: any
|
data: RequestPayload
|
||||||
) {
|
) {
|
||||||
return put(`/api/annotation/template/${templateId}`, data);
|
return put(`/api/annotation/template/${templateId}`, data);
|
||||||
}
|
}
|
||||||
@@ -65,7 +68,7 @@ export function getEditorProjectInfoUsingGet(projectId: string) {
|
|||||||
return get(`/api/annotation/editor/projects/${projectId}`);
|
return get(`/api/annotation/editor/projects/${projectId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listEditorTasksUsingGet(projectId: string, params?: any) {
|
export function listEditorTasksUsingGet(projectId: string, params?: RequestParams) {
|
||||||
return get(`/api/annotation/editor/projects/${projectId}/tasks`, params);
|
return get(`/api/annotation/editor/projects/${projectId}/tasks`, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,11 +80,15 @@ export function getEditorTaskUsingGet(
|
|||||||
return get(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}`, params);
|
return get(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}`, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getEditorTaskSegmentsUsingGet(projectId: string, fileId: string) {
|
||||||
|
return get(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}/segments`);
|
||||||
|
}
|
||||||
|
|
||||||
export function upsertEditorAnnotationUsingPut(
|
export function upsertEditorAnnotationUsingPut(
|
||||||
projectId: string,
|
projectId: string,
|
||||||
fileId: string,
|
fileId: string,
|
||||||
data: {
|
data: {
|
||||||
annotation: any;
|
annotation: Record<string, unknown>;
|
||||||
expectedUpdatedAt?: string;
|
expectedUpdatedAt?: string;
|
||||||
segmentIndex?: number;
|
segmentIndex?: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { Dataset, DatasetType, DataSource } from "../../dataset.model";
|
|||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { queryTasksUsingGet } from "@/pages/DataCollection/collection.apis";
|
import { queryTasksUsingGet } from "@/pages/DataCollection/collection.apis";
|
||||||
import { updateDatasetByIdUsingPut } from "../../dataset.api";
|
import { updateDatasetByIdUsingPut } from "../../dataset.api";
|
||||||
import { sliceFile } from "@/utils/file.util";
|
import { sliceFile, shouldStreamUpload } from "@/utils/file.util";
|
||||||
import Dragger from "antd/es/upload/Dragger";
|
import Dragger from "antd/es/upload/Dragger";
|
||||||
|
|
||||||
const TEXT_FILE_MIME_PREFIX = "text/";
|
const TEXT_FILE_MIME_PREFIX = "text/";
|
||||||
@@ -90,14 +90,16 @@ async function splitFileByLines(file: UploadFile): Promise<UploadFile[]> {
|
|||||||
const lines = text.split(/\r?\n/).filter((line: string) => line.trim() !== "");
|
const lines = text.split(/\r?\n/).filter((line: string) => line.trim() !== "");
|
||||||
if (lines.length === 0) return [];
|
if (lines.length === 0) return [];
|
||||||
|
|
||||||
// 生成文件名:原文件名_序号.扩展名
|
// 生成文件名:原文件名_序号(不保留后缀)
|
||||||
const nameParts = file.name.split(".");
|
const nameParts = file.name.split(".");
|
||||||
const ext = nameParts.length > 1 ? "." + nameParts.pop() : "";
|
if (nameParts.length > 1) {
|
||||||
|
nameParts.pop();
|
||||||
|
}
|
||||||
const baseName = nameParts.join(".");
|
const baseName = nameParts.join(".");
|
||||||
const padLength = String(lines.length).length;
|
const padLength = String(lines.length).length;
|
||||||
|
|
||||||
return lines.map((line: string, index: number) => {
|
return lines.map((line: string, index: number) => {
|
||||||
const newFileName = `${baseName}_${String(index + 1).padStart(padLength, "0")}${ext}`;
|
const newFileName = `${baseName}_${String(index + 1).padStart(padLength, "0")}`;
|
||||||
const blob = new Blob([line], { type: "text/plain" });
|
const blob = new Blob([line], { type: "text/plain" });
|
||||||
const newFile = new File([blob], newFileName, { type: "text/plain" });
|
const newFile = new File([blob], newFileName, { type: "text/plain" });
|
||||||
return {
|
return {
|
||||||
@@ -164,17 +166,75 @@ export default function ImportConfiguration({
|
|||||||
// 本地上传文件相关逻辑
|
// 本地上传文件相关逻辑
|
||||||
|
|
||||||
const handleUpload = async (dataset: Dataset) => {
|
const handleUpload = async (dataset: Dataset) => {
|
||||||
let filesToUpload =
|
const filesToUpload =
|
||||||
(form.getFieldValue("files") as UploadFile[] | undefined) || [];
|
(form.getFieldValue("files") as UploadFile[] | undefined) || [];
|
||||||
|
|
||||||
// 如果启用分行分割,处理文件
|
// 如果启用分行分割,对大文件使用流式处理
|
||||||
if (importConfig.splitByLine && !hasNonTextFile) {
|
if (importConfig.splitByLine && !hasNonTextFile) {
|
||||||
const splitResults = await Promise.all(
|
// 检查是否有大文件需要流式分割上传
|
||||||
filesToUpload.map((file) => splitFileByLines(file))
|
const filesForStreamUpload: File[] = [];
|
||||||
);
|
const filesForNormalUpload: UploadFile[] = [];
|
||||||
filesToUpload = splitResults.flat();
|
|
||||||
|
for (const file of filesToUpload) {
|
||||||
|
const originFile = file.originFileObj ?? file;
|
||||||
|
if (originFile instanceof File && shouldStreamUpload(originFile)) {
|
||||||
|
filesForStreamUpload.push(originFile);
|
||||||
|
} else {
|
||||||
|
filesForNormalUpload.push(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 大文件使用流式分割上传
|
||||||
|
if (filesForStreamUpload.length > 0) {
|
||||||
|
window.dispatchEvent(
|
||||||
|
new CustomEvent("upload:dataset-stream", {
|
||||||
|
detail: {
|
||||||
|
dataset,
|
||||||
|
files: filesForStreamUpload,
|
||||||
|
updateEvent,
|
||||||
|
hasArchive: importConfig.hasArchive,
|
||||||
|
prefix: currentPrefix,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 小文件使用传统分割方式
|
||||||
|
if (filesForNormalUpload.length > 0) {
|
||||||
|
const splitResults = await Promise.all(
|
||||||
|
filesForNormalUpload.map((file) => splitFileByLines(file))
|
||||||
|
);
|
||||||
|
const smallFilesToUpload = splitResults.flat();
|
||||||
|
|
||||||
|
// 计算分片列表
|
||||||
|
const sliceList = smallFilesToUpload.map((file) => {
|
||||||
|
const originFile = (file.originFileObj ?? file) as Blob;
|
||||||
|
const slices = sliceFile(originFile);
|
||||||
|
return {
|
||||||
|
originFile: originFile,
|
||||||
|
slices,
|
||||||
|
name: file.name,
|
||||||
|
size: originFile.size || 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("[ImportConfiguration] Uploading small files with currentPrefix:", currentPrefix);
|
||||||
|
window.dispatchEvent(
|
||||||
|
new CustomEvent("upload:dataset", {
|
||||||
|
detail: {
|
||||||
|
dataset,
|
||||||
|
files: sliceList,
|
||||||
|
updateEvent,
|
||||||
|
hasArchive: importConfig.hasArchive,
|
||||||
|
prefix: currentPrefix,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未启用分行分割,使用普通上传
|
||||||
// 计算分片列表
|
// 计算分片列表
|
||||||
const sliceList = filesToUpload.map((file) => {
|
const sliceList = filesToUpload.map((file) => {
|
||||||
const originFile = (file.originFileObj ?? file) as Blob;
|
const originFile = (file.originFileObj ?? file) as Blob;
|
||||||
|
|||||||
@@ -102,6 +102,13 @@ export interface DatasetTask {
|
|||||||
executionHistory?: { time: string; status: string }[];
|
executionHistory?: { time: string; status: string }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StreamUploadInfo {
|
||||||
|
currentFile: string;
|
||||||
|
fileIndex: number;
|
||||||
|
totalFiles: number;
|
||||||
|
uploadedLines: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface TaskItem {
|
export interface TaskItem {
|
||||||
key: string;
|
key: string;
|
||||||
title: string;
|
title: string;
|
||||||
@@ -113,4 +120,6 @@ export interface TaskItem {
|
|||||||
updateEvent?: string;
|
updateEvent?: string;
|
||||||
size?: number;
|
size?: number;
|
||||||
hasArchive?: boolean;
|
hasArchive?: boolean;
|
||||||
|
prefix?: string;
|
||||||
|
streamUploadInfo?: StreamUploadInfo;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,25 +3,28 @@ import {
|
|||||||
preUploadUsingPost,
|
preUploadUsingPost,
|
||||||
uploadFileChunkUsingPost,
|
uploadFileChunkUsingPost,
|
||||||
} from "@/pages/DataManagement/dataset.api";
|
} from "@/pages/DataManagement/dataset.api";
|
||||||
import { Button, Empty, Progress } from "antd";
|
import { Button, Empty, Progress, Tag } from "antd";
|
||||||
import { DeleteOutlined } from "@ant-design/icons";
|
import { DeleteOutlined, FileTextOutlined } from "@ant-design/icons";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useFileSliceUpload } from "@/hooks/useSliceUpload";
|
import { useFileSliceUpload } from "@/hooks/useSliceUpload";
|
||||||
|
|
||||||
export default function TaskUpload() {
|
export default function TaskUpload() {
|
||||||
const { createTask, taskList, removeTask, handleUpload } = useFileSliceUpload(
|
const { createTask, taskList, removeTask, handleUpload, registerStreamUploadListener } = useFileSliceUpload(
|
||||||
{
|
{
|
||||||
preUpload: preUploadUsingPost,
|
preUpload: preUploadUsingPost,
|
||||||
uploadChunk: uploadFileChunkUsingPost,
|
uploadChunk: uploadFileChunkUsingPost,
|
||||||
cancelUpload: cancelUploadUsingPut,
|
cancelUpload: cancelUploadUsingPut,
|
||||||
}
|
},
|
||||||
|
true, // showTaskCenter
|
||||||
|
true // enableStreamUpload
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const uploadHandler = (e: any) => {
|
const uploadHandler = (e: Event) => {
|
||||||
console.log('[TaskUpload] Received upload event detail:', e.detail);
|
const customEvent = e as CustomEvent;
|
||||||
const { files } = e.detail;
|
console.log('[TaskUpload] Received upload event detail:', customEvent.detail);
|
||||||
const task = createTask(e.detail);
|
const { files } = customEvent.detail;
|
||||||
|
const task = createTask(customEvent.detail);
|
||||||
console.log('[TaskUpload] Created task with prefix:', task.prefix);
|
console.log('[TaskUpload] Created task with prefix:', task.prefix);
|
||||||
handleUpload({ task, files });
|
handleUpload({ task, files });
|
||||||
};
|
};
|
||||||
@@ -29,7 +32,13 @@ export default function TaskUpload() {
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("upload:dataset", uploadHandler);
|
window.removeEventListener("upload:dataset", uploadHandler);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [createTask, handleUpload]);
|
||||||
|
|
||||||
|
// 注册流式上传监听器
|
||||||
|
useEffect(() => {
|
||||||
|
const unregister = registerStreamUploadListener();
|
||||||
|
return unregister;
|
||||||
|
}, [registerStreamUploadListener]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -55,7 +64,22 @@ export default function TaskUpload() {
|
|||||||
></Button>
|
></Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Progress size="small" percent={task.percent} />
|
<Progress size="small" percent={Number(task.percent)} />
|
||||||
|
{task.streamUploadInfo && (
|
||||||
|
<div className="flex items-center gap-2 text-xs text-gray-500 mt-1">
|
||||||
|
<Tag icon={<FileTextOutlined />} size="small">
|
||||||
|
按行分割
|
||||||
|
</Tag>
|
||||||
|
<span>
|
||||||
|
已上传: {task.streamUploadInfo.uploadedLines} 行
|
||||||
|
</span>
|
||||||
|
{task.streamUploadInfo.totalFiles > 1 && (
|
||||||
|
<span>
|
||||||
|
({task.streamUploadInfo.fileIndex}/{task.streamUploadInfo.totalFiles} 文件)
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
{taskList.length === 0 && (
|
{taskList.length === 0 && (
|
||||||
|
|||||||
@@ -1,79 +1,657 @@
|
|||||||
import { UploadFile } from "antd";
|
import { UploadFile } from "antd";
|
||||||
import jsSHA from "jssha";
|
import jsSHA from "jssha";
|
||||||
|
|
||||||
const CHUNK_SIZE = 1024 * 1024 * 60;
|
// 默认分片大小:5MB(适合大多数网络环境)
|
||||||
|
export const DEFAULT_CHUNK_SIZE = 1024 * 1024 * 5;
|
||||||
|
// 大文件阈值:10MB
|
||||||
|
export const LARGE_FILE_THRESHOLD = 1024 * 1024 * 10;
|
||||||
|
// 最大并发上传数
|
||||||
|
export const MAX_CONCURRENT_UPLOADS = 3;
|
||||||
|
// 文本文件读取块大小:20MB(用于计算 SHA256)
|
||||||
|
const BUFFER_CHUNK_SIZE = 1024 * 1024 * 20;
|
||||||
|
|
||||||
export function sliceFile(file, chunkSize = CHUNK_SIZE): Blob[] {
|
/**
|
||||||
|
* 将文件分割为多个分片
|
||||||
|
* @param file 文件对象
|
||||||
|
* @param chunkSize 分片大小(字节),默认 5MB
|
||||||
|
* @returns 分片数组(Blob 列表)
|
||||||
|
*/
|
||||||
|
export function sliceFile(file: Blob, chunkSize = DEFAULT_CHUNK_SIZE): Blob[] {
|
||||||
const totalSize = file.size;
|
const totalSize = file.size;
|
||||||
|
const chunks: Blob[] = [];
|
||||||
|
|
||||||
|
// 小文件不需要分片
|
||||||
|
if (totalSize <= chunkSize) {
|
||||||
|
return [file];
|
||||||
|
}
|
||||||
|
|
||||||
let start = 0;
|
let start = 0;
|
||||||
let end = start + chunkSize;
|
|
||||||
const chunks = [];
|
|
||||||
while (start < totalSize) {
|
while (start < totalSize) {
|
||||||
|
const end = Math.min(start + chunkSize, totalSize);
|
||||||
const blob = file.slice(start, end);
|
const blob = file.slice(start, end);
|
||||||
chunks.push(blob);
|
chunks.push(blob);
|
||||||
|
|
||||||
start = end;
|
start = end;
|
||||||
end = start + chunkSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateSHA256(file: Blob): Promise<string> {
|
/**
|
||||||
let count = 0;
|
* 计算文件的 SHA256 哈希值
|
||||||
const hash = new jsSHA("SHA-256", "ARRAYBUFFER", { encoding: "UTF8" });
|
* @param file 文件 Blob
|
||||||
|
* @param onProgress 进度回调(可选)
|
||||||
|
* @returns SHA256 哈希字符串
|
||||||
|
*/
|
||||||
|
export function calculateSHA256(
|
||||||
|
file: Blob,
|
||||||
|
onProgress?: (percent: number) => void
|
||||||
|
): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const hash = new jsSHA("SHA-256", "ARRAYBUFFER", { encoding: "UTF8" });
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
|
let processedSize = 0;
|
||||||
|
|
||||||
function readChunk(start: number, end: number) {
|
function readChunk(start: number, end: number) {
|
||||||
const slice = file.slice(start, end);
|
const slice = file.slice(start, end);
|
||||||
reader.readAsArrayBuffer(slice);
|
reader.readAsArrayBuffer(slice);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bufferChunkSize = 1024 * 1024 * 20;
|
|
||||||
|
|
||||||
function processChunk(offset: number) {
|
function processChunk(offset: number) {
|
||||||
const start = offset;
|
const start = offset;
|
||||||
const end = Math.min(start + bufferChunkSize, file.size);
|
const end = Math.min(start + BUFFER_CHUNK_SIZE, file.size);
|
||||||
count = end;
|
|
||||||
|
|
||||||
readChunk(start, end);
|
readChunk(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.onloadend = function () {
|
reader.onloadend = function (e) {
|
||||||
const arraybuffer = reader.result;
|
const arraybuffer = reader.result as ArrayBuffer;
|
||||||
|
if (!arraybuffer) {
|
||||||
|
reject(new Error("Failed to read file"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
hash.update(arraybuffer);
|
hash.update(arraybuffer);
|
||||||
if (count < file.size) {
|
processedSize += (e.target as FileReader).result?.byteLength || 0;
|
||||||
processChunk(count);
|
|
||||||
|
if (onProgress) {
|
||||||
|
const percent = Math.min(100, Math.round((processedSize / file.size) * 100));
|
||||||
|
onProgress(percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (processedSize < file.size) {
|
||||||
|
processChunk(processedSize);
|
||||||
} else {
|
} else {
|
||||||
resolve(hash.getHash("HEX", { outputLen: 256 }));
|
resolve(hash.getHash("HEX", { outputLen: 256 }));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
reader.onerror = () => reject(new Error("File reading failed"));
|
||||||
processChunk(0);
|
processChunk(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量计算多个文件的 SHA256
|
||||||
|
* @param files 文件列表
|
||||||
|
* @param onFileProgress 单个文件进度回调(可选)
|
||||||
|
* @returns 哈希值数组
|
||||||
|
*/
|
||||||
|
export async function calculateSHA256Batch(
|
||||||
|
files: Blob[],
|
||||||
|
onFileProgress?: (index: number, percent: number) => void
|
||||||
|
): Promise<string[]> {
|
||||||
|
const results: string[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < files.length; i++) {
|
||||||
|
const hash = await calculateSHA256(files[i], (percent) => {
|
||||||
|
onFileProgress?.(i, percent);
|
||||||
|
});
|
||||||
|
results.push(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查文件是否存在(未被修改或删除)
|
||||||
|
* @param fileList 文件列表
|
||||||
|
* @returns 返回第一个不存在的文件,或 null(如果都存在)
|
||||||
|
*/
|
||||||
export function checkIsFilesExist(
|
export function checkIsFilesExist(
|
||||||
fileList: UploadFile[]
|
fileList: Array<{ originFile?: Blob }>
|
||||||
): Promise<UploadFile | null> {
|
): Promise<{ originFile?: Blob } | null> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const loadEndFn = (file: UploadFile, reachEnd: boolean, e) => {
|
if (!fileList.length) {
|
||||||
const fileNotExist = !e.target.result;
|
resolve(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let checkedCount = 0;
|
||||||
|
const totalCount = fileList.length;
|
||||||
|
|
||||||
|
const loadEndFn = (file: { originFile?: Blob }, e: ProgressEvent<FileReader>) => {
|
||||||
|
checkedCount++;
|
||||||
|
const fileNotExist = !e.target?.result;
|
||||||
if (fileNotExist) {
|
if (fileNotExist) {
|
||||||
resolve(file);
|
resolve(file);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (reachEnd) {
|
if (checkedCount >= totalCount) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (let i = 0; i < fileList.length; i++) {
|
for (const file of fileList) {
|
||||||
const { originFile: file } = fileList[i];
|
|
||||||
const fileReader = new FileReader();
|
const fileReader = new FileReader();
|
||||||
fileReader.readAsArrayBuffer(file);
|
const actualFile = file.originFile;
|
||||||
fileReader.onloadend = (e) =>
|
|
||||||
loadEndFn(fileList[i], i === fileList.length - 1, e);
|
if (!actualFile) {
|
||||||
|
checkedCount++;
|
||||||
|
if (checkedCount >= totalCount) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileReader.readAsArrayBuffer(actualFile.slice(0, 1));
|
||||||
|
fileReader.onloadend = (e) => loadEndFn(file, e);
|
||||||
|
fileReader.onerror = () => {
|
||||||
|
checkedCount++;
|
||||||
|
resolve(file);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否为大文件
|
||||||
|
* @param size 文件大小(字节)
|
||||||
|
* @param threshold 阈值(字节),默认 10MB
|
||||||
|
*/
|
||||||
|
export function isLargeFile(size: number, threshold = LARGE_FILE_THRESHOLD): boolean {
|
||||||
|
return size > threshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化文件大小为人类可读格式
|
||||||
|
* @param bytes 字节数
|
||||||
|
* @param decimals 小数位数
|
||||||
|
*/
|
||||||
|
export function formatFileSize(bytes: number, decimals = 2): string {
|
||||||
|
if (bytes === 0) return "0 B";
|
||||||
|
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB"];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${sizes[i]}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 并发执行异步任务
|
||||||
|
* @param tasks 任务函数数组
|
||||||
|
* @param maxConcurrency 最大并发数
|
||||||
|
* @param onTaskComplete 单个任务完成回调(可选)
|
||||||
|
*/
|
||||||
|
export async function runConcurrentTasks<T>(
|
||||||
|
tasks: (() => Promise<T>)[],
|
||||||
|
maxConcurrency: number,
|
||||||
|
onTaskComplete?: (index: number, result: T) => void
|
||||||
|
): Promise<T[]> {
|
||||||
|
const results: T[] = new Array(tasks.length);
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
async function runNext(): Promise<void> {
|
||||||
|
const currentIndex = index++;
|
||||||
|
if (currentIndex >= tasks.length) return;
|
||||||
|
|
||||||
|
const result = await tasks[currentIndex]();
|
||||||
|
results[currentIndex] = result;
|
||||||
|
onTaskComplete?.(currentIndex, result);
|
||||||
|
|
||||||
|
await runNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
const workers = Array(Math.min(maxConcurrency, tasks.length))
|
||||||
|
.fill(null)
|
||||||
|
.map(() => runNext());
|
||||||
|
|
||||||
|
await Promise.all(workers);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行分割文本文件内容
|
||||||
|
* @param text 文本内容
|
||||||
|
* @param skipEmptyLines 是否跳过空行,默认 true
|
||||||
|
* @returns 行数组
|
||||||
|
*/
|
||||||
|
export function splitTextByLines(text: string, skipEmptyLines = true): string[] {
|
||||||
|
const lines = text.split(/\r?\n/);
|
||||||
|
if (skipEmptyLines) {
|
||||||
|
return lines.filter((line) => line.trim() !== "");
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建分片信息对象
|
||||||
|
* @param file 原始文件
|
||||||
|
* @param chunkSize 分片大小
|
||||||
|
*/
|
||||||
|
export function createFileSliceInfo(
|
||||||
|
file: File | Blob,
|
||||||
|
chunkSize = DEFAULT_CHUNK_SIZE
|
||||||
|
): {
|
||||||
|
originFile: Blob;
|
||||||
|
slices: Blob[];
|
||||||
|
name: string;
|
||||||
|
size: number;
|
||||||
|
totalChunks: number;
|
||||||
|
} {
|
||||||
|
const slices = sliceFile(file, chunkSize);
|
||||||
|
return {
|
||||||
|
originFile: file,
|
||||||
|
slices,
|
||||||
|
name: (file as File).name || "unnamed",
|
||||||
|
size: file.size,
|
||||||
|
totalChunks: slices.length,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持的文本文件 MIME 类型前缀
|
||||||
|
*/
|
||||||
|
export const TEXT_FILE_MIME_PREFIX = "text/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持的文本文件 MIME 类型集合
|
||||||
|
*/
|
||||||
|
export const TEXT_FILE_MIME_TYPES = new Set([
|
||||||
|
"application/json",
|
||||||
|
"application/xml",
|
||||||
|
"application/csv",
|
||||||
|
"application/ndjson",
|
||||||
|
"application/x-ndjson",
|
||||||
|
"application/x-yaml",
|
||||||
|
"application/yaml",
|
||||||
|
"application/javascript",
|
||||||
|
"application/x-javascript",
|
||||||
|
"application/sql",
|
||||||
|
"application/rtf",
|
||||||
|
"application/xhtml+xml",
|
||||||
|
"application/svg+xml",
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持的文本文件扩展名集合
|
||||||
|
*/
|
||||||
|
export const TEXT_FILE_EXTENSIONS = new Set([
|
||||||
|
".txt",
|
||||||
|
".md",
|
||||||
|
".markdown",
|
||||||
|
".csv",
|
||||||
|
".tsv",
|
||||||
|
".json",
|
||||||
|
".jsonl",
|
||||||
|
".ndjson",
|
||||||
|
".log",
|
||||||
|
".xml",
|
||||||
|
".yaml",
|
||||||
|
".yml",
|
||||||
|
".sql",
|
||||||
|
".js",
|
||||||
|
".ts",
|
||||||
|
".jsx",
|
||||||
|
".tsx",
|
||||||
|
".html",
|
||||||
|
".htm",
|
||||||
|
".css",
|
||||||
|
".scss",
|
||||||
|
".less",
|
||||||
|
".py",
|
||||||
|
".java",
|
||||||
|
".c",
|
||||||
|
".cpp",
|
||||||
|
".h",
|
||||||
|
".hpp",
|
||||||
|
".go",
|
||||||
|
".rs",
|
||||||
|
".rb",
|
||||||
|
".php",
|
||||||
|
".sh",
|
||||||
|
".bash",
|
||||||
|
".zsh",
|
||||||
|
".ps1",
|
||||||
|
".bat",
|
||||||
|
".cmd",
|
||||||
|
".svg",
|
||||||
|
".rtf",
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否为文本文件(支持 UploadFile 类型)
|
||||||
|
* @param file UploadFile 对象
|
||||||
|
*/
|
||||||
|
export function isTextUploadFile(file: UploadFile): boolean {
|
||||||
|
const mimeType = (file.type || "").toLowerCase();
|
||||||
|
if (mimeType) {
|
||||||
|
if (mimeType.startsWith(TEXT_FILE_MIME_PREFIX)) return true;
|
||||||
|
if (TEXT_FILE_MIME_TYPES.has(mimeType)) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileName = file.name || "";
|
||||||
|
const dotIndex = fileName.lastIndexOf(".");
|
||||||
|
if (dotIndex < 0) return false;
|
||||||
|
const ext = fileName.slice(dotIndex).toLowerCase();
|
||||||
|
return TEXT_FILE_EXTENSIONS.has(ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件名是否为文本文件
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
|
export function isTextFileByName(fileName: string): boolean {
|
||||||
|
const lowerName = fileName.toLowerCase();
|
||||||
|
|
||||||
|
// 先检查 MIME 类型(如果有)
|
||||||
|
// 这里简化处理,主要通过扩展名判断
|
||||||
|
|
||||||
|
const dotIndex = lowerName.lastIndexOf(".");
|
||||||
|
if (dotIndex < 0) return false;
|
||||||
|
const ext = lowerName.slice(dotIndex);
|
||||||
|
return TEXT_FILE_EXTENSIONS.has(ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件扩展名
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
|
export function getFileExtension(fileName: string): string {
|
||||||
|
const dotIndex = fileName.lastIndexOf(".");
|
||||||
|
if (dotIndex < 0) return "";
|
||||||
|
return fileName.slice(dotIndex).toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全地读取文件为文本
|
||||||
|
* @param file 文件对象
|
||||||
|
* @param encoding 编码,默认 UTF-8
|
||||||
|
*/
|
||||||
|
export function readFileAsText(
|
||||||
|
file: File | Blob,
|
||||||
|
encoding = "UTF-8"
|
||||||
|
): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = (e) => resolve(e.target?.result as string);
|
||||||
|
reader.onerror = () => reject(new Error("Failed to read file"));
|
||||||
|
reader.readAsText(file, encoding);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流式分割文件并逐行上传
|
||||||
|
* 使用 Blob.slice 逐块读取,避免一次性加载大文件到内存
|
||||||
|
* @param file 文件对象
|
||||||
|
* @param datasetId 数据集ID
|
||||||
|
* @param uploadFn 上传函数,接收 FormData 和配置,返回 Promise
|
||||||
|
* @param onProgress 进度回调 (currentBytes, totalBytes, uploadedLines)
|
||||||
|
* @param chunkSize 每次读取的块大小,默认 1MB
|
||||||
|
* @param options 其他选项
|
||||||
|
* @returns 上传结果统计
|
||||||
|
*/
|
||||||
|
export interface StreamUploadOptions {
|
||||||
|
reqId?: number;
|
||||||
|
resolveReqId?: (params: { totalFileNum: number; totalSize: number }) => Promise<number>;
|
||||||
|
onReqIdResolved?: (reqId: number) => void;
|
||||||
|
fileNamePrefix?: string;
|
||||||
|
hasArchive?: boolean;
|
||||||
|
prefix?: string;
|
||||||
|
signal?: AbortSignal;
|
||||||
|
maxConcurrency?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StreamUploadResult {
|
||||||
|
uploadedCount: number;
|
||||||
|
totalBytes: number;
|
||||||
|
skippedEmptyCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function processFileLines(
|
||||||
|
file: File,
|
||||||
|
chunkSize: number,
|
||||||
|
signal: AbortSignal | undefined,
|
||||||
|
onLine?: (line: string, index: number) => Promise<void> | void,
|
||||||
|
onProgress?: (currentBytes: number, totalBytes: number, processedLines: number) => void
|
||||||
|
): Promise<{ lineCount: number; skippedEmptyCount: number }> {
|
||||||
|
const fileSize = file.size;
|
||||||
|
let offset = 0;
|
||||||
|
let buffer = "";
|
||||||
|
let skippedEmptyCount = 0;
|
||||||
|
let lineIndex = 0;
|
||||||
|
|
||||||
|
while (offset < fileSize) {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
const end = Math.min(offset + chunkSize, fileSize);
|
||||||
|
const chunk = file.slice(offset, end);
|
||||||
|
const text = await readFileAsText(chunk);
|
||||||
|
const combined = buffer + text;
|
||||||
|
const lines = combined.split(/\r?\n/);
|
||||||
|
buffer = lines.pop() || "";
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
if (!line.trim()) {
|
||||||
|
skippedEmptyCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const currentIndex = lineIndex;
|
||||||
|
lineIndex += 1;
|
||||||
|
if (onLine) {
|
||||||
|
await onLine(line, currentIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offset = end;
|
||||||
|
onProgress?.(offset, fileSize, lineIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buffer.trim()) {
|
||||||
|
const currentIndex = lineIndex;
|
||||||
|
lineIndex += 1;
|
||||||
|
if (onLine) {
|
||||||
|
await onLine(buffer, currentIndex);
|
||||||
|
}
|
||||||
|
} else if (buffer.length > 0) {
|
||||||
|
skippedEmptyCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { lineCount: lineIndex, skippedEmptyCount };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function streamSplitAndUpload(
|
||||||
|
file: File,
|
||||||
|
uploadFn: (formData: FormData, config?: { onUploadProgress?: (e: { loaded: number; total: number }) => void }) => Promise<unknown>,
|
||||||
|
onProgress?: (currentBytes: number, totalBytes: number, uploadedLines: number) => void,
|
||||||
|
chunkSize: number = 1024 * 1024, // 1MB
|
||||||
|
options: StreamUploadOptions
|
||||||
|
): Promise<StreamUploadResult> {
|
||||||
|
const {
|
||||||
|
reqId: initialReqId,
|
||||||
|
resolveReqId,
|
||||||
|
onReqIdResolved,
|
||||||
|
fileNamePrefix,
|
||||||
|
prefix,
|
||||||
|
signal,
|
||||||
|
maxConcurrency = 3,
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
const fileSize = file.size;
|
||||||
|
let uploadedCount = 0;
|
||||||
|
let skippedEmptyCount = 0;
|
||||||
|
|
||||||
|
// 获取文件名基础部分和扩展名
|
||||||
|
const originalFileName = fileNamePrefix || file.name;
|
||||||
|
const lastDotIndex = originalFileName.lastIndexOf(".");
|
||||||
|
const baseName = lastDotIndex > 0 ? originalFileName.slice(0, lastDotIndex) : originalFileName;
|
||||||
|
const fileExtension = lastDotIndex > 0 ? originalFileName.slice(lastDotIndex) : "";
|
||||||
|
|
||||||
|
let resolvedReqId = initialReqId;
|
||||||
|
if (!resolvedReqId) {
|
||||||
|
const scanResult = await processFileLines(file, chunkSize, signal);
|
||||||
|
const totalFileNum = scanResult.lineCount;
|
||||||
|
skippedEmptyCount = scanResult.skippedEmptyCount;
|
||||||
|
if (totalFileNum === 0) {
|
||||||
|
return {
|
||||||
|
uploadedCount: 0,
|
||||||
|
totalBytes: fileSize,
|
||||||
|
skippedEmptyCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
if (!resolveReqId) {
|
||||||
|
throw new Error("Missing pre-upload request id");
|
||||||
|
}
|
||||||
|
resolvedReqId = await resolveReqId({ totalFileNum, totalSize: fileSize });
|
||||||
|
if (!resolvedReqId) {
|
||||||
|
throw new Error("Failed to resolve pre-upload request id");
|
||||||
|
}
|
||||||
|
onReqIdResolved?.(resolvedReqId);
|
||||||
|
}
|
||||||
|
if (!resolvedReqId) {
|
||||||
|
throw new Error("Missing pre-upload request id");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传单行内容
|
||||||
|
* 每行作为独立文件上传,fileNo 对应行序号,chunkNo 固定为 1
|
||||||
|
*/
|
||||||
|
async function uploadLine(line: string, index: number): Promise<void> {
|
||||||
|
// 检查是否已取消
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!line.trim()) {
|
||||||
|
skippedEmptyCount++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保留原始文件扩展名
|
||||||
|
const fileIndex = index + 1;
|
||||||
|
const newFileName = `${baseName}_${String(fileIndex).padStart(6, "0")}${fileExtension}`;
|
||||||
|
const blob = new Blob([line], { type: "text/plain" });
|
||||||
|
const lineFile = new File([blob], newFileName, { type: "text/plain" });
|
||||||
|
|
||||||
|
// 计算分片(小文件通常只需要一个分片)
|
||||||
|
const slices = sliceFile(lineFile, DEFAULT_CHUNK_SIZE);
|
||||||
|
const checkSum = await calculateSHA256(slices[0]);
|
||||||
|
|
||||||
|
// 检查是否已取消(计算哈希后)
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("file", slices[0]);
|
||||||
|
formData.append("reqId", resolvedReqId.toString());
|
||||||
|
// 每行作为独立文件上传
|
||||||
|
formData.append("fileNo", fileIndex.toString());
|
||||||
|
formData.append("chunkNo", "1");
|
||||||
|
formData.append("fileName", newFileName);
|
||||||
|
formData.append("fileSize", lineFile.size.toString());
|
||||||
|
formData.append("totalChunkNum", "1");
|
||||||
|
formData.append("checkSumHex", checkSum);
|
||||||
|
if (prefix !== undefined) {
|
||||||
|
formData.append("prefix", prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
await uploadFn(formData, {
|
||||||
|
onUploadProgress: () => {
|
||||||
|
// 单行文件很小,进度主要用于追踪上传状态
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const inFlight = new Set<Promise<void>>();
|
||||||
|
let uploadError: unknown = null;
|
||||||
|
const enqueueUpload = async (line: string, index: number) => {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error("Upload cancelled");
|
||||||
|
}
|
||||||
|
if (uploadError) {
|
||||||
|
throw uploadError;
|
||||||
|
}
|
||||||
|
const uploadPromise = uploadLine(line, index)
|
||||||
|
.then(() => {
|
||||||
|
uploadedCount++;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
uploadError = err;
|
||||||
|
});
|
||||||
|
inFlight.add(uploadPromise);
|
||||||
|
uploadPromise.finally(() => inFlight.delete(uploadPromise));
|
||||||
|
if (inFlight.size >= maxConcurrency) {
|
||||||
|
await Promise.race(inFlight);
|
||||||
|
if (uploadError) {
|
||||||
|
throw uploadError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let uploadResult: { lineCount: number; skippedEmptyCount: number } | null = null;
|
||||||
|
try {
|
||||||
|
uploadResult = await processFileLines(
|
||||||
|
file,
|
||||||
|
chunkSize,
|
||||||
|
signal,
|
||||||
|
enqueueUpload,
|
||||||
|
(currentBytes, totalBytes) => {
|
||||||
|
onProgress?.(currentBytes, totalBytes, uploadedCount);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (uploadError) {
|
||||||
|
throw uploadError;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (inFlight.size > 0) {
|
||||||
|
await Promise.allSettled(inFlight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!uploadResult || (initialReqId && uploadResult.lineCount === 0)) {
|
||||||
|
return {
|
||||||
|
uploadedCount: 0,
|
||||||
|
totalBytes: fileSize,
|
||||||
|
skippedEmptyCount: uploadResult?.skippedEmptyCount ?? 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initialReqId) {
|
||||||
|
skippedEmptyCount = skippedEmptyCount || uploadResult.skippedEmptyCount;
|
||||||
|
} else {
|
||||||
|
skippedEmptyCount = uploadResult.skippedEmptyCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
uploadedCount,
|
||||||
|
totalBytes: fileSize,
|
||||||
|
skippedEmptyCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否需要流式分割上传
|
||||||
|
* @param file 文件对象
|
||||||
|
* @param threshold 阈值,默认 5MB
|
||||||
|
*/
|
||||||
|
export function shouldStreamUpload(file: File, threshold: number = 5 * 1024 * 1024): boolean {
|
||||||
|
return file.size > threshold;
|
||||||
|
}
|
||||||
|
|||||||
@@ -92,6 +92,14 @@ class Request {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 监听 AbortSignal 来中止请求
|
||||||
|
if (config.signal) {
|
||||||
|
config.signal.addEventListener("abort", () => {
|
||||||
|
xhr.abort();
|
||||||
|
reject(new Error("上传已取消"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 监听上传进度
|
// 监听上传进度
|
||||||
xhr.upload.addEventListener("progress", function (event) {
|
xhr.upload.addEventListener("progress", function (event) {
|
||||||
if (event.lengthComputable) {
|
if (event.lengthComputable) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ from app.module.annotation.schema.editor import (
|
|||||||
EditorProjectInfo,
|
EditorProjectInfo,
|
||||||
EditorTaskListResponse,
|
EditorTaskListResponse,
|
||||||
EditorTaskResponse,
|
EditorTaskResponse,
|
||||||
|
EditorTaskSegmentsResponse,
|
||||||
UpsertAnnotationRequest,
|
UpsertAnnotationRequest,
|
||||||
UpsertAnnotationResponse,
|
UpsertAnnotationResponse,
|
||||||
)
|
)
|
||||||
@@ -87,6 +88,20 @@ async def get_editor_task(
|
|||||||
return StandardResponse(code=200, message="success", data=task)
|
return StandardResponse(code=200, message="success", data=task)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/projects/{project_id}/tasks/{file_id}/segments",
|
||||||
|
response_model=StandardResponse[EditorTaskSegmentsResponse],
|
||||||
|
)
|
||||||
|
async def list_editor_task_segments(
|
||||||
|
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
|
||||||
|
file_id: str = Path(..., description="文件ID(t_dm_dataset_files.id)"),
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
):
|
||||||
|
service = AnnotationEditorService(db)
|
||||||
|
result = await service.get_task_segments(project_id, file_id)
|
||||||
|
return StandardResponse(code=200, message="success", data=result)
|
||||||
|
|
||||||
|
|
||||||
@router.put(
|
@router.put(
|
||||||
"/projects/{project_id}/tasks/{file_id}/annotation",
|
"/projects/{project_id}/tasks/{file_id}/annotation",
|
||||||
response_model=StandardResponse[UpsertAnnotationResponse],
|
response_model=StandardResponse[UpsertAnnotationResponse],
|
||||||
|
|||||||
@@ -150,6 +150,18 @@ async def create_mapping(
|
|||||||
labeling_project, snapshot_file_ids
|
labeling_project, snapshot_file_ids
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 如果启用了分段且为文本数据集,预生成切片结构
|
||||||
|
if dataset_type == TEXT_DATASET_TYPE and request.segmentation_enabled:
|
||||||
|
try:
|
||||||
|
from ..service.editor import AnnotationEditorService
|
||||||
|
editor_service = AnnotationEditorService(db)
|
||||||
|
# 异步预计算切片(不阻塞创建响应)
|
||||||
|
segmentation_result = await editor_service.precompute_segmentation_for_project(labeling_project.id)
|
||||||
|
logger.info(f"Precomputed segmentation for project {labeling_project.id}: {segmentation_result}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to precompute segmentation for project {labeling_project.id}: {e}")
|
||||||
|
# 不影响项目创建,只记录警告
|
||||||
|
|
||||||
response_data = DatasetMappingCreateResponse(
|
response_data = DatasetMappingCreateResponse(
|
||||||
id=mapping.id,
|
id=mapping.id,
|
||||||
labeling_project_id=str(mapping.labeling_project_id),
|
labeling_project_id=str(mapping.labeling_project_id),
|
||||||
|
|||||||
@@ -79,12 +79,9 @@ class EditorTaskListResponse(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class SegmentInfo(BaseModel):
|
class SegmentInfo(BaseModel):
|
||||||
"""段落信息(用于文本分段标注)"""
|
"""段落摘要(用于文本分段标注)"""
|
||||||
|
|
||||||
idx: int = Field(..., description="段落索引")
|
idx: int = Field(..., description="段落索引")
|
||||||
text: str = Field(..., description="段落文本")
|
|
||||||
start: int = Field(..., description="在原文中的起始位置")
|
|
||||||
end: int = Field(..., description="在原文中的结束位置")
|
|
||||||
has_annotation: bool = Field(False, alias="hasAnnotation", description="该段落是否已有标注")
|
has_annotation: bool = Field(False, alias="hasAnnotation", description="该段落是否已有标注")
|
||||||
line_index: int = Field(0, alias="lineIndex", description="JSONL 行索引(从0开始)")
|
line_index: int = Field(0, alias="lineIndex", description="JSONL 行索引(从0开始)")
|
||||||
chunk_index: int = Field(0, alias="chunkIndex", description="行内分片索引(从0开始)")
|
chunk_index: int = Field(0, alias="chunkIndex", description="行内分片索引(从0开始)")
|
||||||
@@ -100,13 +97,22 @@ class EditorTaskResponse(BaseModel):
|
|||||||
|
|
||||||
# 分段相关字段
|
# 分段相关字段
|
||||||
segmented: bool = Field(False, description="是否启用分段模式")
|
segmented: bool = Field(False, description="是否启用分段模式")
|
||||||
segments: Optional[List[SegmentInfo]] = Field(None, description="段落列表")
|
|
||||||
total_segments: int = Field(0, alias="totalSegments", description="总段落数")
|
total_segments: int = Field(0, alias="totalSegments", description="总段落数")
|
||||||
current_segment_index: int = Field(0, alias="currentSegmentIndex", description="当前段落索引")
|
current_segment_index: int = Field(0, alias="currentSegmentIndex", description="当前段落索引")
|
||||||
|
|
||||||
model_config = ConfigDict(populate_by_name=True)
|
model_config = ConfigDict(populate_by_name=True)
|
||||||
|
|
||||||
|
|
||||||
|
class EditorTaskSegmentsResponse(BaseModel):
|
||||||
|
"""编辑器段落摘要响应"""
|
||||||
|
|
||||||
|
segmented: bool = Field(False, description="是否启用分段模式")
|
||||||
|
segments: List[SegmentInfo] = Field(default_factory=list, description="段落摘要列表")
|
||||||
|
total_segments: int = Field(0, alias="totalSegments", description="总段落数")
|
||||||
|
|
||||||
|
model_config = ConfigDict(populate_by_name=True)
|
||||||
|
|
||||||
|
|
||||||
class UpsertAnnotationRequest(BaseModel):
|
class UpsertAnnotationRequest(BaseModel):
|
||||||
"""保存/覆盖最终标注(Label Studio annotation 原始对象)"""
|
"""保存/覆盖最终标注(Label Studio annotation 原始对象)"""
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ from app.module.annotation.schema.editor import (
|
|||||||
EditorTaskListItem,
|
EditorTaskListItem,
|
||||||
EditorTaskListResponse,
|
EditorTaskListResponse,
|
||||||
EditorTaskResponse,
|
EditorTaskResponse,
|
||||||
|
EditorTaskSegmentsResponse,
|
||||||
SegmentInfo,
|
SegmentInfo,
|
||||||
UpsertAnnotationRequest,
|
UpsertAnnotationRequest,
|
||||||
UpsertAnnotationResponse,
|
UpsertAnnotationResponse,
|
||||||
@@ -538,6 +539,49 @@ class AnnotationEditorService:
|
|||||||
return value
|
return value
|
||||||
return raw_text
|
return raw_text
|
||||||
|
|
||||||
|
def _build_segment_contexts(
|
||||||
|
self,
|
||||||
|
records: List[Tuple[Optional[Dict[str, Any]], str]],
|
||||||
|
record_texts: List[str],
|
||||||
|
segment_annotation_keys: set[str],
|
||||||
|
) -> Tuple[List[SegmentInfo], List[Tuple[Optional[Dict[str, Any]], str, str, int, int]]]:
|
||||||
|
splitter = AnnotationTextSplitter(max_chars=self.SEGMENT_THRESHOLD)
|
||||||
|
segment_contexts: List[Tuple[Optional[Dict[str, Any]], str, str, int, int]] = []
|
||||||
|
segment_cursor = 0
|
||||||
|
|
||||||
|
for record_index, ((payload, raw_text), record_text) in enumerate(zip(records, record_texts)):
|
||||||
|
normalized_text = record_text or ""
|
||||||
|
if len(normalized_text) > self.SEGMENT_THRESHOLD:
|
||||||
|
raw_segments = splitter.split(normalized_text)
|
||||||
|
for chunk_index, seg in enumerate(raw_segments):
|
||||||
|
segments.append(
|
||||||
|
SegmentInfo(
|
||||||
|
idx=segment_cursor,
|
||||||
|
hasAnnotation=str(segment_cursor) in segment_annotation_keys,
|
||||||
|
lineIndex=record_index,
|
||||||
|
chunkIndex=chunk_index,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
segment_contexts.append((payload, raw_text, seg["text"], record_index, chunk_index))
|
||||||
|
segment_cursor += 1
|
||||||
|
else:
|
||||||
|
segments.append(
|
||||||
|
SegmentInfo(
|
||||||
|
idx=segment_cursor,
|
||||||
|
hasAnnotation=str(segment_cursor) in segment_annotation_keys,
|
||||||
|
lineIndex=record_index,
|
||||||
|
chunkIndex=0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
segment_contexts.append((payload, raw_text, normalized_text, record_index, 0))
|
||||||
|
segment_cursor += 1
|
||||||
|
|
||||||
|
if not segments:
|
||||||
|
segments = [SegmentInfo(idx=0, hasAnnotation=False, lineIndex=0, chunkIndex=0)]
|
||||||
|
segment_contexts = [(None, "", "", 0, 0)]
|
||||||
|
|
||||||
|
return segments, segment_contexts
|
||||||
|
|
||||||
async def get_project_info(self, project_id: str) -> EditorProjectInfo:
|
async def get_project_info(self, project_id: str) -> EditorProjectInfo:
|
||||||
project = await self._get_project_or_404(project_id)
|
project = await self._get_project_or_404(project_id)
|
||||||
|
|
||||||
@@ -668,6 +712,87 @@ class AnnotationEditorService:
|
|||||||
|
|
||||||
return await self._build_text_task(project, file_record, file_id, segment_index)
|
return await self._build_text_task(project, file_record, file_id, segment_index)
|
||||||
|
|
||||||
|
async def get_task_segments(
|
||||||
|
self,
|
||||||
|
project_id: str,
|
||||||
|
file_id: str,
|
||||||
|
) -> EditorTaskSegmentsResponse:
|
||||||
|
project = await self._get_project_or_404(project_id)
|
||||||
|
|
||||||
|
dataset_type = self._normalize_dataset_type(await self._get_dataset_type(project.dataset_id))
|
||||||
|
if dataset_type != DATASET_TYPE_TEXT:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail="当前仅支持 TEXT 项目的段落摘要",
|
||||||
|
)
|
||||||
|
|
||||||
|
file_result = await self.db.execute(
|
||||||
|
select(DatasetFiles).where(
|
||||||
|
DatasetFiles.id == file_id,
|
||||||
|
DatasetFiles.dataset_id == project.dataset_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file_record = file_result.scalar_one_or_none()
|
||||||
|
if not file_record:
|
||||||
|
raise HTTPException(status_code=404, detail=f"文件不存在或不属于该项目: {file_id}")
|
||||||
|
|
||||||
|
if not self._resolve_segmentation_enabled(project):
|
||||||
|
return EditorTaskSegmentsResponse(segmented=False, segments=[], totalSegments=0)
|
||||||
|
|
||||||
|
text_content = await self._fetch_text_content_via_download_api(project.dataset_id, file_id)
|
||||||
|
assert isinstance(text_content, str)
|
||||||
|
label_config = await self._resolve_project_label_config(project)
|
||||||
|
primary_text_key = self._resolve_primary_text_key(label_config)
|
||||||
|
file_name = str(getattr(file_record, "file_name", "")).lower()
|
||||||
|
|
||||||
|
records: List[Tuple[Optional[Dict[str, Any]], str]] = []
|
||||||
|
if file_name.endswith(JSONL_EXTENSION):
|
||||||
|
records = self._parse_jsonl_records(text_content)
|
||||||
|
else:
|
||||||
|
parsed_payload = self._try_parse_json_payload(text_content)
|
||||||
|
if parsed_payload:
|
||||||
|
records = [(parsed_payload, text_content)]
|
||||||
|
|
||||||
|
if not records:
|
||||||
|
records = [(None, text_content)]
|
||||||
|
|
||||||
|
record_texts = [
|
||||||
|
self._resolve_primary_text_value(payload, raw_text, primary_text_key)
|
||||||
|
for payload, raw_text in records
|
||||||
|
]
|
||||||
|
if not record_texts:
|
||||||
|
record_texts = [text_content]
|
||||||
|
|
||||||
|
needs_segmentation = len(records) > 1 or any(
|
||||||
|
len(text or "") > self.SEGMENT_THRESHOLD for text in record_texts
|
||||||
|
)
|
||||||
|
if not needs_segmentation:
|
||||||
|
return EditorTaskSegmentsResponse(segmented=False, segments=[], totalSegments=0)
|
||||||
|
|
||||||
|
ann_result = await self.db.execute(
|
||||||
|
select(AnnotationResult).where(
|
||||||
|
AnnotationResult.project_id == project.id,
|
||||||
|
AnnotationResult.file_id == file_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
ann = ann_result.scalar_one_or_none()
|
||||||
|
segment_annotations: Dict[str, Dict[str, Any]] = {}
|
||||||
|
if ann and isinstance(ann.annotation, dict):
|
||||||
|
segment_annotations = self._extract_segment_annotations(ann.annotation)
|
||||||
|
segment_annotation_keys = set(segment_annotations.keys())
|
||||||
|
|
||||||
|
segments, _ = self._build_segment_contexts(
|
||||||
|
records,
|
||||||
|
record_texts,
|
||||||
|
segment_annotation_keys,
|
||||||
|
)
|
||||||
|
|
||||||
|
return EditorTaskSegmentsResponse(
|
||||||
|
segmented=True,
|
||||||
|
segments=segments,
|
||||||
|
totalSegments=len(segments),
|
||||||
|
)
|
||||||
|
|
||||||
async def _build_text_task(
|
async def _build_text_task(
|
||||||
self,
|
self,
|
||||||
project: LabelingProject,
|
project: LabelingProject,
|
||||||
@@ -723,7 +848,8 @@ class AnnotationEditorService:
|
|||||||
needs_segmentation = segmentation_enabled and (
|
needs_segmentation = segmentation_enabled and (
|
||||||
len(records) > 1 or any(len(text or "") > self.SEGMENT_THRESHOLD for text in record_texts)
|
len(records) > 1 or any(len(text or "") > self.SEGMENT_THRESHOLD for text in record_texts)
|
||||||
)
|
)
|
||||||
segments: Optional[List[SegmentInfo]] = None
|
segments: List[SegmentInfo] = []
|
||||||
|
segment_contexts: List[Tuple[Optional[Dict[str, Any]], str, str, int, int]] = []
|
||||||
current_segment_index = 0
|
current_segment_index = 0
|
||||||
display_text = record_texts[0] if record_texts else text_content
|
display_text = record_texts[0] if record_texts else text_content
|
||||||
selected_payload = records[0][0] if records else None
|
selected_payload = records[0][0] if records else None
|
||||||
@@ -732,46 +858,13 @@ class AnnotationEditorService:
|
|||||||
display_text = "\n".join(record_texts) if record_texts else text_content
|
display_text = "\n".join(record_texts) if record_texts else text_content
|
||||||
|
|
||||||
if needs_segmentation:
|
if needs_segmentation:
|
||||||
splitter = AnnotationTextSplitter(max_chars=self.SEGMENT_THRESHOLD)
|
_, segment_contexts = self._build_segment_contexts(
|
||||||
segment_contexts: List[Tuple[Optional[Dict[str, Any]], str, str, int, int]] = []
|
records,
|
||||||
segments = []
|
record_texts,
|
||||||
segment_cursor = 0
|
segment_annotation_keys,
|
||||||
|
)
|
||||||
for record_index, ((payload, raw_text), record_text) in enumerate(zip(records, record_texts)):
|
|
||||||
normalized_text = record_text or ""
|
|
||||||
if len(normalized_text) > self.SEGMENT_THRESHOLD:
|
|
||||||
raw_segments = splitter.split(normalized_text)
|
|
||||||
for chunk_index, seg in enumerate(raw_segments):
|
|
||||||
segments.append(SegmentInfo(
|
|
||||||
idx=segment_cursor,
|
|
||||||
text=seg["text"],
|
|
||||||
start=seg["start"],
|
|
||||||
end=seg["end"],
|
|
||||||
hasAnnotation=str(segment_cursor) in segment_annotation_keys,
|
|
||||||
lineIndex=record_index,
|
|
||||||
chunkIndex=chunk_index,
|
|
||||||
))
|
|
||||||
segment_contexts.append((payload, raw_text, seg["text"], record_index, chunk_index))
|
|
||||||
segment_cursor += 1
|
|
||||||
else:
|
|
||||||
segments.append(SegmentInfo(
|
|
||||||
idx=segment_cursor,
|
|
||||||
text=normalized_text,
|
|
||||||
start=0,
|
|
||||||
end=len(normalized_text),
|
|
||||||
hasAnnotation=str(segment_cursor) in segment_annotation_keys,
|
|
||||||
lineIndex=record_index,
|
|
||||||
chunkIndex=0,
|
|
||||||
))
|
|
||||||
segment_contexts.append((payload, raw_text, normalized_text, record_index, 0))
|
|
||||||
segment_cursor += 1
|
|
||||||
|
|
||||||
if not segments:
|
|
||||||
segments = [SegmentInfo(idx=0, text="", start=0, end=0, hasAnnotation=False, lineIndex=0, chunkIndex=0)]
|
|
||||||
segment_contexts = [(None, "", "", 0, 0)]
|
|
||||||
|
|
||||||
current_segment_index = segment_index if segment_index is not None else 0
|
current_segment_index = segment_index if segment_index is not None else 0
|
||||||
if current_segment_index < 0 or current_segment_index >= len(segments):
|
if current_segment_index < 0 or current_segment_index >= len(segment_contexts):
|
||||||
current_segment_index = 0
|
current_segment_index = 0
|
||||||
|
|
||||||
selected_payload, _, display_text, _, _ = segment_contexts[current_segment_index]
|
selected_payload, _, display_text, _, _ = segment_contexts[current_segment_index]
|
||||||
@@ -849,8 +942,7 @@ class AnnotationEditorService:
|
|||||||
task=task,
|
task=task,
|
||||||
annotationUpdatedAt=annotation_updated_at,
|
annotationUpdatedAt=annotation_updated_at,
|
||||||
segmented=needs_segmentation,
|
segmented=needs_segmentation,
|
||||||
segments=segments,
|
totalSegments=len(segment_contexts) if needs_segmentation else 1,
|
||||||
totalSegments=len(segments) if segments else 1,
|
|
||||||
currentSegmentIndex=current_segment_index,
|
currentSegmentIndex=current_segment_index,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1185,3 +1277,195 @@ class AnnotationEditorService:
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("标注同步知识管理失败:%s", exc)
|
logger.warning("标注同步知识管理失败:%s", exc)
|
||||||
|
|
||||||
|
async def precompute_segmentation_for_project(
|
||||||
|
self,
|
||||||
|
project_id: str,
|
||||||
|
max_retries: int = 3
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
为指定项目的所有文本文件预计算切片结构并持久化到数据库
|
||||||
|
|
||||||
|
Args:
|
||||||
|
project_id: 标注项目ID
|
||||||
|
max_retries: 失败重试次数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
统计信息:{total_files, succeeded, failed}
|
||||||
|
"""
|
||||||
|
project = await self._get_project_or_404(project_id)
|
||||||
|
dataset_type = self._normalize_dataset_type(await self._get_dataset_type(project.dataset_id))
|
||||||
|
|
||||||
|
# 只处理文本数据集
|
||||||
|
if dataset_type != DATASET_TYPE_TEXT:
|
||||||
|
logger.info(f"项目 {project_id} 不是文本数据集,跳过切片预生成")
|
||||||
|
return {"total_files": 0, "succeeded": 0, "failed": 0}
|
||||||
|
|
||||||
|
# 检查是否启用分段
|
||||||
|
if not self._resolve_segmentation_enabled(project):
|
||||||
|
logger.info(f"项目 {project_id} 未启用分段,跳过切片预生成")
|
||||||
|
return {"total_files": 0, "succeeded": 0, "failed": 0}
|
||||||
|
|
||||||
|
# 获取项目的所有文本文件(排除源文档)
|
||||||
|
files_result = await self.db.execute(
|
||||||
|
select(DatasetFiles)
|
||||||
|
.join(LabelingProjectFile, LabelingProjectFile.file_id == DatasetFiles.id)
|
||||||
|
.where(
|
||||||
|
LabelingProjectFile.project_id == project_id,
|
||||||
|
DatasetFiles.dataset_id == project.dataset_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file_records = files_result.scalars().all()
|
||||||
|
|
||||||
|
if not file_records:
|
||||||
|
logger.info(f"项目 {project_id} 没有文件,跳过切片预生成")
|
||||||
|
return {"total_files": 0, "succeeded": 0, "failed": 0}
|
||||||
|
|
||||||
|
# 过滤源文档文件
|
||||||
|
valid_files = []
|
||||||
|
for file_record in file_records:
|
||||||
|
file_type = str(getattr(file_record, "file_type", "") or "").lower()
|
||||||
|
file_name = str(getattr(file_record, "file_name", "")).lower()
|
||||||
|
is_source_document = (
|
||||||
|
file_type in SOURCE_DOCUMENT_TYPES or
|
||||||
|
any(file_name.endswith(ext) for ext in SOURCE_DOCUMENT_EXTENSIONS)
|
||||||
|
)
|
||||||
|
if not is_source_document:
|
||||||
|
valid_files.append(file_record)
|
||||||
|
|
||||||
|
total_files = len(valid_files)
|
||||||
|
succeeded = 0
|
||||||
|
failed = 0
|
||||||
|
|
||||||
|
label_config = await self._resolve_project_label_config(project)
|
||||||
|
primary_text_key = self._resolve_primary_text_key(label_config)
|
||||||
|
|
||||||
|
for file_record in valid_files:
|
||||||
|
file_id = str(file_record.id) # type: ignore
|
||||||
|
file_name = str(getattr(file_record, "file_name", ""))
|
||||||
|
|
||||||
|
for retry in range(max_retries):
|
||||||
|
try:
|
||||||
|
# 读取文本内容
|
||||||
|
text_content = await self._fetch_text_content_via_download_api(project.dataset_id, file_id)
|
||||||
|
if not isinstance(text_content, str):
|
||||||
|
logger.warning(f"文件 {file_id} 内容不是字符串,跳过切片")
|
||||||
|
failed += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
# 解析文本记录
|
||||||
|
records: List[Tuple[Optional[Dict[str, Any]], str]] = []
|
||||||
|
if file_name.lower().endswith(JSONL_EXTENSION):
|
||||||
|
records = self._parse_jsonl_records(text_content)
|
||||||
|
else:
|
||||||
|
parsed_payload = self._try_parse_json_payload(text_content)
|
||||||
|
if parsed_payload:
|
||||||
|
records = [(parsed_payload, text_content)]
|
||||||
|
|
||||||
|
if not records:
|
||||||
|
records = [(None, text_content)]
|
||||||
|
|
||||||
|
record_texts = [
|
||||||
|
self._resolve_primary_text_value(payload, raw_text, primary_text_key)
|
||||||
|
for payload, raw_text in records
|
||||||
|
]
|
||||||
|
if not record_texts:
|
||||||
|
record_texts = [text_content]
|
||||||
|
|
||||||
|
# 判断是否需要分段
|
||||||
|
needs_segmentation = len(records) > 1 or any(
|
||||||
|
len(text or "") > self.SEGMENT_THRESHOLD for text in record_texts
|
||||||
|
)
|
||||||
|
|
||||||
|
if not needs_segmentation:
|
||||||
|
# 不需要分段的文件,跳过
|
||||||
|
succeeded += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
# 执行切片
|
||||||
|
splitter = AnnotationTextSplitter(max_chars=self.SEGMENT_THRESHOLD)
|
||||||
|
segment_cursor = 0
|
||||||
|
segments = {}
|
||||||
|
|
||||||
|
for record_index, ((payload, raw_text), record_text) in enumerate(zip(records, record_texts)):
|
||||||
|
normalized_text = record_text or ""
|
||||||
|
|
||||||
|
if len(normalized_text) > self.SEGMENT_THRESHOLD:
|
||||||
|
raw_segments = splitter.split(normalized_text)
|
||||||
|
for chunk_index, seg in enumerate(raw_segments):
|
||||||
|
segments[str(segment_cursor)] = {
|
||||||
|
SEGMENT_RESULT_KEY: [],
|
||||||
|
SEGMENT_CREATED_AT_KEY: datetime.utcnow().isoformat() + "Z",
|
||||||
|
SEGMENT_UPDATED_AT_KEY: datetime.utcnow().isoformat() + "Z",
|
||||||
|
}
|
||||||
|
segment_cursor += 1
|
||||||
|
else:
|
||||||
|
segments[str(segment_cursor)] = {
|
||||||
|
SEGMENT_RESULT_KEY: [],
|
||||||
|
SEGMENT_CREATED_AT_KEY: datetime.utcnow().isoformat() + "Z",
|
||||||
|
SEGMENT_UPDATED_AT_KEY: datetime.utcnow().isoformat() + "Z",
|
||||||
|
}
|
||||||
|
segment_cursor += 1
|
||||||
|
|
||||||
|
if not segments:
|
||||||
|
succeeded += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
# 构造分段标注结构
|
||||||
|
final_payload = {
|
||||||
|
SEGMENTED_KEY: True,
|
||||||
|
"version": 1,
|
||||||
|
SEGMENTS_KEY: segments,
|
||||||
|
SEGMENT_TOTAL_KEY: segment_cursor,
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查是否已存在标注
|
||||||
|
existing_result = await self.db.execute(
|
||||||
|
select(AnnotationResult).where(
|
||||||
|
AnnotationResult.project_id == project_id,
|
||||||
|
AnnotationResult.file_id == file_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
existing = existing_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
now = datetime.utcnow()
|
||||||
|
|
||||||
|
if existing:
|
||||||
|
# 更新现有标注
|
||||||
|
existing.annotation = final_payload # type: ignore[assignment]
|
||||||
|
existing.annotation_status = ANNOTATION_STATUS_IN_PROGRESS # type: ignore[assignment]
|
||||||
|
existing.updated_at = now # type: ignore[assignment]
|
||||||
|
else:
|
||||||
|
# 创建新标注记录
|
||||||
|
record = AnnotationResult(
|
||||||
|
id=str(uuid.uuid4()),
|
||||||
|
project_id=project_id,
|
||||||
|
file_id=file_id,
|
||||||
|
annotation=final_payload,
|
||||||
|
annotation_status=ANNOTATION_STATUS_IN_PROGRESS,
|
||||||
|
created_at=now,
|
||||||
|
updated_at=now,
|
||||||
|
)
|
||||||
|
self.db.add(record)
|
||||||
|
|
||||||
|
await self.db.commit()
|
||||||
|
succeeded += 1
|
||||||
|
logger.info(f"成功为文件 {file_id} 预生成 {segment_cursor} 个切片")
|
||||||
|
break
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(
|
||||||
|
f"为文件 {file_id} 预生成切片失败 (重试 {retry + 1}/{max_retries}): {e}"
|
||||||
|
)
|
||||||
|
if retry == max_retries - 1:
|
||||||
|
failed += 1
|
||||||
|
await self.db.rollback()
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"项目 {project_id} 切片预生成完成: 总计 {total_files}, 成功 {succeeded}, 失败 {failed}"
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"total_files": total_files,
|
||||||
|
"succeeded": succeeded,
|
||||||
|
"failed": failed,
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user