mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
copy plugin sources
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "tauri-plugin-upload"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tauri.workspace = true
|
||||
log.workspace = true
|
||||
thiserror.workspace = true
|
||||
tokio = { version = "1.17", features = [ "fs" ] }
|
||||
tokio-util = { version = "0.7", features = [ "codec" ] }
|
||||
reqwest = { version = "0.11", features = [ "json", "stream" ] }
|
||||
futures = "0.3"
|
||||
read-progress-stream = "1.0.0"
|
||||
+46
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+38
@@ -0,0 +1,38 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri';
|
||||
import { appWindow } from '@tauri-apps/api/window';
|
||||
|
||||
const handlers = new Map();
|
||||
let listening = false;
|
||||
async function listenToUploadEventIfNeeded() {
|
||||
if (listening) {
|
||||
return await Promise.resolve();
|
||||
}
|
||||
return await appWindow
|
||||
.listen("upload://progress", ({ payload }) => {
|
||||
const handler = handlers.get(payload.id);
|
||||
if (handler != null) {
|
||||
handler(payload.progress, payload.total);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
listening = true;
|
||||
});
|
||||
}
|
||||
async function upload(url, filePath, progressHandler, headers) {
|
||||
const ids = new Uint32Array(1);
|
||||
window.crypto.getRandomValues(ids);
|
||||
const id = ids[0];
|
||||
if (progressHandler != null) {
|
||||
handlers.set(id, progressHandler);
|
||||
}
|
||||
await listenToUploadEventIfNeeded();
|
||||
await invoke("plugin:upload|upload", {
|
||||
id,
|
||||
url,
|
||||
filePath,
|
||||
headers: headers !== null && headers !== void 0 ? headers : {},
|
||||
});
|
||||
}
|
||||
|
||||
export { upload as default };
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAUA,MAAM,QAAQ,GAAiC,IAAI,GAAG,EAAE,CAAC;AACzD,IAAI,SAAS,GAAG,KAAK,CAAC;AAEtB,eAAe,2BAA2B,GAAA;AACxC,IAAA,IAAI,SAAS,EAAE;AACb,QAAA,OAAO,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC;AAChC,KAAA;AACD,IAAA,OAAO,MAAM,SAAS;SACnB,MAAM,CAAkB,mBAAmB,EAAE,CAAC,EAAE,OAAO,EAAE,KAAI;QAC5D,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACzC,IAAI,OAAO,IAAI,IAAI,EAAE;YACnB,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;AAC1C,SAAA;AACH,KAAC,CAAC;SACD,IAAI,CAAC,MAAK;QACT,SAAS,GAAG,IAAI,CAAC;AACnB,KAAC,CAAC,CAAC;AACP,CAAC;AAEc,eAAe,MAAM,CAClC,GAAW,EACX,QAAgB,EAChB,eAAiC,EACjC,OAA6B,EAAA;AAE7B,IAAA,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAC/B,IAAA,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;AACnC,IAAA,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAElB,IAAI,eAAe,IAAI,IAAI,EAAE;AAC3B,QAAA,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,eAAe,CAAC,CAAC;AACnC,KAAA;IAED,MAAM,2BAA2B,EAAE,CAAC;IAEpC,MAAM,MAAM,CAAC,sBAAsB,EAAE;QACnC,EAAE;QACF,GAAG;QACH,QAAQ;AACR,QAAA,OAAO,EAAE,OAAO,KAAA,IAAA,IAAP,OAAO,KAAP,KAAA,CAAA,GAAA,OAAO,GAAI,EAAE;AACvB,KAAA,CAAC,CAAC;AACL;;;;"}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { appWindow } from "@tauri-apps/api/window";
|
||||
|
||||
interface ProgressPayload {
|
||||
id: number;
|
||||
progress: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
type ProgressHandler = (progress: number, total: number) => void;
|
||||
const handlers: Map<number, ProgressHandler> = new Map();
|
||||
let listening = false;
|
||||
|
||||
async function listenToUploadEventIfNeeded(): Promise<void> {
|
||||
if (listening) {
|
||||
return await Promise.resolve();
|
||||
}
|
||||
return await appWindow
|
||||
.listen<ProgressPayload>("upload://progress", ({ payload }) => {
|
||||
const handler = handlers.get(payload.id);
|
||||
if (handler != null) {
|
||||
handler(payload.progress, payload.total);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
listening = true;
|
||||
});
|
||||
}
|
||||
|
||||
export default async function upload(
|
||||
url: string,
|
||||
filePath: string,
|
||||
progressHandler?: ProgressHandler,
|
||||
headers?: Map<string, string>
|
||||
): Promise<void> {
|
||||
const ids = new Uint32Array(1);
|
||||
window.crypto.getRandomValues(ids);
|
||||
const id = ids[0];
|
||||
|
||||
if (progressHandler != null) {
|
||||
handlers.set(id, progressHandler);
|
||||
}
|
||||
|
||||
await listenToUploadEventIfNeeded();
|
||||
|
||||
await invoke("plugin:upload|upload", {
|
||||
id,
|
||||
url,
|
||||
filePath,
|
||||
headers: headers ?? {},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "tauri-plugin-upload-api",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT or APACHE-2.0",
|
||||
"type": "module",
|
||||
"browser": "dist/index.min.js",
|
||||
"module": "dist/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"browser": "./dist/index.min.js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"!dist/**/*.map",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"devDependencies": {
|
||||
"tslib": "^2.4.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tauri-apps/api": "^1.2.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { readFileSync } from "fs";
|
||||
|
||||
import { createConfig } from "../../../shared/rollup.config.mjs";
|
||||
|
||||
export default createConfig({
|
||||
pkg: JSON.parse(
|
||||
readFileSync(new URL("./package.json", import.meta.url), "utf8")
|
||||
),
|
||||
external: [/^@tauri-apps\/api/],
|
||||
});
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../shared/tsconfig.json
|
||||
@@ -0,0 +1,106 @@
|
||||
// Copyright 2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use futures::TryStreamExt;
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
use tauri::{command, plugin::Plugin, Invoke, Runtime, Window};
|
||||
use tokio::fs::File;
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
|
||||
use read_progress_stream::ReadProgressStream;
|
||||
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
Request(#[from] reqwest::Error),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
struct ProgressPayload {
|
||||
id: u32,
|
||||
progress: u64,
|
||||
total: u64,
|
||||
}
|
||||
|
||||
#[command]
|
||||
async fn upload<R: Runtime>(
|
||||
window: Window<R>,
|
||||
id: u32,
|
||||
url: &str,
|
||||
file_path: &str,
|
||||
headers: HashMap<String, String>,
|
||||
) -> Result<serde_json::Value> {
|
||||
// Read the file
|
||||
let file = File::open(file_path).await?;
|
||||
|
||||
// Create the request and attach the file to the body
|
||||
let client = reqwest::Client::new();
|
||||
let mut request = client.post(url).body(file_to_body(id, window, file));
|
||||
|
||||
// Loop trought the headers keys and values
|
||||
// and add them to the request object.
|
||||
for (key, value) in headers {
|
||||
request = request.header(&key, value);
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
response.json().await.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn file_to_body<R: Runtime>(id: u32, window: Window<R>, file: File) -> reqwest::Body {
|
||||
let stream = FramedRead::new(file, BytesCodec::new()).map_ok(|r| r.freeze());
|
||||
let window = Mutex::new(window);
|
||||
reqwest::Body::wrap_stream(ReadProgressStream::new(
|
||||
stream,
|
||||
Box::new(move |progress, total| {
|
||||
let _ = window.lock().unwrap().emit(
|
||||
"upload://progress",
|
||||
ProgressPayload {
|
||||
id,
|
||||
progress,
|
||||
total,
|
||||
},
|
||||
);
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
/// Tauri plugin.
|
||||
pub struct Upload<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for Upload<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![upload]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for Upload<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"upload"
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user