xxHash 32- . 30% , SparkMD5. HTML5 ArrayBuffer, .
var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
var chunkSize = 1024 * 1024 * 2;
$(document).on('drop', function (dropEvent) {
dropEvent.preventDefault();
_.each(dropEvent.originalEvent.dataTransfer.files, function (file) {
var startTime = +new Date(), elapsed;
var chunks = Math.ceil(file.size / chunkSize);
var currentChunk = 0;
var xxh = XXH();
var fileReader = new FileReader();
var readNextChunk = function() {
var start = currentChunk * chunkSize;
var end = Math.min(start + chunkSize, file.size);
fileReader.readAsText(blobSlice.call(file, start, end));
};
fileReader.onload = function (e) {
console.log("read chunk nr", currentChunk + 1, "of", chunks);
xxh.update(e.target.result);
++currentChunk;
if (currentChunk < chunks) {
readNextChunk();
} else {
elapsed = +new Date() - startTime;
console.info("computed hash", xxh.digest().toString(16), 'for file', file.name, 'in', elapsed, 'ms');
}
};
fileReader.onerror = function () {
console.warn("oops, something went wrong.");
};
readNextChunk();
});
});
, blobSlice , . . , API ArrayBuffer, xxHash - HTML5 Uint8Array Node.js Buffer.
Uint8Array.prototype.copy = function(targetBuffer, targetStart, sourceStart, sourceEnd) {
targetStart = targetStart || 0;
sourceStart = sourceStart || 0;
sourceEnd = sourceEnd || this.length;
for(var i=sourceStart; i<sourceEnd; ++i) {
targetBuffer[targetStart+i] = this[i];
}
};
$(document).on('drop', function(dropEvent) {
dropEvent.preventDefault();
_.each(dropEvent.originalEvent.dataTransfer.files, function(file) {
var reader = new FileReader();
var pos = 0;
var startTime = +new Date();
var xxh = XXH();
reader.onprogress = function(progress) {
var length = progress.loaded - pos;
var arr = new Uint8Array(reader.result, pos, length);
pos += length;
xxh.update(arr);
if(progress.lengthComputable) {
console.log((progress.loaded/progress.total*100).toFixed(1)+'%');
}
};
reader.onload = function() {
var arr = new Uint8Array(reader.result, pos);
xxh.update(arr);
var elapsed = +new Date() - startTime;
console.info("computed hash", xxh.digest().toString(16), 'for file', file.name, 'in', elapsed, 'ms');
};
reader.readAsArrayBuffer(file);
});
});
, , . , 270 8 , , 15 .