aboutsummaryrefslogtreecommitdiff
path: root/src/mimetype.ts
blob: 4110ae53d389d9e8c85d3672ef681b23802f29b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import * as fromData from 'file-type';
import { lookup as fromName } from 'mime-types';

const DEFAULT_MIMETYPE: string = 'application/octet-stream';

export const mimetype: (
	contentType?: string, data?: Buffer, name?: string
) => string = (
	contentType?: string, data?: Buffer, name?: string
): string => {
	if (contentType && contentType !== DEFAULT_MIMETYPE) {
		return contentType;
	}
	if (data) {
		const fType: fromData.FileTypeResult = fromData(data);
		if (fType) {
			return fType.mime;
		}
	}
	if (name) {
		const mType: string | false = fromName(name);
		if (mType) {
			return mType;
		}
	}
	return DEFAULT_MIMETYPE;
};

export { extension } from 'mime-types';