Skip to content

Commit

Permalink
feat: 调整下载全部通过 rs 能力实现
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonnow committed Jun 25, 2023
1 parent 3c97825 commit f70920d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion bin/utils/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src-tauri/src/app/invoke.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -8,6 +10,12 @@ pub struct DownloadFileParams {
filename: String,
}

#[derive(serde::Deserialize)]
pub struct BinaryDownloadParams {
filename: String,
binary: Vec<u8>,
}

#[command]
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
let window: Window = app.get_window("pake").unwrap();
Expand All @@ -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, &params.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())
}
}
}
19 changes: 9 additions & 10 deletions src-tauri/src/inject/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => {
e.metaKey ||
e.ctrlKey ||
isDownloadLink(absoluteUrl)) &&
!externalDownLoadLink()
!externalDownLoadLink() && !url.startsWith("blob")
) {
e.preventDefault();
invoke('download_file', {
Expand Down Expand Up @@ -257,27 +257,25 @@ 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
},
});
});
}

// 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);
Expand All @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f70920d

Please sign in to comment.