Skip to content

Commit

Permalink
fix(2889): allow to get mime type directly by adding raw flag (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
adong authored Jun 15, 2023
1 parent f44cb72 commit b6965be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 9 additions & 4 deletions helpers/mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
10 changes: 10 additions & 0 deletions plugins/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"`;
}
Expand Down
6 changes: 3 additions & 3 deletions test/plugins/builds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand All @@ -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');

Expand Down

0 comments on commit b6965be

Please sign in to comment.