Skip to content

Commit

Permalink
feat!: automatically detect contentType if not provided (#1190)
Browse files Browse the repository at this point in the history
* Automatically change contentType to auto if none provided.

* add tests

Co-authored-by: Stephen <stephenplusplus@users.noreply.github.com>
Co-authored-by: Jonathan Lui <jonathanlui@google.com>
  • Loading branch information
3 people committed May 13, 2020
1 parent 5a4044a commit b31ba4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 44 deletions.
6 changes: 0 additions & 6 deletions src/bucket.ts
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/file.ts
Expand Up @@ -1694,9 +1694,15 @@ class File extends ServiceObject<File> {

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;
}
}

Expand Down
36 changes: 0 additions & 36 deletions test/bucket.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -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) => {
Expand Down
23 changes: 23 additions & 0 deletions test/file.ts
Expand Up @@ -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
Expand Down

0 comments on commit b31ba4a

Please sign in to comment.