From b28509eb7d4ad7faab3f2417facb19bd782ae383 Mon Sep 17 00:00:00 2001 From: jeasonnow Date: Wed, 19 Jul 2023 16:51:55 +0800 Subject: [PATCH] feat: support datauri download --- src-tauri/src/inject/event.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js index 2bf8afa..b9e53b4 100644 --- a/src-tauri/src/inject/event.js +++ b/src-tauri/src/inject/event.js @@ -262,6 +262,28 @@ function convertBlobUrlToBinary(blobUrl) { }); } +function downladFromDataUri(dataURI) { + const byteString = atob(dataURI.split(',')[1]); + // write the bytes of the string to an ArrayBuffer + const bufferArray = new ArrayBuffer(byteString.length); + + // create a view into the buffer + const binary = new Uint8Array(bufferArray); + + // set the bytes of the buffer to the correct values + for (var i = 0; i < byteString.length; i++) { + binary[i] = byteString.charCodeAt(i); + } + + // write the ArrayBuffer to a binary, and you're done + invoke('download_file_by_binary', { + params: { + filename, + binary + }, + }); +} + function downloadFromBlobUrl(blobUrl, filename) { convertBlobUrlToBinary(blobUrl).then((binary) => { invoke('download_file_by_binary', { @@ -284,8 +306,10 @@ function detectDownloadByCreateAnchor() { anchorEle.addEventListener('click', () => { const url = anchorEle.href; if (window.blobToUrlCaches.has(url)) { - downloadFromBlobUrl(url, anchorEle.download || getFilenameFromUrl(url)); + // case: downoload from dataURL -> convert dataURL -> + } else if (url.startsWith('data:')) { + downladFromDataUri(url); } }, true);