Skip to content

Commit

Permalink
fixes #78 : Add UnitTests for modules
Browse files Browse the repository at this point in the history
Refactoring S3Storage module in preparation of unit test creation
  • Loading branch information
remie committed Sep 20, 2015
1 parent a7cafd0 commit a226f44
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions lib/s3storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = function(options) {
// ------------------------------------------------------------------------------------------ Module definition

function S3Storage(options) {
options = options || {};
this.options = options;

if(_.isObject(this.options)) {
Expand All @@ -36,7 +35,6 @@ function S3Storage(options) {
}
}


S3Storage.prototype.upload = function(file, context, next) {
try {
var s3obj = this.s3obj;
Expand Down Expand Up @@ -83,14 +81,15 @@ S3Storage.prototype.archive = function(token, res, next) {
next(err);
} else {
var bundle = JSON.parse(data.Body);
if(bundle) {
if(bundle.files) {
res.setHeader('Content-disposition', 'attachment; filename="bundle.zip"');
res.setHeader('Content-type', 'application/octet-stream');
var archive = archiver('zip');

var completed = _.after(bundle.files.length, function() {
archive.pipe(res);
archive.finalize();
next();
});

_.each(bundle.files, function(file, key) {
Expand All @@ -101,12 +100,12 @@ S3Storage.prototype.archive = function(token, res, next) {
completed();
});
} else {
throw 'invalid bundle identifier';
throw 'Invalid bundle data';
}
}
});
} else {
throw 'invalid token exception'
throw 'Bundle identifier unknown';
}
} catch(err) {
next(err);
Expand All @@ -115,44 +114,66 @@ S3Storage.prototype.archive = function(token, res, next) {

S3Storage.prototype.download = function(token, res, next) {
try {
var s3obj = this.s3obj;
s3obj.getObject({
Key: token
}, function(err, data) {
if(err) {
throw err;
} else {
var context = JSON.parse(data.Metadata.json || {});
res.setHeader('Content-disposition', 'attachment; filename="' + context.name + '"');
res.setHeader('Content-type', 'application/octet-stream');
res.setHeader('Content-length', context.size);
zlib.gunzip(data.Body, function(err, unzipped) {
res.send(unzipped);
});
}
});
if(token) {
var s3obj = this.s3obj;
s3obj.getObject({
Key: token
}, function(err, data) {
if(err) {
throw err;
} else {
var context = JSON.parse(data.Metadata.json);
res.setHeader('Content-disposition', 'attachment; filename="' + context.name + '"');
res.setHeader('Content-type', 'application/octet-stream');
res.setHeader('Content-length', context.size);
zlib.gunzip(data.Body, function(err, unzipped) {
res.send(unzipped);
next();
});
}
});
} else {
throw 'invalid token exception';
}
} catch(err) {
next(err);
}
},

S3Storage.prototype.purge = function() {
S3Storage.prototype.purge = function(next) {
try {
var s3obj = this.s3obj;
var filesToDelete = [];
s3obj.listObjects(function(err, data) {

var completed = _.after(data.Contents.length, function() {
next(null, filesToDelete);
});

_.each(data.Contents, function(item, key) {
s3obj.headObject({
Key: item.Key
}, function(err, data) {
var context = JSON.parse(data.Metadata.json);
var expires = new Date(context.expires);
if(Date.compare(expires, new Date()) < 0) {
s3obj.deleteObject({
Key: item.Key
});
};
if(!err) {
var context = JSON.parse(data.Metadata.json);
var expires = new Date(context.expires);
if(Date.compare(expires, new Date()) < 0) {
s3obj.deleteObject({
Key: item.Key
}, function(err) {
filesToDelete.push(context.name);
completed();
});
} else {
completed();
}
} else {
completed();
}
});
});
});
} catch(err) { }
} catch(err) {
next(err, filesToDelete);
}
};

0 comments on commit a226f44

Please sign in to comment.