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 4056c3f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
83 changes: 83 additions & 0 deletions apps/meteor/app/utils/lib/mimeTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 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');
});
});
});
7 changes: 6 additions & 1 deletion apps/meteor/app/utils/lib/mimeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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.define('audio/aac', { source: '', extensions: ['aac'] }, mime.dupAppend);
mime.types.aac = 'audio/aac';
mime.types.ico = 'image/x-icon';

const getExtension = (param: string): string => {
Expand All @@ -13,7 +15,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 (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 4056c3f

Please sign in to comment.