Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: detect contentType from upload() #284

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!