Skip to content

Commit

Permalink
Improve logic and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Jul 11, 2024
1 parent cda9d26 commit 649999a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
89 changes: 89 additions & 0 deletions apps/meteor/app/utils/lib/mimeTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect } from 'chai';

import { getExtension, getMimeType } from './mimeTypes';

const mimeTypeToExtension = {
'text/plain': 'txt',
'image/x-icon': 'ico',
'image/vnd.microsoft.icon': 'ico',
'image/png': 'png',
'image/jpeg': 'jpeg',
'image/gif': 'gif',
'image/webp': 'webp',
'image/svg+xml': 'svg',
'image/bmp': 'bmp',
'image/tiff': 'tif',
'audio/wav': 'wav',
'audio/wave': 'wav',
'audio/aac': 'aac',
'audio/x-aac': 'aac',
'audio/mp4': 'm4a',
'audio/mpeg': 'mpga',
'audio/ogg': 'oga',
'application/octet-stream': 'bin',
};

const extensionToMimeType = {
lst: 'text/plain',
txt: 'text/plain',
ico: 'image/x-icon',
png: 'image/png',
jpeg: 'image/jpeg',
gif: 'image/gif',
webp: 'image/webp',
svg: 'image/svg+xml',
bmp: 'image/bmp',
tiff: 'image/tiff',
tif: 'image/tiff',
wav: 'audio/wav',
aac: 'audio/aac',
mp3: 'audio/mpeg',
ogg: 'audio/ogg',
oga: 'audio/ogg',
m4a: 'audio/mp4',
mpga: 'audio/mpeg',
mp4: 'video/mp4',
bin: 'application/octet-stream',
};

describe('mimeTypes', () => {
describe('getExtension', () => {
for (const [mimeType, extension] of Object.entries(mimeTypeToExtension)) {
it(`should return the correct extension ${extension} for the given mimeType ${mimeType}`, async () => {
expect(getExtension(mimeType)).to.be.eql(extension);
});
}

it('should return an empty string if the mimeType is not found', async () => {
expect(getExtension('application/unknown')).to.be.eql('');
});
});

describe('getMimeType', () => {
for (const [extension, mimeType] of Object.entries(extensionToMimeType)) {
it(`should return the correct mimeType ${mimeType} for the given fileName file.${extension} passing the correct mimeType`, async () => {
expect(getMimeType(mimeType, `file.${extension}`)).to.be.eql(mimeType);
});
}

it('should return the correct mimeType for the given fileName', async () => {
for (const [extension, mimeType] of Object.entries(extensionToMimeType)) {
expect(getMimeType('application/unknown', `file.${extension}`)).to.be.eql(mimeType);
}
});

it('should return the correct mimeType for the given fileName when informed mimeType is application/octet-stream', async () => {
for (const [extension, mimeType] of Object.entries(extensionToMimeType)) {
expect(getMimeType('application/octet-stream', `file.${extension}`)).to.be.eql(mimeType);
}
});

it('should return the mimeType if it is not application/octet-stream', async () => {
expect(getMimeType('audio/wav', 'file.wav')).to.be.eql('audio/wav');
});

it('should return application/octet-stream if the mimeType is not found', async () => {
expect(getMimeType('application/octet-stream', 'file.unknown')).to.be.eql('application/octet-stream');
});
});
});
9 changes: 6 additions & 3 deletions apps/meteor/app/utils/lib/mimeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mime from 'mime-type/with-db';
mime.types.wav = 'audio/wav';
mime.types.lst = 'text/plain';
mime.define('image/vnd.microsoft.icon', { source: '', extensions: ['ico'] }, mime.dupAppend);
mime.define('image/x-icon', { source: '', extensions: ['ico'] }, mime.dupAppend);
mime.types.ico = 'image/x-icon';
mime.define('image/x-icon', { source: '', extensions: ['ico'] }, mime.dupOverwrite);
mime.define('audio/aac', { source: '', extensions: ['aac'] }, mime.dupOverwrite);

const getExtension = (param: string): string => {
const extension = mime.extension(param);
Expand All @@ -13,7 +13,10 @@ const getExtension = (param: string): string => {
};

const getMimeType = (mimetype: string, fileName: string): string => {
if (mimetype && mimetype !== 'application/octet-stream') {
// If the extension from the mimetype is different from the file extension, the file
// extension may be wrong so use the informed mimetype
const extension = mime.extension(mimetype);
if (mimetype !== 'application/octet-stream' && extension && extension !== fileName.split('.').pop()) {
return mimetype;
}

Expand Down
1 change: 1 addition & 0 deletions apps/meteor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const config: Config = {
'<rootDir>/app/livechat/server/api/**/*.spec.ts',
'<rootDir>/ee/app/authorization/server/validateUserRoles.spec.ts',
'<rootDir>/app/cloud/server/functions/supportedVersionsToken/**.spec.ts',
'<rootDir>/app/utils/lib/**.spec.ts',
],
transformIgnorePatterns: ['!/node_modules/jose'],
errorOnDeprecated: true,
Expand Down

0 comments on commit 649999a

Please sign in to comment.