forked from prscX/react-native-file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (32 loc) · 1.03 KB
/
index.js
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
import ReactNativeBlobUtil from 'react-native-blob-util';
import { Base64 } from "js-base64";
import fileType from "file-type";
function FileType(path) {
const { fs } = ReactNativeBlobUtil;
return fs.readFile(path, "base64")
.then(fileData => {
let convertedData = CovertBase64ToArrayBuffer(fileData);
convertedData = new Uint8Array(convertedData);
let type = fileType(convertedData);
if (type === undefined || type === null) {
let decodedData = String.fromCharCode.apply(null, convertedData);
if (
decodedData.startsWith("<html>") ||
decodedData.endsWith("</html>")
) {
type = { ext: "html", mime: "text/html" };
}
}
return type;
});
}
function CovertBase64ToArrayBuffer(data) {
let UTF8Data = Base64.atob(data);
let UTF8DataLength = UTF8Data.length;
let bytes = new Uint8Array(UTF8DataLength);
for (var i = 0; i < UTF8DataLength; i++) {
bytes[i] = UTF8Data.charCodeAt(i);
}
return bytes.buffer;
}
export default FileType;