From c61fa4890b6d07b875466454846f764fcead33b2 Mon Sep 17 00:00:00 2001 From: Austin Date: Tue, 12 May 2020 16:03:19 -0400 Subject: [PATCH 1/2] Automatically change contentType to auto if none provided. --- src/file.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/file.ts b/src/file.ts index 0a70292de..ea3a8832a 100644 --- a/src/file.ts +++ b/src/file.ts @@ -1703,7 +1703,11 @@ class File extends ServiceObject { if (options.metadata.contentType === 'auto') { options.metadata.contentType = mime.getType(this.name); + } else { + options.metadata.contentType = mime.getType(this.name); } + } else { + options.metadata.contentType = mime.getType(this.name); } let gzip = options.gzip; From 3c0a9a76fe14ea9c22ef3f503fa377b6b818b653 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 12 May 2020 19:47:43 -0400 Subject: [PATCH 2/2] add tests --- src/bucket.ts | 6 ------ src/file.ts | 14 ++++++++------ test/bucket.ts | 36 ------------------------------------ test/file.ts | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 48 deletions(-) diff --git a/src/bucket.ts b/src/bucket.ts index a7c9eec38..90753768d 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -3438,12 +3438,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 ea3a8832a..f0a5604d8 100644 --- a/src/file.ts +++ b/src/file.ts @@ -1700,14 +1700,16 @@ class File extends ServiceObject { if (options.contentType) { options.metadata.contentType = options.contentType; + } - if (options.metadata.contentType === 'auto') { - options.metadata.contentType = mime.getType(this.name); - } else { - 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; } - } else { - options.metadata.contentType = mime.getType(this.name); } let gzip = options.gzip; 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 963d58673..e4f01254d 100644 --- a/test/file.ts +++ b/test/file.ts @@ -1896,6 +1896,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