You've already forked DataMate
feat(DataManagement): 添加按行分割文件功能
- 引入 Tooltip 和 QuestionCircleOutlined 组件用于提示信息 - 移除未使用的 useMemo 钩子和 fileSliceList 变量 - 新增 splitFileByLines 函数实现文件按行分割逻辑 - 在 handleUpload 函数中集成分行分割功能 - 添加 splitByLine 开关配置项到表单中 - 实现文本文件每行分割成独立文件的功能 - 优化文件上传处理流程以支持分割后的文件列表
This commit is contained in:
@@ -1,13 +1,51 @@
|
|||||||
import { Select, Input, Form, Radio, Modal, Button, UploadFile, Switch } from "antd";
|
import { Select, Input, Form, Radio, Modal, Button, UploadFile, Switch, Tooltip } from "antd";
|
||||||
import { InboxOutlined } from "@ant-design/icons";
|
import { InboxOutlined, QuestionCircleOutlined } from "@ant-design/icons";
|
||||||
import { dataSourceOptions } from "../../dataset.const";
|
import { dataSourceOptions } from "../../dataset.const";
|
||||||
import { Dataset, DataSource } from "../../dataset.model";
|
import { Dataset, DataSource } from "../../dataset.model";
|
||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, 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 } from "@/utils/file.util";
|
||||||
import Dragger from "antd/es/upload/Dragger";
|
import Dragger from "antd/es/upload/Dragger";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行分割文件
|
||||||
|
* @param file 原始文件
|
||||||
|
* @returns 分割后的文件列表,每行一个文件
|
||||||
|
*/
|
||||||
|
async function splitFileByLines(file: UploadFile): Promise<UploadFile[]> {
|
||||||
|
const originFile = (file as any).originFileObj || file;
|
||||||
|
if (!originFile || typeof originFile.text !== "function") {
|
||||||
|
return [file];
|
||||||
|
}
|
||||||
|
|
||||||
|
const text = await originFile.text();
|
||||||
|
if (!text) return [file];
|
||||||
|
|
||||||
|
// 按行分割并过滤空行
|
||||||
|
const lines = text.split(/\r?\n/).filter((line: string) => line.trim() !== "");
|
||||||
|
if (lines.length === 0) return [];
|
||||||
|
|
||||||
|
// 生成文件名:原文件名_序号.扩展名
|
||||||
|
const nameParts = file.name.split(".");
|
||||||
|
const ext = nameParts.length > 1 ? "." + nameParts.pop() : "";
|
||||||
|
const baseName = nameParts.join(".");
|
||||||
|
const padLength = String(lines.length).length;
|
||||||
|
|
||||||
|
return lines.map((line: string, index: number) => {
|
||||||
|
const newFileName = `${baseName}_${String(index + 1).padStart(padLength, "0")}${ext}`;
|
||||||
|
const blob = new Blob([line], { type: "text/plain" });
|
||||||
|
const newFile = new File([blob], newFileName, { type: "text/plain" });
|
||||||
|
return {
|
||||||
|
uid: `${file.uid}-${index}`,
|
||||||
|
name: newFileName,
|
||||||
|
size: newFile.size,
|
||||||
|
type: "text/plain",
|
||||||
|
originFileObj: newFile as any,
|
||||||
|
} as UploadFile;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default function ImportConfiguration({
|
export default function ImportConfiguration({
|
||||||
data,
|
data,
|
||||||
open,
|
open,
|
||||||
@@ -29,13 +67,6 @@ export default function ImportConfiguration({
|
|||||||
const [currentPrefix, setCurrentPrefix] = useState<string>("");
|
const [currentPrefix, setCurrentPrefix] = useState<string>("");
|
||||||
|
|
||||||
const [fileList, setFileList] = useState<UploadFile[]>([]);
|
const [fileList, setFileList] = useState<UploadFile[]>([]);
|
||||||
const fileSliceList = useMemo(() => {
|
|
||||||
const sliceList = fileList.map((file) => {
|
|
||||||
const slices = sliceFile(file);
|
|
||||||
return { originFile: file, slices, name: file.name, size: file.size };
|
|
||||||
});
|
|
||||||
return sliceList;
|
|
||||||
}, [fileList]);
|
|
||||||
|
|
||||||
// 本地上传文件相关逻辑
|
// 本地上传文件相关逻辑
|
||||||
|
|
||||||
@@ -44,16 +75,34 @@ export default function ImportConfiguration({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleUpload = async (dataset: Dataset) => {
|
const handleUpload = async (dataset: Dataset) => {
|
||||||
const formData = new FormData();
|
let filesToUpload = fileList;
|
||||||
fileList.forEach((file) => {
|
|
||||||
formData.append("file", file);
|
// 如果启用分行分割,处理文件
|
||||||
|
if (importConfig.splitByLine) {
|
||||||
|
const splitResults = await Promise.all(
|
||||||
|
fileList.map((file) => splitFileByLines(file))
|
||||||
|
);
|
||||||
|
filesToUpload = splitResults.flat();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算分片列表
|
||||||
|
const sliceList = filesToUpload.map((file) => {
|
||||||
|
const originFile = (file as any).originFileObj || file;
|
||||||
|
const slices = sliceFile(originFile);
|
||||||
|
return {
|
||||||
|
originFile: file,
|
||||||
|
slices,
|
||||||
|
name: file.name,
|
||||||
|
size: (file as any).size || originFile.size || 0,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
console.log('[ImportConfiguration] Uploading with currentPrefix:', currentPrefix);
|
|
||||||
|
console.log("[ImportConfiguration] Uploading with currentPrefix:", currentPrefix);
|
||||||
window.dispatchEvent(
|
window.dispatchEvent(
|
||||||
new CustomEvent("upload:dataset", {
|
new CustomEvent("upload:dataset", {
|
||||||
detail: {
|
detail: {
|
||||||
dataset,
|
dataset,
|
||||||
files: fileSliceList,
|
files: sliceList,
|
||||||
updateEvent,
|
updateEvent,
|
||||||
hasArchive: importConfig.hasArchive,
|
hasArchive: importConfig.hasArchive,
|
||||||
prefix: currentPrefix,
|
prefix: currentPrefix,
|
||||||
@@ -222,6 +271,21 @@ export default function ImportConfiguration({
|
|||||||
>
|
>
|
||||||
<Switch />
|
<Switch />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
label={
|
||||||
|
<span>
|
||||||
|
按分行分割{" "}
|
||||||
|
<Tooltip title="选中后,文本文件的每一行将被分割成独立文件">
|
||||||
|
<QuestionCircleOutlined style={{ color: "#999" }} />
|
||||||
|
</Tooltip>
|
||||||
|
</span>
|
||||||
|
}
|
||||||
|
name="splitByLine"
|
||||||
|
valuePropName="checked"
|
||||||
|
initialValue={false}
|
||||||
|
>
|
||||||
|
<Switch />
|
||||||
|
</Form.Item>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label="上传文件"
|
label="上传文件"
|
||||||
name="files"
|
name="files"
|
||||||
|
|||||||
Reference in New Issue
Block a user