Skip to content

Commit fa5a471

Browse files
committed
Merge pull request #284 from stephenplusplus/spp--storage-detect-file-type
storage: detect contentType from upload()
2 parents 7d78904 + 67fddd5 commit fa5a471

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

lib/storage/bucket.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var extend = require('extend');
2424
var fs = require('fs');
25+
var mime = require('mime');
2526
var path = require('path');
2627

2728
/**
@@ -300,6 +301,17 @@ Bucket.prototype.upload = function(localPath, destination, metadata, callback) {
300301
newFile = destination;
301302
}
302303
newFile = newFile || this.file(name);
304+
305+
var contentType = mime.lookup(localPath);
306+
if (contentType && !metadata.contentType) {
307+
metadata.contentType = contentType;
308+
}
309+
310+
var charset = mime.charsets.lookup(metadata.contentType);
311+
if (charset) {
312+
metadata.contentType += '; charset=' + charset;
313+
}
314+
303315
fs.createReadStream(localPath)
304316
.pipe(newFile.createWriteStream(metadata))
305317
.on('error', callback)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"duplexify": "^3.1.2",
4848
"extend": "^1.3.0",
4949
"gapitoken": "^0.1.3",
50+
"mime": "^1.2.11",
5051
"node-uuid": "^1.4.1",
5152
"protobufjs": "^3.4.0",
5253
"request": "^2.39.0",

test/storage/bucket.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ describe('Bucket', function() {
263263
describe('upload', function() {
264264
var basename = 'proto_query.json';
265265
var filepath = 'test/testdata/' + basename;
266+
var textFilepath = 'test/testdata/textfile.txt';
266267
var metadata = { a: 'b', c: 'd' };
267268

268269
beforeEach(function() {
@@ -334,6 +335,46 @@ describe('Bucket', function() {
334335
});
335336
});
336337

338+
it('should guess at the content type', function(done) {
339+
var fakeFile = new FakeFile(bucket, 'file-name');
340+
fakeFile.createWriteStream = function(metadata) {
341+
var dup = duplexify();
342+
setImmediate(function() {
343+
assert.equal(metadata.contentType, 'application/json');
344+
done();
345+
});
346+
return dup;
347+
};
348+
bucket.upload(filepath, fakeFile, assert.ifError);
349+
});
350+
351+
it('should guess at the charset', function(done) {
352+
var fakeFile = new FakeFile(bucket, 'file-name');
353+
fakeFile.createWriteStream = function(metadata) {
354+
var dup = duplexify();
355+
setImmediate(function() {
356+
assert.equal(metadata.contentType, 'text/plain; charset=UTF-8');
357+
done();
358+
});
359+
return dup;
360+
};
361+
bucket.upload(textFilepath, fakeFile, assert.ifError);
362+
});
363+
364+
it('should allow overriding content type', function(done) {
365+
var fakeFile = new FakeFile(bucket, 'file-name');
366+
var metadata = { contentType: 'made-up-content-type' };
367+
fakeFile.createWriteStream = function(meta) {
368+
var dup = duplexify();
369+
setImmediate(function() {
370+
assert.equal(meta.contentType, metadata.contentType);
371+
done();
372+
});
373+
return dup;
374+
};
375+
bucket.upload(filepath, fakeFile, metadata, assert.ifError);
376+
});
377+
337378
it('should execute callback on error', function(done) {
338379
var error = new Error('Error.');
339380
var fakeFile = new FakeFile(bucket, 'file-name');

test/testdata/textfile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test file!

0 commit comments

Comments
 (0)