-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimecheck.ts
96 lines (86 loc) · 2.46 KB
/
mimecheck.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
export interface FileResult {
name: string,
type: string,
binaryType: BinaryType,
isAllowed: boolean,
hex: string
}
export enum BinaryType {
PNG = "image/png",
JPEG = "image/jpeg",
GIF = "image/gif",
PDF = "application/pdf",
ZIP = "application/zip",
OTHER = "unknown"
}
export function getBinaryType(signature: string): BinaryType {
switch (signature) {
case '89504E47':
return BinaryType.PNG
case '47494638':
return BinaryType.GIF
case '25504446':
return BinaryType.PDF
case 'FFD8FFDB':
case 'FFD8FFE0':
case 'FFD8FFE1':
return BinaryType.JPEG
case '504B0304':
return BinaryType.ZIP
default:
return BinaryType.OTHER
}
}
function isFileResult(arg: BinaryType | FileResult): arg is FileResult {
return (<FileResult>arg).binaryType !== undefined;
}
export function isAllowedType(arg: BinaryType | FileResult) {
const type = isFileResult(arg) ? arg.binaryType : <BinaryType>arg;
return type == BinaryType.JPEG || type == BinaryType.PNG || type == BinaryType.GIF;
}
export function getFileType(file: File, callback: (file: FileResult) => void) {
const fileReader = new FileReader();
fileReader.onloadend = evt => {
if (evt.target.readyState === FileReader.DONE) {
const uint = new Uint8Array(evt.target.result);
let bytes = [];
uint.forEach(byte => {
bytes.push(byte.toString(16));
})
const hex = bytes.join('').toUpperCase();
const binaryType = getBinaryType(hex);
callback({
name: file.name,
type: file.type,
binaryType: binaryType,
isAllowed: isAllowedType(binaryType),
hex: hex
})
}
}
const blob = file.slice(0, 4);
fileReader.readAsArrayBuffer(blob);
}
export function getFileTypes(
files: File[],
next?: (file: FileResult) => void,
success?: (file: FileResult[]) => void
) {
const results = []
for (let file of files) {
getFileType(file, result => {
if (next) next(result)
results.push(result);
if (success && results.length == files.length) {
success(results);
}
})
}
}
export default {
BinaryType,
getFileType,
getFileTypes,
getBinaryType,
isAllowedType
}