function readFile()

in src/component/utils/getTextContentFromFiles.js [50:82]


function readFile(file: File, callback: (contents: string) => void): void {
  if (!global.FileReader || (file.type && !(file.type in TEXT_TYPES))) {
    callback('');
    return;
  }

  if (file.type === '') {
    let contents = '';
    // Special-case text clippings, which have an empty type but include
    // `.textClipping` in the file name. `readAsText` results in an empty
    // string for text clippings, so we force the file name to serve
    // as the text value for the file.
    if (TEXT_CLIPPING_REGEX.test(file.name)) {
      contents = file.name.replace(TEXT_CLIPPING_REGEX, '');
    }
    callback(contents);
    return;
  }

  const reader = new FileReader();
  reader.onload = function () {
    const result = reader.result;
    invariant(
      typeof result === 'string',
      'We should be calling "FileReader.readAsText" which returns a string',
    );
    callback(result);
  };
  reader.onerror = function () {
    callback('');
  };
  reader.readAsText(file);
}