Skip to content

Commit

Permalink
Merge pull request jamhall#3 from 4front/master
Browse files Browse the repository at this point in the history
Support for ContentEncoding
  • Loading branch information
jamhall committed May 24, 2015
2 parents 3dfeca5 + dae0f70 commit f2655dd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
res.header('Etag', object.md5);
res.header('Last-Modified', new Date(object.modifiedDate).toUTCString());
res.header('Content-Type', object.contentType);

if (object.contentEncoding)
res.header('Content-Encoding', object.contentEncoding);

res.header('Content-Length', object.size);
if (object.customMetaData.length > 0) {
object.customMetaData.forEach(function (metaData) {
Expand Down Expand Up @@ -213,7 +217,7 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
return buildResponse(req, res, 200, object, data);
});
},
putObject: function (req, res) {
putObject: function (req, res) {
var template;
var copy = req.headers['x-amz-copy-source'];
if (copy) {
Expand Down
6 changes: 6 additions & 0 deletions lib/file-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var FileStore = function (rootDirectory) {
key: key,
md5: json.md5,
contentType: json.contentType,
contentEncoding: json.contentEncoding,
size: json.size,
modifiedDate: json.modifiedDate,
creationDate: json.creationDate,
Expand All @@ -162,6 +163,7 @@ var FileStore = function (rootDirectory) {
var createMetaData = function (data, done) {
var contentFile = data.contentFile,
type = data.type,
encoding = data.encoding,
metaFile = data.metaFile,
headers = data.headers;
async.parallel([
Expand Down Expand Up @@ -193,6 +195,9 @@ var FileStore = function (rootDirectory) {
creationDate: results[0].ctime,
customMetaData: getCustomMetaData(headers)
};
if (encoding)
metaData.contentEncoding = encoding;

fs.writeFile(metaFile, JSON.stringify(metaData), function (err) {
if (err) {
return done(err);
Expand All @@ -217,6 +222,7 @@ var FileStore = function (rootDirectory) {
createMetaData({
contentFile: contentFile,
type: req.headers['content-type'],
encoding: req.headers['content-encoding'],
key: key,
metaFile: metaFile,
headers: req.headers
Expand Down
1 change: 1 addition & 0 deletions lib/models/s3-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var S3Object = function (s3Item) {
var item = {
key: s3Item.key,
contentType: s3Item.contentType,
contentEncoding: s3Item.contentEncoding,
md5: s3Item.md5,
size: s3Item.size,
modifiedDate: s3Item.modifiedDate,
Expand Down
5 changes: 5 additions & 0 deletions test/resources/jquery.js.gz

Large diffs are not rendered by default.

71 changes: 55 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,31 @@ describe('S3rver Tests', function () {
if (err) {
return done(err);
}
done();

// Create 5 buckets
async.eachSeries(buckets, function (bucket, callback) {
s3Client.createBucket({Bucket: bucket}, callback);
}, done);
});
});
});
});

it('should create five buckets', function (done) {
async.eachSeries(buckets, function (bucket, callback) {
s3Client.createBucket({Bucket: bucket}, function (err) {
if (err) {
return callback(err);
}
callback(null);
});
}, function (err) {
if (err) {
return done(err);
}
done();
});
});
// it('should create five buckets', function (done) {
// async.eachSeries(buckets, function (bucket, callback) {
// s3Client.createBucket({Bucket: bucket}, function (err) {
// if (err) {
// return callback(err);
// }
// callback(null);
// });
// }, function (err) {
// if (err) {
// return done(err);
// }
// done();
// });
// });

it('should fetch fetch five buckets', function (done) {
s3Client.listBuckets(function (err, buckets) {
Expand Down Expand Up @@ -199,6 +203,41 @@ describe('S3rver Tests', function () {
});
});

it('should store a gzip encoded file in bucket', function(done) {
var file = path.join(__dirname, 'resources/jquery.js.gz');
var stats = fs.statSync(file);

var params = {
Bucket: buckets[0],
Key: 'jquery',
Body: fs.createReadStream(file), // new Buffer(data),
ContentType: 'application/javascript',
ContentEncoding: 'gzip',
ContentLength: stats.size
};

s3Client.putObject(params, function (err, data) {
if (err) return done(err);
// /[a-fA-F0-9]{32}/.test(data.ETag).should.equal(true);
// if (err) {
// return done(err);
// }
// done();

s3Client.getObject({Bucket: buckets[0], Key: 'jquery'}, function (err, object) {
if (err) {
return done(err);
}
debugger;
// object.ETag.should.equal(md5(data));
object.ContentLength.should.equal(stats.size.toString());
object.ContentEncoding.should.equal('gzip');
object.ContentType.should.equal('application/javascript');
done();
});
});
});

it('should copy an image object into another bucket', function (done) {
var params = {
Bucket: buckets[3],
Expand Down

0 comments on commit f2655dd

Please sign in to comment.