From b31ba4a11399b57538ddf0d6ca2e10b2aa3fbc3a Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 13 May 2020 17:41:01 -0400 Subject: [PATCH] feat!: automatically detect contentType if not provided (#1190) * Automatically change contentType to auto if none provided. * add tests Co-authored-by: Stephen Co-authored-by: Jonathan Lui --- src/bucket.ts | 6 ------ src/file.ts | 10 ++++++++-- test/bucket.ts | 36 ------------------------------------ test/file.ts | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/bucket.ts b/src/bucket.ts index 7e0ec08f0..54447bfae 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -3440,12 +3440,6 @@ class Bucket extends ServiceObject { }); } - const contentType = mime.contentType(path.basename(pathString)); - - if (contentType && !options.metadata.contentType) { - options.metadata.contentType = contentType; - } - if (options.resumable !== null && typeof options.resumable === 'boolean') { upload(); } else { diff --git a/src/file.ts b/src/file.ts index 940485b25..6ec980d96 100644 --- a/src/file.ts +++ b/src/file.ts @@ -1694,9 +1694,15 @@ class File extends ServiceObject { if (options.contentType) { options.metadata.contentType = options.contentType; + } - if (options.metadata.contentType === 'auto') { - options.metadata.contentType = mime.getType(this.name); + if ( + !options.metadata.contentType || + options.metadata.contentType === 'auto' + ) { + const detectedContentType = mime.getType(this.name); + if (detectedContentType) { + options.metadata.contentType = detectedContentType; } } diff --git a/test/bucket.ts b/test/bucket.ts index 51e653beb..78e2ee769 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -2246,10 +2246,6 @@ describe('Bucket', () => { describe('upload', () => { const basename = 'testfile.json'; const filepath = path.join(__dirname, '../../test/testdata/' + basename); - const textFilepath = path.join( - __dirname, - '../../test/testdata/textfile.txt' - ); const metadata = { metadata: { a: 'b', @@ -2360,38 +2356,6 @@ describe('Bucket', () => { }); }); - it('should guess at the content type', done => { - const fakeFile = new FakeFile(bucket, 'file-name'); - const options = {destination: fakeFile}; - fakeFile.createWriteStream = (options: CreateWriteStreamOptions) => { - const ws = new stream.Writable(); - ws.write = () => true; - setImmediate(() => { - const expectedContentType = 'application/json; charset=utf-8'; - assert.strictEqual(options.metadata.contentType, expectedContentType); - done(); - }); - return ws; - }; - bucket.upload(filepath, options, assert.ifError); - }); - - it('should guess at the charset', done => { - const fakeFile = new FakeFile(bucket, 'file-name'); - const options = {destination: fakeFile}; - fakeFile.createWriteStream = (options: CreateWriteStreamOptions) => { - const ws = new stream.Writable(); - ws.write = () => true; - setImmediate(() => { - const expectedContentType = 'text/plain; charset=utf-8'; - assert.strictEqual(options.metadata.contentType, expectedContentType); - done(); - }); - return ws; - }; - bucket.upload(textFilepath, options, assert.ifError); - }); - describe('resumable uploads', () => { beforeEach(() => { fsStatOverride = (path: string, callback: Function) => { diff --git a/test/file.ts b/test/file.ts index 22debadde..ecd796a50 100644 --- a/test/file.ts +++ b/test/file.ts @@ -1884,6 +1884,29 @@ describe('File', () => { writable.write('data'); }); + it('should detect contentType if not defined', done => { + const writable = file.createWriteStream(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + file.startResumableUpload_ = (stream: {}, options: any) => { + assert.strictEqual(options.metadata.contentType, 'image/png'); + done(); + }; + + writable.write('data'); + }); + + it('should not set a contentType if mime lookup failed', done => { + const file = new File('file-without-ext'); + const writable = file.createWriteStream(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + file.startResumableUpload_ = (stream: {}, options: any) => { + assert.strictEqual(typeof options.metadata.contentType, 'undefined'); + done(); + }; + + writable.write('data'); + }); + it('should set encoding with gzip:true', done => { const writable = file.createWriteStream({gzip: true}); // eslint-disable-next-line @typescript-eslint/no-explicit-any