Skip to content

Commit

Permalink
cdn-sync go succeeds in uploading missing files to AWS S3
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Aug 8, 2013
1 parent 1d31f10 commit fe6dbf2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ function go() {
}).then(function (files) {
localFiles = files;
cli.info(localFiles.length + ' file(s) found here');
async.eachLimit(config.targets, 1, eachTarget);
async.eachLimit(config.targets, 1, eachTarget, function () {
cli.ok('all done!');
});
}).done();
}

Expand Down
56 changes: 54 additions & 2 deletions lib/cdn/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ CDN.prototype.executeBulkDelete = function (actions) {
return dfrd.promise;
};

CDN.prototype.executeUpload = function (action) {
var dfrd, options, self;
self = this;
dfrd = Q.defer();
options = {
Bucket: this.cfg.Bucket,
//ContentEncoding: '',
ContentType: action.file.headers['Content-Type'],
Key: action.file.path
};

action.file.getBuffer().then(function (buffer) {
options.Body = buffer;
self.api.putObject(options, function (err, res) {
if (err) {
dfrd.reject(err);
} else {
self.progress += currentBatch.length;
self.emit('progress', self.progress, self.totalActions);
dfrd.resolve();
}
});
});

return dfrd.promise;
};

CDN.prototype.executeAction = function () { // (action)
var dfrd, self;
self = this;
Expand All @@ -93,7 +120,7 @@ CDN.prototype.executeAction = function () { // (action)
};

CDN.prototype.executeActions = function (actions) {
var self, dfrd, deleteActions, otherActions;
var self, dfrd, deleteActions, uploadActions, otherActions;
self = this;
dfrd = Q.defer();

Expand Down Expand Up @@ -122,6 +149,31 @@ CDN.prototype.executeActions = function (actions) {
}
// only doHeader and doUpload actions left now

uploadActions = actions.filter(function (action) {
return action.doUpload && action.path;
});

otherActions = actions.filter(function (action) {
return !action.doUpload && action.path;
});

if (uploadActions.length) {
async.eachLimit(uploadActions, 5, function (action, done) { // perItem
self.executeUpload(action).done(done, done);
}, function (err) { // onComplete
if (err) {
dfrd.reject(err);
} else {
self.executeActions(otherActions).done(function () {
dfrd.resolve();
});
}
});
return dfrd.promise;
}

// only doHeader actions left now

async.eachLimit(actions, 5, function (action, done) { // perItem
self.executeAction(action).done(done, done);
}, function (err) { // onComplete
Expand Down Expand Up @@ -210,7 +262,7 @@ CDN.prototype.listAllFiles = function (options) {
if (obj.Key[obj.Key.length - 1] !== '/') {
file = new File({
path: obj.Key,
md5: obj.ETag,
md5: obj.ETag.replace(/"/g, ''), // AWS wraps in quotes, urgh
size: obj.Size
});
files.push(file);
Expand Down
23 changes: 23 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ File.prototype.setMIME = function (mime) {
} else {
this.mime = mime;
}
this.headers['Content-Type'] = this.mime;
};

File.prototype.detectMIME = function () {
Expand Down Expand Up @@ -156,6 +157,28 @@ File.prototype.calculateMD5 = function () {
return dfrd.promise;
};

File.prototype.getBuffer = function () {
var buffer, dfrd, self;
self = this;
dfrd = Q.defer();

try {
fs.readFile(this.localPath, function (err, data) {
if (err) {
dfrd.reject(err);
} else if (data.length !== self.size) {
dfrd.reject(new Error('file size mismatch, re-run'));
} else {
dfrd.resolve(data);
}
});
} catch (err) {
dfrd.reject(err);
}

return dfrd.promise;
};

// exports

module.exports = File;

0 comments on commit fe6dbf2

Please sign in to comment.