fx115: Remove support for OS.File instance from md5Async()
This commit is contained in:
parent
95caebde2b
commit
ef75f59037
1 changed files with 23 additions and 47 deletions
|
@ -138,13 +138,11 @@ 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);
|
||||
}
|
||||
|
@ -153,15 +151,11 @@ Zotero.Utilities.Internal = {
|
|||
.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 data = await file.read(CHUNK_SIZE);
|
||||
ch.update(data, data.length);
|
||||
if (data.length == CHUNK_SIZE) {
|
||||
return readChunk(file);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -183,24 +177,6 @@ Zotero.Utilities.Internal = {
|
|||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
if (file instanceof OS.File) {
|
||||
return readChunk(file);
|
||||
}
|
||||
|
||||
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;
|
||||
},
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue