Skip to content

Commit

Permalink
Merge pull request pifantastic#90 from coen-hyde/refactor-sync
Browse files Browse the repository at this point in the history
Refactored sync functionality to be more readable
  • Loading branch information
pifantastic committed Aug 15, 2013
2 parents dc9f543 + 3a5cd01 commit ca8062e
Showing 1 changed file with 45 additions and 48 deletions.
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

0 comments on commit ca8062e

Please sign in to comment.