Sha256: b2451296388481bf78e82a31be29f119e6f90b0ad07cd1b6d65cd662942c927c

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents


var CondoMD5Hasher = (function(global) {
	
	var part_size = 1048576,	// This is the amount of the file we read into memory as we are building the hash (1mb)
		async = true;
	
	//
	// Firefox does not have FileReader in webworkers? WTF
	//	See issue #2
	//
	if (!!!global.FileReader) {
		async = false;
		global.FileReader = global.FileReaderSync;
	}

	return function(callback) {
		
		
		//
		// responds with: {success: true|false, result: <Object>}
		//
		this.hash = function(blob) {
			
			var current_part,
				md5 = new global.SparkMD5(),
				reader = new global.FileReader(),
				part_number = 0,
				length = Math.ceil(blob.size / part_size),
				fail = function() {
					callback({
						success: false,
						result: 'file read failed'
					});
				},
				hashData = function(e) {
					md5.appendBinary(e.target.result);
					if(part_number * part_size >= blob.size) {
						callback({
							success: true,
							result: md5.end()
						});
					} else {
						processPart();
					}
				},
				processPart = function() {
					var endbyte = 0;
					
					part_number += 1;
					
					if (blob.size > part_size) {		// If blob bigger then part_size we will slice it up
						endbyte = part_number * part_size;
						if (endbyte > blob.size)
							endbyte = blob.size;
							
						current_part = blob.slice((part_number - 1) * part_size, endbyte);
					} else {
						current_part = blob;
					}
					
					if(async)
						reader.readAsArrayBuffer(current_part);
					else {
						setTimeout(function() {
							try {
								hashData({
									target: {
										result: reader.readAsArrayBuffer(current_part)
									}
								});
							} catch (e) {
								fail();
							}
						}, 1);
					}
						
				};
			
			
			if(async) {
				reader.onload = hashData;
				reader.onerror = fail;
				reader.onabort = fail;
			}
			
			
			processPart();
		};
	};
	
})(this);

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
condo-1.0.3 app/assets/javascripts/condo/md5/hasher.js