Browse Source

✨ feat(upload): 添加上传插件并更新相关配置

陈雪 2 weeks ago
parent
commit
021ddc2d75
7 changed files with 98 additions and 1 deletions
  1. 1 0
      package.json
  2. 10 0
      pnpm-lock.yaml
  3. 47 0
      src-tauri/Cargo.lock
  4. 1 0
      src-tauri/Cargo.toml
  5. 12 1
      src-tauri/capabilities/default.json
  6. 1 0
      src-tauri/src/lib.rs
  7. 26 0
      src/utils/request.ts

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "@tauri-apps/plugin-opener": "^2",
     "@tauri-apps/plugin-sql": "^2.2.0",
     "@tauri-apps/plugin-store": "~2",
+    "@tauri-apps/plugin-upload": "~2",
     "element-plus": "^2.9.5",
     "pinia": "^3.0.1",
     "tailwindcss": "^4.0.8",

+ 10 - 0
pnpm-lock.yaml

@@ -29,6 +29,9 @@ importers:
       '@tauri-apps/plugin-store':
         specifier: ~2
         version: 2.2.0
+      '@tauri-apps/plugin-upload':
+        specifier: ~2
+        version: 2.2.1
       element-plus:
         specifier: ^2.9.5
         version: 2.9.5(vue@3.5.13(typescript@5.7.3))
@@ -549,6 +552,9 @@ packages:
   '@tauri-apps/plugin-store@2.2.0':
     resolution: {integrity: sha512-hJTRtuJis4w5fW1dkcgftsYxKXK0+DbAqurZ3CURHG5WkAyyZgbxpeYctw12bbzF9ZbZREXZklPq8mocCC3Sgg==}
 
+  '@tauri-apps/plugin-upload@2.2.1':
+    resolution: {integrity: sha512-2EyVhJYLAp2mJH0UzO3QGU0vPk/YWvAfdI2wXbczyzEZY/AZVa9wConyB1TV/NGhyJRim4LwWgkmnEvcKLkECw==}
+
   '@types/estree@1.0.6':
     resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==}
 
@@ -1398,6 +1404,10 @@ snapshots:
     dependencies:
       '@tauri-apps/api': 2.2.0
 
+  '@tauri-apps/plugin-upload@2.2.1':
+    dependencies:
+      '@tauri-apps/api': 2.2.0
+
   '@types/estree@1.0.6': {}
 
   '@types/lodash-es@4.17.12':

+ 47 - 0
src-tauri/Cargo.lock

@@ -1217,6 +1217,21 @@ dependencies = [
 ]
 
 [[package]]
+name = "futures"
+version = "0.3.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-executor",
+ "futures-io",
+ "futures-sink",
+ "futures-task",
+ "futures-util",
+]
+
+[[package]]
 name = "futures-channel"
 version = "0.3.31"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1302,6 +1317,7 @@ version = "0.3.31"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
 dependencies = [
+ "futures-channel",
  "futures-core",
  "futures-io",
  "futures-macro",
@@ -3406,6 +3422,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
 
 [[package]]
+name = "read-progress-stream"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6435842fc2fea44b528719eb8c32203bbc1bb2f5b619fbe0c0a3d8350fd8d2a8"
+dependencies = [
+ "bytes",
+ "futures",
+ "pin-project-lite",
+]
+
+[[package]]
 name = "redox_syscall"
 version = "0.5.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -4232,6 +4259,7 @@ dependencies = [
  "tauri-plugin-opener",
  "tauri-plugin-sql",
  "tauri-plugin-store",
+ "tauri-plugin-upload",
 ]
 
 [[package]]
@@ -4657,6 +4685,25 @@ dependencies = [
 ]
 
 [[package]]
+name = "tauri-plugin-upload"
+version = "2.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e90ac6d3a783d4406caeae8c75aa05e96346474765517fddfd1dc313ff91aa89"
+dependencies = [
+ "futures-util",
+ "log",
+ "read-progress-stream",
+ "reqwest",
+ "serde",
+ "serde_json",
+ "tauri",
+ "tauri-plugin",
+ "thiserror 2.0.11",
+ "tokio",
+ "tokio-util",
+]
+
+[[package]]
 name = "tauri-runtime"
 version = "2.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"

+ 1 - 0
src-tauri/Cargo.toml

@@ -24,6 +24,7 @@ serde = { version = "1", features = ["derive"] }
 serde_json = "1"
 tauri-plugin-store = "2"
 tauri-plugin-http = "2"
+tauri-plugin-upload = "2"
 [dependencies.tauri-plugin-sql]
 features = ["sqlite"] # or "postgres", or "mysql"
 version = "2"

+ 12 - 1
src-tauri/capabilities/default.json

@@ -11,6 +11,17 @@
     "sql:default",
     "sql:allow-execute",
     "store:default",
-    "http:default"
+    {
+      "identifier": "http:default",
+      "allow": [
+        "http://**",
+        "https://**"
+      ],
+      "deny": [
+        "http://**",
+        "https://**"
+      ]
+    },
+    "upload:default"
   ]
 }

+ 1 - 0
src-tauri/src/lib.rs

@@ -11,6 +11,7 @@ pub fn run() {
         kind: MigrationKind::Up,
     }];
     tauri::Builder::default()
+        .plugin(tauri_plugin_upload::init())
         .plugin(tauri_plugin_http::init())
         .plugin(tauri_plugin_store::Builder::new().build())
         .plugin(

+ 26 - 0
src/utils/request.ts

@@ -1 +1,27 @@
 import { fetch } from '@tauri-apps/plugin-http'
+
+interface RequestOptions {
+  method: 'GET' | 'POST' | 'PUT' | 'DELETE'
+  url: string
+  headers?: Record<string, string>
+  body?: unknown
+}
+
+export const request = async ({ method, url, headers, body }: RequestOptions) => {
+  try {
+    const response = await fetch(url, {
+      method,
+      headers,
+      body: body ? JSON.stringify(body) : undefined,
+    })
+
+    if (!response.ok) {
+      throw new Error(`HTTP error! status: ${response.status}`)
+    }
+
+    return await response.json()
+  } catch (error) {
+    console.error('Request failed', error)
+    throw error
+  }
+}