Skip to content

Commit

Permalink
Logic refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Dec 9, 2014
1 parent 88e9eab commit 9802af2
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,32 +319,31 @@ File.prototype.createReadStream = function(options) {

.on('complete', function(res) {
var failed = false;

if (crc32c) {
localCrc32cHash = new Buffer([localCrc32cHash]);
localCrc32cHash = localCrc32cHash.toString('base64');
}

if (md5) {
localMd5Hash = localMd5Hash.digest('base64');
}
var crcFail = true;
var md5Fail = true;

var hashes = {};
res.headers['x-goog-hash'].split(',').forEach(function(hash) {
var hashType = hash.split('=')[0];
hashes[hashType] = hash.substr(hash.indexOf('=') + 1);
});

var remoteMd5 = hashes.md5;
var remoteCrc = hashes.crc32c && hashes.crc32c.substr(4);

if (crc32c) {
crcFail =
new Buffer([localCrc32cHash]).toString('base64') !== remoteCrc;
failed = crcFail;
}

if (md5) {
md5Fail = localMd5Hash.digest('base64') !== remoteMd5;
failed = md5Fail;
}

if (validation === 'all') {
if (hashes.md5) {
failed = localMd5Hash !== hashes.md5;
} else if (hashes.crc32) {
failed = localCrc32cHash !== hashes.crc32c.substr(4);
}
} else if (md5) {
failed = localMd5Hash !== hashes.md5;
} else if (crc32c) {
failed = localCrc32cHash !== hashes.crc32c.substr(4);
failed = remoteMd5 ? md5Fail || crcFail : crcFail;
}

if (failed) {
Expand Down

0 comments on commit 9802af2

Please sign in to comment.