Special case mime type for ts file. (#2902)
This commit is contained in:
parent
383306e17e
commit
82afc75350
|
@ -196,6 +196,11 @@ describe('fileUtils', () => {
|
||||||
vi.restoreAllMocks(); // Restore spies on actualNodeFs
|
vi.restoreAllMocks(); // Restore spies on actualNodeFs
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should detect typescript type by extension (ts)', () => {
|
||||||
|
expect(detectFileType('file.ts')).toBe('text');
|
||||||
|
expect(detectFileType('file.test.ts')).toBe('text');
|
||||||
|
});
|
||||||
|
|
||||||
it('should detect image type by extension (png)', () => {
|
it('should detect image type by extension (png)', () => {
|
||||||
mockMimeLookup.mockReturnValueOnce('image/png');
|
mockMimeLookup.mockReturnValueOnce('image/png');
|
||||||
expect(detectFileType('file.png')).toBe('image');
|
expect(detectFileType('file.png')).toBe('image');
|
||||||
|
|
|
@ -100,8 +100,14 @@ export function detectFileType(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
): 'text' | 'image' | 'pdf' | 'audio' | 'video' | 'binary' {
|
): 'text' | 'image' | 'pdf' | 'audio' | 'video' | 'binary' {
|
||||||
const ext = path.extname(filePath).toLowerCase();
|
const ext = path.extname(filePath).toLowerCase();
|
||||||
const lookedUpMimeType = mime.lookup(filePath); // Returns false if not found, or the mime type string
|
|
||||||
|
|
||||||
|
// The mimetype for "ts" is MPEG transport stream (a video format) but we want
|
||||||
|
// to assume these are typescript files instead.
|
||||||
|
if (ext === '.ts') {
|
||||||
|
return 'text';
|
||||||
|
}
|
||||||
|
|
||||||
|
const lookedUpMimeType = mime.lookup(filePath); // Returns false if not found, or the mime type string
|
||||||
if (lookedUpMimeType) {
|
if (lookedUpMimeType) {
|
||||||
if (lookedUpMimeType.startsWith('image/')) {
|
if (lookedUpMimeType.startsWith('image/')) {
|
||||||
return 'image';
|
return 'image';
|
||||||
|
|
Loading…
Reference in New Issue