From b6965be995bc3115565429be254e07bf93d2e0de Mon Sep 17 00:00:00 2001 From: Alan Date: Thu, 15 Jun 2023 16:11:50 -0700 Subject: [PATCH] fix(2889): allow to get mime type directly by adding raw flag (#140) --- helpers/mime.js | 13 +++++++++---- plugins/builds.js | 10 ++++++++++ test/plugins/builds.test.js | 6 +++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/helpers/mime.js b/helpers/mime.js index 4ba956c..0a61fd5 100644 --- a/helpers/mime.js +++ b/helpers/mime.js @@ -32,11 +32,16 @@ const KNOWN_FILE_EXTS_IN_TEXT_FORMAT = [ /** * Get MIME types from file name and file extension * @method getMimeFromFileName - * @param {String} fileExtension File extension (e.g. css, txt, html) - * @param {String} fileName File name (e.g. dockerfile, main) - * @return {String} MIME Type eg. text/html, text/plain + * @param {String} fileExtension File extension (e.g. css, txt, html) + * @param {String} fileName File name (e.g. dockerfile, main) + * @param {Boolean} raw raw data type (e.g. application/javascript instead of text/javascript) + * @return {String} MIME Type eg. text/html, text/plain */ -function getMimeFromFileName(fileExtension, fileName = '') { +function getMimeFromFileName(fileExtension, fileName = '', raw = false) { + if (raw === true) { + return mime.lookup(fileExtension) || ''; + } + if (fileName.toLowerCase().endsWith('file')) { return 'text/plain'; } diff --git a/plugins/builds.js b/plugins/builds.js index e735645..684cda0 100644 --- a/plugins/builds.js +++ b/plugins/builds.js @@ -101,6 +101,16 @@ exports.plugin = { response.headers['content-type'] = mime; + if (!displayableMimes.includes(mime)) { + response.headers['content-disposition'] = `inline; filename="${encodeURI(fileName)}"`; + } + } else { + const fileExt = fileName.split('.').pop(); + const raw = true; + const mime = getMimeFromFileName(fileExt, fileName, raw); + + response.headers['content-type'] = mime; + if (!displayableMimes.includes(mime)) { response.headers['content-disposition'] = `inline; filename="${encodeURI(fileName)}"`; } diff --git a/test/plugins/builds.test.js b/test/plugins/builds.test.js index 918806e..d5eae55 100644 --- a/test/plugins/builds.test.js +++ b/test/plugins/builds.test.js @@ -308,7 +308,7 @@ describe('builds plugin test', () => { assert.equal(getResponse.statusCode, 200); assert.equal(getResponse.headers['x-foo'], 'bar'); - assert.equal(getResponse.headers['content-type'], 'application/x-ndjson'); + assert.equal(getResponse.headers['content-type'], 'application/octet-stream'); assert.isNotOk(getResponse.headers.ignore); assert.equal(getResponse.result, 'THIS IS A TEST'); @@ -330,7 +330,7 @@ describe('builds plugin test', () => { it('saves an artifact of Japanese filename', async () => { options.url = `/builds/${mockBuildID}/日本語.txt`; - options.headers['content-type'] = 'application/x-ndjson'; + options.headers['content-type'] = 'text/plain; charset=utf-8'; const putResponse = await server.inject(options); assert.equal(putResponse.statusCode, 202); @@ -348,7 +348,7 @@ describe('builds plugin test', () => { assert.equal(getResponse.statusCode, 200); assert.equal(getResponse.headers['x-foo'], 'bar'); - assert.equal(getResponse.headers['content-type'], 'application/x-ndjson'); + assert.equal(getResponse.headers['content-type'], 'text/plain; charset=utf-8'); assert.isNotOk(getResponse.headers.ignore); assert.equal(getResponse.result, 'THIS IS A TEST');