Explorar o código

获取文件的hash

John-Hong %!s(int64=2) %!d(string=hai) anos
pai
achega
873367348c

+ 54 - 0
src-tauri/Cargo.lock

@@ -376,6 +376,15 @@ dependencies = [
  "cfg-if",
 ]
 
+[[package]]
+name = "crypto"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "17d88231face754cdb045c1b65912a0f4ff78ca24bf371e30a4a8cfa993b3897"
+dependencies = [
+ "crypto-common",
+]
+
 [[package]]
 name = "crypto-common"
 version = "0.1.6"
@@ -464,6 +473,12 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "data-encoding"
+version = "2.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb"
+
 [[package]]
 name = "dbus"
 version = "0.9.6"
@@ -1021,6 +1036,12 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "hex"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
+
 [[package]]
 name = "html5ever"
 version = "0.25.2"
@@ -2103,6 +2124,21 @@ dependencies = [
  "windows 0.37.0",
 ]
 
+[[package]]
+name = "ring"
+version = "0.16.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
+dependencies = [
+ "cc",
+ "libc",
+ "once_cell",
+ "spin",
+ "untrusted",
+ "web-sys",
+ "winapi",
+]
+
 [[package]]
 name = "rustc_version"
 version = "0.3.3"
@@ -2427,6 +2463,12 @@ dependencies = [
  "system-deps 5.0.0",
 ]
 
+[[package]]
+name = "spin"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
+
 [[package]]
 name = "sqlite"
 version = "0.30.2"
@@ -2676,9 +2718,15 @@ dependencies = [
 name = "tauri-app"
 version = "0.0.1"
 dependencies = [
+ "crypto",
+ "data-encoding",
+ "digest",
+ "hex",
  "regex",
+ "ring",
  "serde",
  "serde_json",
+ "sha2",
  "sqlite",
  "tauri",
  "tauri-build",
@@ -3054,6 +3102,12 @@ version = "1.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a"
 
+[[package]]
+name = "untrusted"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
+
 [[package]]
 name = "url"
 version = "2.3.1"

+ 6 - 0
src-tauri/Cargo.toml

@@ -26,6 +26,12 @@ tauri = {version = "1.2", features = ["api-all"] }
 #tensorflow-sys = "0.22.1"
 uuid = "1.2.2"
 regex = "1.7.0"
+crypto = "0.4.0"
+ring = "0.16.20"
+data-encoding = "2.3.3"
+sha2 = "0.10"
+digest = "0.10.6"
+hex = "0.4.3"
 
 [workspace]
 #members = ["modules"]

+ 37 - 5
src-tauri/src/event_loop/mod.rs → src-tauri/src/event_loop.rs

@@ -1,5 +1,3 @@
-
-
 // 这里先放基础类型数据,如果不够的话再按模块拆分
 #[tauri::command(name = "greet")]
 pub fn greet(name: &str) -> String {
@@ -14,13 +12,13 @@ pub fn file_path(name: &str, parentid: &str) -> String {
     // #[macro_use]
     // extern crate lazy_static;
     extern crate regex;
-    use std::path::Path;
     use crate::files::file_sqlite3::file_sqlite3::{inset, is_create};
     use crate::files::file_struct::File;
+    use std::path::Path;
     use std::time::SystemTime;
     // 生成 UUID
     let uuid = Uuid::new_v4();
-   
+
     let err = is_create("files");
     println!("2424  {}", err);
     // let parent = "".to_string();
@@ -33,7 +31,7 @@ pub fn file_path(name: &str, parentid: &str) -> String {
     let timestamp = system_time
         .duration_since(SystemTime::UNIX_EPOCH)
         .expect("Failed to get timestamp");
-        
+
     let filepath = Path::new(name);
     // 获取路径的最后一个部分(即用户名)
     let filename: String = filepath.file_name().unwrap().to_str().unwrap().to_string();
@@ -76,3 +74,37 @@ pub fn file_path(name: &str, parentid: &str) -> String {
     res
     // "files"
 }
+
+use sha2::{Digest, Sha256};
+use std::{fs, io};
+
+#[derive(Debug)]
+struct UseHashInfoStruct {
+    path: String,
+    processed: u64,
+    hash: String,
+}
+
+fn get_file_hase(path: &str) -> UseHashInfoStruct {
+    let file_path = path;
+    let mut file = fs::File::open(&path).unwrap();
+    let mut hasher = Sha256::new();
+    let n = io::copy(&mut file, &mut hasher).unwrap();
+    let hash = hasher.finalize();
+    let hash_str = format!("{:x}", hash);
+    // println!("Path: {}", path);
+    // println!("Bytes processed: {}", n);
+    // println!("Hash value: {}", hash_str);
+    let info = UseHashInfoStruct {
+        path: file_path.to_string(),
+        processed: n,
+        hash: hash_str,
+    };
+    info
+}
+
+#[tauri::command(name = "file_sort")]
+pub fn file_sort(path: &str) {
+    let hash_info: UseHashInfoStruct = get_file_hase(&path);
+    println!("{:?}", hash_info)
+}

+ 3 - 2
src-tauri/src/main.rs

@@ -4,9 +4,10 @@ mod files;
 // use crate::files::{file_struct, file_tools};
 mod menus;
 mod event_loop;
+
 use crate::menus::default::use_memu;
 use crate::menus::event::m_event;
-use crate::event_loop::{greet, file_path};
+use crate::event_loop::{greet, file_path, file_sort};
 
 
 fn main() {
@@ -16,7 +17,7 @@ fn main() {
             // 处理菜单事件
             m_event(event);
         })
-        .invoke_handler(tauri::generate_handler![greet, file_path])
+        .invoke_handler(tauri::generate_handler![greet, file_path, file_sort])
         .run(tauri::generate_context!())
         .expect("error while running tauri application");
 }

+ 16 - 2
src/pages/FileSort/FileSort.tsx

@@ -1,9 +1,23 @@
 import styles from "./FileSort.module.less";
+import { Button } from "antd";
+import { invoke } from "@tauri-apps/api/tauri";
+import { open } from "@tauri-apps/api/dialog";
 export default function FileSort() {
+  async function sort() {
+
+    const selected = await open({
+      directory: false,
+        multiple: false,
+      //   defaultPath: await appDataDir(),
+    });
+    await invoke("file_sort", {path: selected});
+  }
   return (
     <div className={styles.FileSortBox}>
-      FileClear FileClear
-      <div style={{ backgroundColor: "green", height: "200vh" }}>FileClear</div>
+      {/* <div style={{ backgroundColor: "green", height: "200vh" }}>FileClear</div> */}
+      <div>
+        <Button onClick={() => sort()}>Sort</Button>
+      </div>
     </div>
   );
 }