From 05c62d731fa48fd06b8cb3694a962d8cb0db8619 Mon Sep 17 00:00:00 2001 From: enri90 Date: Tue, 3 Dec 2024 14:35:11 +0100 Subject: [PATCH] feat(upload): Added body to download function (#1523) Co-authored-by: FabianLars --- .changes/feat-download-on-post.md | 6 ++++++ plugins/fs/src/file_path.rs | 4 ++-- plugins/os/src/lib.rs | 2 +- plugins/shell/src/scope.rs | 2 +- plugins/stronghold/src/lib.rs | 2 +- plugins/upload/api-iife.js | 2 +- plugins/upload/guest-js/index.ts | 6 ++++-- plugins/upload/src/lib.rs | 12 ++++++++---- 8 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 .changes/feat-download-on-post.md diff --git a/.changes/feat-download-on-post.md b/.changes/feat-download-on-post.md new file mode 100644 index 00000000..8f2fe206 --- /dev/null +++ b/.changes/feat-download-on-post.md @@ -0,0 +1,6 @@ +--- +upload: patch +upload-js: patch +--- + +Added post request on download function \ No newline at end of file diff --git a/plugins/fs/src/file_path.rs b/plugins/fs/src/file_path.rs index 9ff7a947..735fc105 100644 --- a/plugins/fs/src/file_path.rs +++ b/plugins/fs/src/file_path.rs @@ -138,7 +138,7 @@ impl<'de> serde::Deserialize<'de> for FilePath { { struct FilePathVisitor; - impl<'de> serde::de::Visitor<'de> for FilePathVisitor { + impl serde::de::Visitor<'_> for FilePathVisitor { type Value = FilePath; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -169,7 +169,7 @@ impl<'de> serde::Deserialize<'de> for SafeFilePath { { struct SafeFilePathVisitor; - impl<'de> serde::de::Visitor<'de> for SafeFilePathVisitor { + impl serde::de::Visitor<'_> for SafeFilePathVisitor { type Value = SafeFilePath; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index 913d1742..c041c8ea 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -110,7 +110,7 @@ struct InitJavascript<'a> { exe_extension: &'a str, } -impl<'a> InitJavascript<'a> { +impl InitJavascript<'_> { fn new() -> Self { Self { #[cfg(windows)] diff --git a/plugins/shell/src/scope.rs b/plugins/shell/src/scope.rs index 9e76931a..d46bfa20 100644 --- a/plugins/shell/src/scope.rs +++ b/plugins/shell/src/scope.rs @@ -224,7 +224,7 @@ impl OpenScope { } } -impl<'a> ShellScope<'a> { +impl ShellScope<'_> { /// Validates argument inputs and creates a Tauri sidecar [`Command`]. pub fn prepare_sidecar( &self, diff --git a/plugins/stronghold/src/lib.rs b/plugins/stronghold/src/lib.rs index 3454a662..23acc3a2 100644 --- a/plugins/stronghold/src/lib.rs +++ b/plugins/stronghold/src/lib.rs @@ -125,7 +125,7 @@ impl<'de> Deserialize<'de> for KeyType { { struct KeyTypeVisitor; - impl<'de> Visitor<'de> for KeyTypeVisitor { + impl Visitor<'_> for KeyTypeVisitor { type Value = KeyType; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { diff --git a/plugins/upload/api-iife.js b/plugins/upload/api-iife.js index 3dfe99be..5918901f 100644 --- a/plugins/upload/api-iife.js +++ b/plugins/upload/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_UPLOAD__=function(t){"use strict";function e(t,e,n,s){if("a"===n&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?s:"a"===n?s.call(t):s?s.value:e.get(t)}function n(t,e,n,s,o){if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return e.set(t,n),n}var s,o,r;"function"==typeof SuppressedError&&SuppressedError;const i="__TAURI_TO_IPC_KEY__";class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,s.set(this,(()=>{})),o.set(this,0),r.set(this,{}),this.id=function(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}((({message:t,id:i})=>{if(i===e(this,o,"f")){n(this,o,i+1),e(this,s,"f").call(this,t);const a=Object.keys(e(this,r,"f"));if(a.length>0){let t=i+1;for(const n of a.sort()){if(parseInt(n)!==t)break;{const o=e(this,r,"f")[n];delete e(this,r,"f")[n],e(this,s,"f").call(this,o),t+=1}}n(this,o,t)}}else e(this,r,"f")[i.toString()]=t}))}set onmessage(t){n(this,s,t)}get onmessage(){return e(this,s,"f")}[(s=new WeakMap,o=new WeakMap,r=new WeakMap,i)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[i]()}}async function _(t,e={},n){return window.__TAURI_INTERNALS__.invoke(t,e,n)}return t.download=async function(t,e,n,s){const o=new Uint32Array(1);window.crypto.getRandomValues(o);const r=o[0],i=new a;n&&(i.onmessage=n),await _("plugin:upload|download",{id:r,url:t,filePath:e,headers:s??{},onProgress:i})},t.upload=async function(t,e,n,s){const o=new Uint32Array(1);window.crypto.getRandomValues(o);const r=o[0],i=new a;return n&&(i.onmessage=n),await _("plugin:upload|upload",{id:r,url:t,filePath:e,headers:s??{},onProgress:i})},t}({});Object.defineProperty(window.__TAURI__,"upload",{value:__TAURI_PLUGIN_UPLOAD__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_UPLOAD__=function(t){"use strict";function e(t,e,n,o){if("a"===n&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?o:"a"===n?o.call(t):o?o.value:e.get(t)}function n(t,e,n,o,s){if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return e.set(t,n),n}var o,s,r;"function"==typeof SuppressedError&&SuppressedError;const i="__TAURI_TO_IPC_KEY__";class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,o.set(this,(()=>{})),s.set(this,0),r.set(this,{}),this.id=function(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}((({message:t,id:i})=>{if(i===e(this,s,"f")){n(this,s,i+1),e(this,o,"f").call(this,t);const a=Object.keys(e(this,r,"f"));if(a.length>0){let t=i+1;for(const n of a.sort()){if(parseInt(n)!==t)break;{const s=e(this,r,"f")[n];delete e(this,r,"f")[n],e(this,o,"f").call(this,s),t+=1}}n(this,s,t)}}else e(this,r,"f")[i.toString()]=t}))}set onmessage(t){n(this,o,t)}get onmessage(){return e(this,o,"f")}[(o=new WeakMap,s=new WeakMap,r=new WeakMap,i)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[i]()}}async function _(t,e={},n){return window.__TAURI_INTERNALS__.invoke(t,e,n)}return t.download=async function(t,e,n,o,s){const r=new Uint32Array(1);window.crypto.getRandomValues(r);const i=r[0],c=new a;n&&(c.onmessage=n),await _("plugin:upload|download",{id:i,url:t,filePath:e,headers:o??{},onProgress:c,body:s})},t.upload=async function(t,e,n,o){const s=new Uint32Array(1);window.crypto.getRandomValues(s);const r=s[0],i=new a;return n&&(i.onmessage=n),await _("plugin:upload|upload",{id:r,url:t,filePath:e,headers:o??{},onProgress:i})},t}({});Object.defineProperty(window.__TAURI__,"upload",{value:__TAURI_PLUGIN_UPLOAD__})} diff --git a/plugins/upload/guest-js/index.ts b/plugins/upload/guest-js/index.ts index d036f00a..03129db4 100644 --- a/plugins/upload/guest-js/index.ts +++ b/plugins/upload/guest-js/index.ts @@ -45,7 +45,8 @@ async function download( url: string, filePath: string, progressHandler?: ProgressHandler, - headers?: Map + headers?: Map, + body?: string ): Promise { const ids = new Uint32Array(1) window.crypto.getRandomValues(ids) @@ -61,7 +62,8 @@ async function download( url, filePath, headers: headers ?? {}, - onProgress + onProgress, + body }) } diff --git a/plugins/upload/src/lib.rs b/plugins/upload/src/lib.rs index d6aa3fb9..23c33b11 100644 --- a/plugins/upload/src/lib.rs +++ b/plugins/upload/src/lib.rs @@ -69,12 +69,16 @@ async fn download( url: &str, file_path: &str, headers: HashMap, + body: Option, on_progress: Channel, ) -> Result<()> { let client = reqwest::Client::new(); - - let mut request = client.get(url); - // Loop through the headers keys and values + let mut request = if let Some(body) = body { + client.post(url).body(body) + } else { + client.get(url) + }; + // Loop trought the headers keys and values // and add them to the request object. for (key, value) in headers { request = request.header(&key, value); @@ -206,7 +210,7 @@ mod tests { let _ = msg; Ok(()) }); - download(url, file_path, headers, sender).await + download(url, file_path, headers, None, sender).await } async fn spawn_server_mocked(return_status: usize) -> MockedServer {