From 3a5cd014d2bada65490cf0e4c08fd235b0a0e419 Mon Sep 17 00:00:00 2001 From: Coen Hyde Date: Thu, 15 Aug 2013 14:30:14 -0700 Subject: [PATCH] refactored sync functionality to be more readable --- tasks/lib/s3.js | 93 ++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/tasks/lib/s3.js b/tasks/lib/s3.js index 23839d3..e975333 100644 --- a/tasks/lib/s3.js +++ b/tasks/lib/s3.js @@ -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();