Explorar el Código

批量迁移文件

john hace 1 año
padre
commit
3918b8e5e4

+ 45 - 1
src-tauri/src/self_plugin/tauri_plugin_file/files.rs

@@ -1,6 +1,7 @@
 use hex;
 use serde::{Deserialize, Serialize};
-use sha2::{Digest as OtherDigest, Sha256}; // 确保导入 `Digest`
+use sha2::{Digest as OtherDigest, Sha256}; use std::ffi::OsStr;
+// 确保导入 `Digest`
 use std::fs;
 use std::path::{Path, PathBuf};
 use std::time::UNIX_EPOCH;
@@ -299,3 +300,46 @@ pub fn show_file_in_explorer(file_path: String) -> RequestMvFile {
         },
     }
 }
+
+
+// 批量移动指定的多个文件到一个目标目录
+#[command]
+pub fn move_specific_files(file_paths: Vec<PathBuf>, dest_dir: &str) -> RequestMvFile {
+    // 检查目标目录
+    let destination = Path::new(dest_dir);
+    if !destination.is_dir() {
+        return RequestMvFile {
+            code: Some(400),
+            msg: Some("Destination directory does not exist or is not a directory.".to_string()),
+            data: Some("Destination directory does not exist or is not a directory.".to_string()),
+        };
+    }
+
+    // 遍历提供的文件路径列表
+    for file_path in file_paths {
+        if file_path.is_file() {  // 确保路径是文件
+            let dest_file_path = destination.join(
+                file_path.file_name().unwrap_or_else(|| OsStr::new(""))
+            );
+            if let Err(e) = fs::rename(&file_path, &dest_file_path) {
+                return RequestMvFile {
+                    code: Some(500),
+                    msg: Some(format!("Failed to move file '{}': {}", file_path.display(), e)),
+                    data: Some(format!("Failed to move file '{}': {}", file_path.display(), e)),
+                };
+            }
+        } else {
+            return RequestMvFile {
+                code: Some(400),
+                msg: Some(format!("Provided path '{}' is not a file.", file_path.display())),
+                data: Some(format!("Provided path '{}' is not a file.", file_path.display())),
+            };
+        }
+    }
+
+    RequestMvFile {
+        code: Some(200),
+        msg: Some("All specified files moved successfully.".to_string()),
+        data: Some("All specified files moved successfully.".to_string()),
+    }
+}

+ 2 - 1
src-tauri/src/self_plugin/tauri_plugin_file/mod.rs

@@ -18,7 +18,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
             mv_file_to_trash,
             get_app_data_dir,
             // open_finder
-            show_file_in_explorer
+            show_file_in_explorer,
+            move_specific_files
             // get_file_info_by_path,
         ])
         .setup(|_app| {

+ 32 - 18
src/pages/DuplicateFile/CalculateListPage.tsx

@@ -8,6 +8,7 @@ import {
   Space,
   Button,
   Spin,
+  Empty,
 } from "antd";
 import type { CheckboxProps } from "antd";
 import { useEffect, useState } from "react";
@@ -19,8 +20,9 @@ import {
 import {
   message as tauriMessage,
   save as dialogSave,
+  open as dialogopen,
 } from "@tauri-apps/api/dialog";
-import { appDataDir, join } from "@tauri-apps/api/path";
+import { appDataDir, homeDir, join } from "@tauri-apps/api/path";
 import styles from "./CalculateListPage.module.less";
 import { useParams } from "react-router";
 import { insertSearchFilesPasamsType } from "@/types/files";
@@ -112,14 +114,21 @@ export default function CalculateListPage() {
       <div>
         <FolderOpenOutlined onClick={() => openFileShowInExplorer(item.path)} />
       </div>
+      <div className={styles.modified_time}>
+        <CopyText
+          width="300px"
+          color="#333"
+          ellipsisLine={0}
+          name={item.name || ""}
+        ></CopyText>
+      </div>
       <div className={styles.path}>
-        {item.path}
-        {/* <CopyText
+        <CopyText
           width="300px"
           color="#333"
           ellipsisLine={1}
           name={item.path || ""}
-        ></CopyText> */}
+        ></CopyText>
       </div>
       <div className={styles.modified_time}>
         {/* <CopyText
@@ -137,15 +146,6 @@ export default function CalculateListPage() {
         ></CopyText> */}
         {item.file_size}
       </div>
-      <div className={styles.modified_time}>
-        {/* <CopyText
-          width="100px"
-          color="#333"
-          ellipsisLine={1}
-          name={ || ""}
-        ></CopyText> */}
-        {item.name}
-      </div>
     </div>
   );
   const waittime = (time = 100) => {
@@ -203,11 +203,15 @@ export default function CalculateListPage() {
     // const appDataDir = await File.getAppDataDir();
     // const appDataDirPath = await appDataDir();
     // console.log(190, appDataDirPath);
-    const res = await File.showFileInExplorer(
-      "/Users/sysadmin/Downloads/hhyy/src/pages/activity/static/noData.png"
-    );
-    console.log(193, res);
-
+    // 打开本地的系统目录,暂时不支持多选
+    const selected = await dialogopen({
+      title: "请选择需要保存的目录",
+      directory: true,
+      multiple: false,
+      defaultPath: await homeDir(),
+    });
+    console.log(213, selected);
+    await File.moveSpecificFiles(['/Users/honghaitao/Downloads/Xnip2023-05-31_21-39-11_副本.png'], `${selected}`)
     return;
     // dialogSave
     const filePath = await dialogSave({
@@ -276,6 +280,16 @@ export default function CalculateListPage() {
               ))}
             </div>
           </Checkbox.Group>
+          {!data.length && (
+            <div
+              style={{
+                padding: "48px 0",
+                backgroundColor: "#fff",
+              }}
+            >
+              <Empty description={"当前目录没有找到重复的文件"} />
+            </div>
+          )}
         </div>
       </Spin>
     </div>

+ 2 - 2
src/pages/DuplicateFile/FileInfoEditer.tsx

@@ -19,7 +19,7 @@ import { PlusCircleOutlined, RedoOutlined } from "@ant-design/icons";
 import styles from "./FileInfoEditer.module.less";
 
 import { open } from "@tauri-apps/api/dialog";
-import { appDataDir } from "@tauri-apps/api/path";
+import { appDataDir, homeDir } from "@tauri-apps/api/path";
 
 /* 导入类型 */
 import { FileInfoEditerType, FileInfoType } from "@/types/files";
@@ -51,7 +51,7 @@ export default function FileInfoEditer({
     const selected = await open({
       directory: true,
       multiple: false,
-      defaultPath: await appDataDir(),
+      defaultPath: await homeDir(),
     });
     console.log(55, selected);
 

+ 8 - 2
src/plugins/tauri-plugin-file/file.ts

@@ -27,13 +27,13 @@ export class File {
       filePath: path,
     });
   }
-  
+
   static async getInfo(path: string): Promise<any> {
     return await invoke<string>("plugin:st-files|get_file_info", {
       filePath: path,
     });
   }
-  
+
   static async rmFile(path: string): Promise<any> {
     return await invoke<string>("plugin:st-files|mv_file_to_trash", {
       filePath: path,
@@ -47,6 +47,12 @@ export class File {
       filePath
     });
   }
+  static async moveSpecificFiles(filePaths: string[], destDir: string): Promise<string> {
+    return await invoke<string>("plugin:st-files|move_specific_files", {
+      filePaths,
+      destDir
+    });
+  }
 
   // async close(): Promise<boolean> {
   //     return await invoke('plugin:st-sqlite|close', { path: this.path })