Skip to content

Commit

Permalink
Merge pull request #284 from stephenplusplus/spp--storage-detect-file…
Browse files Browse the repository at this point in the history
…-type

storage: detect contentType from upload()
  • Loading branch information
silvolu committed Nov 6, 2014
2 parents 7d78904 + 67fddd5 commit fa5a471
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var extend = require('extend');
var fs = require('fs');
var mime = require('mime');
var path = require('path');

/**
Expand Down Expand Up @@ -300,6 +301,17 @@ Bucket.prototype.upload = function(localPath, destination, metadata, callback) {
newFile = destination;
}
newFile = newFile || this.file(name);

var contentType = mime.lookup(localPath);
if (contentType && !metadata.contentType) {
metadata.contentType = contentType;
}

var charset = mime.charsets.lookup(metadata.contentType);
if (charset) {
metadata.contentType += '; charset=' + charset;
}

fs.createReadStream(localPath)
.pipe(newFile.createWriteStream(metadata))
.on('error', callback)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"duplexify": "^3.1.2",
"extend": "^1.3.0",
"gapitoken": "^0.1.3",
"mime": "^1.2.11",
"node-uuid": "^1.4.1",
"protobufjs": "^3.4.0",
"request": "^2.39.0",
Expand Down
41 changes: 41 additions & 0 deletions test/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ describe('Bucket', function() {
describe('upload', function() {
var basename = 'proto_query.json';
var filepath = 'test/testdata/' + basename;
var textFilepath = 'test/testdata/textfile.txt';
var metadata = { a: 'b', c: 'd' };

beforeEach(function() {
Expand Down Expand Up @@ -334,6 +335,46 @@ describe('Bucket', function() {
});
});

it('should guess at the content type', function(done) {
var fakeFile = new FakeFile(bucket, 'file-name');
fakeFile.createWriteStream = function(metadata) {
var dup = duplexify();
setImmediate(function() {
assert.equal(metadata.contentType, 'application/json');
done();
});
return dup;
};
bucket.upload(filepath, fakeFile, assert.ifError);
});

it('should guess at the charset', function(done) {
var fakeFile = new FakeFile(bucket, 'file-name');
fakeFile.createWriteStream = function(metadata) {
var dup = duplexify();
setImmediate(function() {
assert.equal(metadata.contentType, 'text/plain; charset=UTF-8');
done();
});
return dup;
};
bucket.upload(textFilepath, fakeFile, assert.ifError);
});

it('should allow overriding content type', function(done) {
var fakeFile = new FakeFile(bucket, 'file-name');
var metadata = { contentType: 'made-up-content-type' };
fakeFile.createWriteStream = function(meta) {
var dup = duplexify();
setImmediate(function() {
assert.equal(meta.contentType, metadata.contentType);
done();
});
return dup;
};
bucket.upload(filepath, fakeFile, metadata, assert.ifError);
});

it('should execute callback on error', function(done) {
var error = new Error('Error.');
var fakeFile = new FakeFile(bucket, 'file-name');
Expand Down
1 change: 1 addition & 0 deletions test/testdata/textfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file!

0 comments on commit fa5a471

Please sign in to comment.