-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No need to download a file when comparing the MD5 hash #88
Conversation
…ader, no need to calculate
This is a good catch. Have you pulled the latest master where the Travis build is fixed? |
Yes I did. Travis failed on testDownload... no idea why. It may be a random thing?
|
@@ -406,7 +406,7 @@ exports.init = function (grunt) { | |||
}).end(); | |||
} else { | |||
// verify was truthy, so we need to make sure that this file is actually the file it thinks it is | |||
client.getFile( dest, function(err, res) { | |||
client.headFile( dest, function(err, res) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we call headFile
for both cases (!options.verify and options.verify
), can we simplify this code a bit?
It could definitely be simplified. I typically code for readability and let a minimizer handle the simplifying. Also, right now they are both block scoped inside their own functions. Where if I moved the |
Actually, looking at this more, I'm not sure what the |
You might be reading it wrong. Here's how it flows.
If the hashes do not match, then we know they are different, we then have to check mtime to verify that the local one is actually newer before uploading and overwriting. Imagine you were to |
Ha, yeah, I apologize. I read that too quickly. Thanks for breaking it down. It looks to me like this can be rewritten with something like this: client.headFile( dest, function(err, res) {
var upload;
// If the file was not found, then we should be able to continue with a normal upload procedure
if (res && res.statusCode === 404) {
upload = exports.upload( src, dest, opts);
// pass through the dfd state
upload.then( dfd.resolve, dfd.reject );
}
else if (!res || err || res.statusCode !== 200 ) {
dfd.reject(makeError(MSG_ERR_DOWNLOAD, src, err || res.statusCode));
}
else {
if (!options.verify) {
return dfd.resolve(util.format(MSG_SKIP_SUCCESS, src));
}
// the file exists so let's check to make sure it's the right file, if not, we'll update it
// Read the local file so we can get its md5 hash.
fs.readFile(src, function (err, data) {
var remoteHash, localHash;
if (err) {
dfd.reject(makeError(MSG_ERR_UPLOAD, src, err));
}
else {
// The etag head in the response from s3 has double quotes around
// it. Strip them out.
remoteHash = res.headers.etag.replace(/"/g, '');
// Get an md5 of the local file so we can verify the upload.
localHash = crypto.createHash('md5').update(data).digest('hex');
if (remoteHash === localHash) {
// the file exists and is the same so do nothing with that
dfd.resolve(util.format(MSG_SKIP_MATCHES, src));
}
else {
fs.stat( src, function(err, stats) {
var remoteWhen, localWhen, upload;
if (err) {
dfd.reject(makeError(MSG_ERR_UPLOAD, src, err));
}
else {
// which one is newer? if local is newer, we should upload it
remoteWhen = new Date(res.headers['last-modified'] || "0"); // earliest date possible if no header is returned
localWhen = new Date(stats.mtime || "1"); // make second earliest date possible if mtime isn't set
if( localWhen > remoteWhen ) {
// default is that local is newer, only upload when it is
upload = exports.upload( src, dest, opts);
// pass through the dfd state
upload.then( dfd.resolve, dfd.reject );
} else {
dfd.resolve(util.format(MSG_SKIP_OLDER, src));
}
}
});
}
}
});
}
}).end(); |
Looks good, updated! |
Looks like Travis was happier that time too. |
No need to download a file when comparing the MD5 hash
Originally the file was needed to compare the MD5 by calculating it. Later, it was found the hash exists on the file's header. Found by @coen-hyde here geedew@fde0f93#commitcomment-3868274