From a2e8be2af5c175a2df898e9e14712889531edea0 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Thu, 6 Jul 2023 13:56:38 +0800 Subject: [PATCH] Fix extremely slow binary to base64 (#3201) --- chrome/content/zotero/xpcom/data/item.js | 19 ++++++++++++------- chrome/content/zotero/xpcom/file.js | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 196447fe31..8a9224d104 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3613,13 +3613,18 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentDataURI', { return ''; } let buf = await OS.File.read(path, {}); - let bytes = new Uint8Array(buf); - let binary = ''; - let len = bytes.byteLength; - for (let i = 0; i < len; i++) { - binary += String.fromCharCode(bytes[i]); - } - return 'data:' + this.attachmentContentType + ';base64,' + btoa(binary); + buf = new Uint8Array(buf).buffer; + return new Promise((resolve, reject) => { + let blob = new Blob([buf], { type: this.attachmentContentType }); + let reader = new FileReader(); + reader.onloadend = function () { + resolve(reader.result); + } + reader.onerror = function (e) { + reject("FileReader error: " + e); + }; + reader.readAsDataURL(blob); + }); } }); diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index 571cf60c77..433ac3d89d 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -831,13 +831,18 @@ Zotero.File = new function(){ } var buf = await OS.File.read(file, {}); - var bytes = new Uint8Array(buf); - var binary = ''; - var len = bytes.byteLength; - for (let i = 0; i < len; i++) { - binary += String.fromCharCode(bytes[i]); - } - return 'data:' + contentType + ';base64,' + btoa(binary); + buf = new Uint8Array(buf).buffer; + return new Promise((resolve, reject) => { + let blob = new Blob([buf], { type: contentType }); + let reader = new FileReader(); + reader.onloadend = function () { + resolve(reader.result); + } + reader.onerror = function (e) { + reject("FileReader error: " + e); + }; + reader.readAsDataURL(blob); + }); };