From f70920dbbebc70118c27c1f71647c4c0be54fa49 Mon Sep 17 00:00:00 2001 From: jeasonnow Date: Sun, 25 Jun 2023 15:24:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E9=80=9A=E8=BF=87=20rs=20=E8=83=BD=E5=8A=9B?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/utils/combine.ts | 3 ++- src-tauri/src/app/invoke.rs | 27 +++++++++++++++++++++++++++ src-tauri/src/inject/event.js | 19 +++++++++---------- src-tauri/src/main.rs | 4 ++-- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/bin/utils/combine.ts b/bin/utils/combine.ts index 9599669..0fc1939 100644 --- a/bin/utils/combine.ts +++ b/bin/utils/combine.ts @@ -6,7 +6,8 @@ export default async function combineFiles(files: string[], output: string) { if (file.endsWith('.css')) { return "window.addEventListener('DOMContentLoaded', (_event) => { const css = `" + fileContent + "`; const style = document.createElement('style'); style.innerHTML = css; document.head.appendChild(style); });"; } - return fileContent; + + return "window.addEventListener('DOMContentLoaded', (_event) => { " + fileContent + " });"; }); fs.writeFileSync(output, contents.join('\n')); return files; diff --git a/src-tauri/src/app/invoke.rs b/src-tauri/src/app/invoke.rs index 4553d74..5c13f36 100644 --- a/src-tauri/src/app/invoke.rs +++ b/src-tauri/src/app/invoke.rs @@ -1,3 +1,5 @@ +use std::{fs}; + use crate::util::{check_file_or_append, get_download_message, show_toast}; use download_rs::sync_download::Download; use tauri::{api, command, AppHandle, Manager, Window}; @@ -8,6 +10,12 @@ pub struct DownloadFileParams { filename: String, } +#[derive(serde::Deserialize)] +pub struct BinaryDownloadParams { + filename: String, + binary: Vec, +} + #[command] pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> { let window: Window = app.get_window("pake").unwrap(); @@ -25,3 +33,22 @@ pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result } } } + + +#[command] +pub async fn download_file_by_binary(app: AppHandle, params: BinaryDownloadParams) -> Result<(), String> { + let window: Window = app.get_window("pake").unwrap(); + let output_path = api::path::download_dir().unwrap().join(params.filename); + let file_path = check_file_or_append(output_path.to_str().unwrap()); + let download_file_result = fs::write(&file_path, ¶ms.binary); + match download_file_result { + Ok(_) => { + show_toast(&window, &get_download_message()); + Ok(()) + }, + Err(e) => { + show_toast(&window, &e.to_string()); + Err(e.to_string()) + } + } +} \ No newline at end of file diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js index 2beca4d..2bf8afa 100644 --- a/src-tauri/src/inject/event.js +++ b/src-tauri/src/inject/event.js @@ -164,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => { e.metaKey || e.ctrlKey || isDownloadLink(absoluteUrl)) && - !externalDownLoadLink() + !externalDownLoadLink() && !url.startsWith("blob") ) { e.preventDefault(); invoke('download_file', { @@ -257,19 +257,18 @@ function convertBlobUrlToBinary(blobUrl) { reader.readAsArrayBuffer(blob); reader.onload = () => { - resolve(reader.result); + resolve(Array.from(new Uint8Array(reader.result))); }; }); } function downloadFromBlobUrl(blobUrl, filename) { - const tauri = window.__TAURI__; convertBlobUrlToBinary(blobUrl).then((binary) => { - console.log('binary', binary); - tauri.fs.writeBinaryFile(filename, binary, { - dir: tauri.fs.BaseDirectory.Download, - }).then(() => { - window.pakeToast('Download successful, saved to download directory~'); + invoke('download_file_by_binary', { + params: { + filename, + binary + }, }); }); } @@ -277,7 +276,6 @@ function downloadFromBlobUrl(blobUrl, filename) { // detect blob download by createElement("a") function detectDownloadByCreateAnchor() { const createEle = document.createElement; - const tauri = window.__TAURI__; document.createElement = (el) => { if (el !== 'a') return createEle.call(document, el); const anchorEle = createEle.call(document, el); @@ -286,9 +284,10 @@ function detectDownloadByCreateAnchor() { anchorEle.addEventListener('click', () => { const url = anchorEle.href; if (window.blobToUrlCaches.has(url)) { + downloadFromBlobUrl(url, anchorEle.download || getFilenameFromUrl(url)); } - }); + }, true); return anchorEle; }; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e51ca4f..129e3b7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -7,7 +7,7 @@ mod app; mod util; use app::{invoke, menu, window}; -use invoke::download_file; +use invoke::{download_file, download_file_by_binary}; use menu::{get_menu, menu_event_handle}; use tauri_plugin_window_state::Builder as windowStatePlugin; use util::{get_data_dir, get_pake_config}; @@ -41,7 +41,7 @@ pub fn run_app() { tauri_app .plugin(windowStatePlugin::default().build()) - .invoke_handler(tauri::generate_handler![download_file]) + .invoke_handler(tauri::generate_handler![download_file, download_file_by_binary]) .setup(|app| { let _window = get_window(app, pake_config, data_dir); // Prevent initial shaking