From ef75f590379e4180b04832da27ddfd082c4a5104 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 4 Dec 2023 05:19:16 -0500 Subject: [PATCH] fx115: Remove support for OS.File instance from md5Async() --- .../zotero/xpcom/utilities_internal.js | 70 ++++++------------- 1 file changed, 23 insertions(+), 47 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 9ced78dc47..5d8cc1f099 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -138,69 +138,45 @@ Zotero.Utilities.Internal = { /** - * @param {OS.File|nsIFile|String} file File or file path + * @param {nsIFile|String} file File or file path * @param {Boolean} [base64=FALSE] Return as base-64-encoded string * rather than hex string */ md5Async: async function (file, base64) { - const CHUNK_SIZE = 16384; - function toHexString(charCode) { return ("0" + charCode.toString(16)).slice(-2); } var ch = Components.classes["@mozilla.org/security/hash;1"] - .createInstance(Components.interfaces.nsICryptoHash); + .createInstance(Components.interfaces.nsICryptoHash); ch.init(ch.MD5); - // Recursively read chunks of the file and return a promise for the hash - let readChunk = async function (file) { + try { + let is = Cc["@mozilla.org/network/file-input-stream;1"] + .createInstance(Ci.nsIFileInputStream); + is.init(Zotero.File.pathToFile(file), -1, -1, Ci.nsIFileInputStream.CLOSE_ON_EOF); + ch.updateFromStream(is, -1); + let hash = ch.finish(base64); + // Base64 + if (base64) { + return hash; + } + // Hex string + let hexStr = ""; + for (let i = 0; i < hash.length; i++) { + hexStr += toHexString(hash.charCodeAt(i)); + } + return hexStr; + } + catch (e) { try { - let data = await file.read(CHUNK_SIZE); - ch.update(data, data.length); - if (data.length == CHUNK_SIZE) { - return readChunk(file); - } - - let hash = ch.finish(base64); - // Base64 - if (base64) { - return hash; - } - // Hex string - let hexStr = ""; - for (let i = 0; i < hash.length; i++) { - hexStr += toHexString(hash.charCodeAt(i)); - } - return hexStr; + ch.finish(false); } catch (e) { - try { - ch.finish(false); - } - catch (e) { - Zotero.logError(e); - } - throw e; + Zotero.logError(e); } - }; - - if (file instanceof OS.File) { - return readChunk(file); + throw e; } - - var path = (file instanceof Components.interfaces.nsIFile) ? file.path : file; - var hash; - try { - var osFile = await OS.File.open(path); - hash = await readChunk(osFile); - } - finally { - if (osFile) { - await osFile.close(); - } - } - return hash; },