feat: 调整下载全部通过 rs 能力实现
This commit is contained in:
3
bin/utils/combine.ts
vendored
3
bin/utils/combine.ts
vendored
@@ -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;
|
||||
|
||||
@@ -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<u8>,
|
||||
}
|
||||
|
||||
#[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())
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src-tauri/src/inject/event.js
vendored
19
src-tauri/src/inject/event.js
vendored
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user