react-native-file-type
react-native-file-type copied to clipboard
solution to get files from android using rn-fetch-blob
Here's the code in case someone wants to read files that start with content:// on Android:
import RNFS from "react-native-fs";
import { Base64 } from "js-base64";
import fileType from "file-type";
import RNFetchBlob from "rn-fetch-blob";
import { Platform } from "react-native";
function min(a, b) {
return a < b ? a : b;
}
function FileType(path) {
return new Promise((resolve, reject) => {
if (Platform.OS === "ios") {
RNFS.stat(path)
.then((statResult) => {
const numberBytes = min(statResult.size, 64);
RNFS.read(path, numberBytes, 0, "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" };
}
}
resolve(type);
});
})
.catch((reason) => {
reject(reason);
});
} else {
RNFetchBlob.fs
.stat(path)
.then((file) => {
const numberBytes = min(file.size, 64);
RNFetchBlob.fs.readFile(path, "base64", numberBytes).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" };
}
}
resolve(type);
});
})
.catch((reason) => {
reject(reason);
});
}
});
}
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;