瀏覽代碼

上传文件重复上传过滤

cynthia-qin 1 月之前
父節點
當前提交
3d499eeaaf
共有 1 個文件被更改,包括 81 次插入10 次删除
  1. 81 10
      src/dialogs/OCRDialog.vue

+ 81 - 10
src/dialogs/OCRDialog.vue

@@ -220,6 +220,9 @@ const form = ref({
   status: 'queue'
 })
 const fileList = ref<any>([])
+// 记录已导入文件的哈希值
+const importedFileHashes = ref<string[]>([])
+
 const props = defineProps({
   title: { type: String, required: true, default: '工作' },
   queue_category: { type: String, required: true, default: 'SYSTEM' },
@@ -230,7 +233,7 @@ const props = defineProps({
   job_creator: { type: String, required: false, default: 'admin' },
   status: { type: Number, required: false, default: 0 },
 })
-const emit = defineEmits(['update:modelValue', 'success', 'cancel'])
+const emit = defineEmits(['update:modelValue', 'success', 'cancel','closed'])
 const showDialog = (visible: boolean = true) => {
   // console.log("OCRDialog showDialog")
   if (upload.value) {
@@ -238,6 +241,15 @@ const showDialog = (visible: boolean = true) => {
   }
   dialogFormVisible.value = visible
 }
+
+// 清除上传文件列表
+const clearFileList = () => {
+    fileList.value = []
+    if (upload.value) {
+        upload.value.clearFiles()
+    }
+    importedFileHashes.value = [] // 清空已导入文件的哈希值记录
+}
 function handleGetKnowledgeBase() {
   getKnowledgeBase().then((response: any) => {
     // console.log('getKnowledgeBase', response)
@@ -295,6 +307,7 @@ function fetchFile(fileName: string, fileUrl: string) {
 function handleSelectedImport() {
   const SelectionRows = KBTableRef.value.getSelectionRows()
   handleImportFiles(toRaw(SelectionRows))
+  knowledgeBase.value.visible = false
   // console.log(SelectionRows)
 }
 // 使用原生方法计算文件的hash值,该方法兼容性较差
@@ -336,12 +349,12 @@ async function calculateFileHashForCryptoJS(file: File) {
 }
 
 
-function handleImportFiles(filesList: any[]) {
-  for (let i = 0; i < filesList.length; i++) {
-    // if (i > 0) break;
-    fetchFile(filesList[i].file_name, filesList[i].minio_url)
-  }
-}
+// function handleImportFiles(filesList: any[]) {
+//   for (let i = 0; i < filesList.length; i++) {
+//     // if (i > 0) break;
+//     fetchFile(filesList[i].file_name, filesList[i].minio_url)
+//   }
+// }
 
 /**
  * 用于计算文件的hash值,包括sha256值和md5值
@@ -404,9 +417,61 @@ const handleRemove = (file: any, fileList: any) => {
   console.log(file, fileList)
 }
 
+async function calculateFileHash(file: File) {
+  const arrayBuffer = await file.arrayBuffer();
+  const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
+  const hashArray = Array.from(new Uint8Array(hashBuffer));
+  const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
+  return hashHex;
+}
+
+function handleImportFiles(filesList: any[]) {
+  filesList.forEach(async (fileInfo) => {
+    const { file_name, minio_url } = fileInfo;
+    try {
+      const response = await axios.get(minio_url, { responseType: 'blob' });
+      const rawFile = new File([response.data], file_name, { type: response.data.type });
+      const fileHash = await calculateFileHash(rawFile);
+
+      if (importedFileHashes.value.includes(fileHash)) {
+        ElMessage({
+          message: `文件“${file_name}”已导入过,不能重复导入`,
+          type: 'info',
+          plain: true,
+        });
+        return;
+      }
+
+      const file = rawFile as UploadRawFile;
+      file.uid = genFileId();
+      upload.value!.handleStart(file);
+      importedFileHashes.value.push(fileHash);
+      ElMessage({
+        message: `文件“${file_name}”导入成功`,
+        type: 'success',
+        plain: true,
+      });
+    } catch (error) {
+      ElMessage({
+        message: `从知识库获取文件“${file_name}”失败`,
+        type: 'error'
+      });
+      console.log(error);
+    }
+  });
+}
+
 const handleSuccess = (response: any, file: any, fileList: any) => {
-  console.log(response, file, fileList)
-  emit('success')
+  console.log(response, file, fileList);
+  const fileHash = importedFileHashes.value.find(hash => {
+    // 这里简单通过文件名匹配,实际可根据需求优化
+    return file.name === file.raw.name;
+  });
+  if (fileHash) {
+    // 上传成功后保留哈希记录
+    importedFileHashes.value.push(fileHash);
+  }
+  emit('success');
 }
 
 const handleConfirm = () => {
@@ -429,16 +494,22 @@ const handleConfirm = () => {
     }
   })
 }
-defineExpose({ showDialog })
+defineExpose({ showDialog,clearFileList })
 
 function handleClosed() {
   knowledgeBase.value.visible = false
+  clearFileList ()
 }
 
 watch(() => knowledgeBase.value.activeId, (newValue) => {
   debounceGetKBfileList()
   // console.log('activeId', newValue)
 })
+watch(() =>knowledgeBase.value.visible, (newValue) => {
+  if (!newValue) {
+    KBTableRef.value.clearSelection() // 清除选择
+  }
+})
 
 async function checkLinkValidity(index: number, url: string) {
   try {