Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution to get files from android using rn-fetch-blob #5

Open
efstathiosntonas opened this issue Apr 29, 2020 · 0 comments
Open

solution to get files from android using rn-fetch-blob #5

efstathiosntonas opened this issue Apr 29, 2020 · 0 comments

Comments

@efstathiosntonas
Copy link

efstathiosntonas commented Apr 29, 2020

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant