Skip to content
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

Refactored sync functionality to be more readable #90

Merged
merged 1 commit into from
Aug 15, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 45 additions & 48 deletions tasks/lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,64 +396,61 @@ exports.init = function (grunt) {
if (res && res.statusCode === 404) {
upload = exports.upload( src, dest, opts);
// pass through the dfd state
upload.then( dfd.resolve, dfd.reject );
return upload.then( dfd.resolve, dfd.reject );
}
else if (!res || err || res.statusCode !== 200 ) {
dfd.reject(makeError(MSG_ERR_DOWNLOAD, src, err || res.statusCode));

if (!res || err || res.statusCode !== 200 ) {
return dfd.reject(makeError(MSG_ERR_DOWNLOAD, src, err || res.statusCode));
}
else {
// we do not wish to overwrite a file that exists by verifying we have a newer one in place
if( !options.verify ) {
// the file exists so do nothing with that
dfd.resolve(util.format(MSG_SKIP_SUCCESS, src));

// we do not wish to overwrite a file that exists by verifying we have a newer one in place
if( !options.verify ) {
// the file exists so do nothing with that
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) {
return dfd.reject(makeError(MSG_ERR_UPLOAD, src, err));
}
// The etag head in the response from s3 has double quotes around
// it. Strip them out.
remoteHash = res.headers.etag.replace(/"/g, '');

// 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;
// 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
return dfd.resolve(util.format(MSG_SKIP_MATCHES, src));
}

fs.stat( src, function(err, stats) {
var remoteWhen, localWhen, upload;

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, '');
return dfd.reject(makeError(MSG_ERR_UPLOAD, src, err));
}

// Get an md5 of the local file so we can verify the upload.
localHash = crypto.createHash('md5').update(data).digest('hex');
// 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 (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));
}
}
});
}
if ( localWhen <= remoteWhen ) {
// Remote file was older
return dfd.resolve(util.format(MSG_SKIP_OLDER, src));
}

// 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 );
});
}
});
}).end();

return dfd.promise();
Expand Down