Pārlūkot izejas kodu

优化筛选过程 80%

John 1 gadu atpakaļ
vecāks
revīzija
6941fc4b10

+ 24 - 21
src/pages/DuplicateFile/CalculateDuplicateFiles.tsx

@@ -28,6 +28,7 @@ import {
 import { readDir, BaseDirectory } from "@tauri-apps/api/fs";
 import { fileTypeList } from "./config";
 import get_progress_by_sourceId, {
+  get_fileInfo_by_path,
   get_list_by_sourceid,
   updateFileHsah,
 } from "@/services/file-service";
@@ -117,8 +118,6 @@ export default function CalculateDuplicateFiles() {
       setPercent(100);
       console.log('扫描目录文件 结束');
 
-      
-
       console.log(118, files);
 
       // 计算文件属性
@@ -140,25 +139,29 @@ export default function CalculateDuplicateFiles() {
           async (prevPromise: any, currentFile: any) => {
             // 等待上一个 Promise 完成
             await prevPromise;
-            // 获取文件类型和哈希
-            const fileInfo = await File.getInfo(currentFile);
-            // const hash = await File.getHash(currentFile);
-            const hash = "";
-            fileIndex++;
-            setPercent(Math.floor((fileIndex / allFilesLength) * 100));
-            // await waittime(300);
-            return insertSearchFiles({
-              // 组装数据
-              sourceId: `${fileId}`,
-              path: currentFile,
-              // type: await File.getType(elm),
-              name: fileInfo.file_name,
-              creation_time: fileInfo.creation_time,
-              modified_time: fileInfo.modified_time,
-              file_size: fileInfo.file_size,
-              type: fileInfo.file_type,
-              hash,
-            });
+            const [ishaveFile, fileinfo] = await get_fileInfo_by_path(currentFile, `${fileId}`)
+            if(!ishaveFile) {
+              // 获取文件类型和哈希
+              const fileInfo = await File.getInfo(currentFile);
+              // const hash = await File.getHash(currentFile);
+              const hash = "";
+              fileIndex++;
+              setPercent(Math.floor((fileIndex / allFilesLength) * 100));
+              // await waittime(300);
+              return insertSearchFiles({
+                // 组装数据
+                sourceId: `${fileId}`,
+                path: currentFile,
+                // type: await File.getType(elm),
+                name: fileInfo.file_name,
+                creation_time: fileInfo.creation_time,
+                modified_time: fileInfo.modified_time,
+                file_size: fileInfo.file_size,
+                type: fileInfo.file_type,
+                hash,
+              });
+            }
+            return Promise.resolve(0) 
           },
           Promise.resolve(0)
         );

+ 21 - 0
src/services/file-service.ts

@@ -447,6 +447,27 @@ export async function get_fileInfo_by_id(id: string, sourceId: string) {
   }
 }
 
+
+export async function get_fileInfo_by_path(path: string, sourceId: string) {
+  try {
+    const DB = await Database.load(`sqlite:files_${sourceId}.db`);
+    // 创建表
+    await DB.execute(createSql.search_files);
+    const res = await DB.select("SELECT * FROM search_files WHERE path = $1 and sourceId = $2", [
+      path, sourceId
+    ]);
+    if (Array.isArray(res) && res.length) {
+      return [res[0], ""];
+    }
+    return [false, "暂无数据"];
+  } catch (err) {
+    if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
+      return [false, "当前路径重复"];
+    }
+    return [false, `${err}`];
+  }
+}
+
 export async function del_file_by_id(path: string, sourceId: string) {
   try {
     const DB = await Database.load(`sqlite:files_${sourceId}.db`);