From b48c7528b76d2b61bf73c735aa20265bb652ec4b Mon Sep 17 00:00:00 2001 From: Tobias Gassmann Date: Thu, 10 May 2018 11:51:35 +0200 Subject: [PATCH] move the example in the repo to the sourcecode basically I just copied the example-code to the sourcecode --- spark-md5.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spark-md5.js b/spark-md5.js index e9ff520..eb7c132 100644 --- a/spark-md5.js +++ b/spark-md5.js @@ -621,6 +621,45 @@ return raw ? hexToBinaryString(ret) : ret; }; + + SparkMD5.hashFile = function(file, callback) { + var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice; + var chunkSize = 2097152; + var chunks = Math.ceil(file.size / chunkSize); + var currentChunk = 0; + var spark = new SparkMD5.ArrayBuffer(); + + var frOnload = function(e){ + spark.append(e.target.result); // append array buffer + currentChunk++; + if (currentChunk < chunks){ + loadNext(); + } + else { + var md5Hash = spark.end(); + file.md5Hash = md5Hash; + callback(file); + } + }; + + var frOnerror = function () { + console.debug('something went wrong'); + }; + + + var loadNext = function() { + var fileReader = new FileReader(); + fileReader.onload = frOnload; + fileReader.onerror = frOnerror; + var start = currentChunk * chunkSize; + var end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize; + + fileReader.readAsArrayBuffer(blobSlice.call(file, start, end)); + }; + + loadNext(); + }; + // --------------------------------------------------- /**