<commit_before>// @flow

import type {
  MediaType,
  Dimensions,
  MediaMissionStep,
  MediaMissionFailure,
} from 'lib/types/media-types';

import { getMessageForException } from 'lib/utils/errors';

import { probeFile } from './blob-utils';
import { getOrientation } from './image-utils';

type PreloadImageSuccess = {|
  success: true,
  image: Image,
|};
async function preloadImage(
  uri: string,
): Promise<{|
  steps: $ReadOnlyArray<MediaMissionStep>,
  result: MediaMissionFailure | PreloadImageSuccess,
|}> {
  let image, exceptionMessage;
  const start = Date.now();
  try {
    image = await new Promise((resolve, reject) => {
      const img = new Image();
      img.src = uri;
      img.onload = () => {
        resolve(img);
      };
      img.onerror = reject;
    });
  } catch (e) {
    exceptionMessage = getMessageForException(e);
  }
  const result = image
    ? { success: true, image }
    : { success: false, reason: 'preload_image_failed' };
  const dimensions = image
    ? { height: image.height, width: image.width }
    : null;
  const step = {
    step: 'preload_image',
    success: !!image,
    exceptionMessage,
    time: Date.now() - start,
    uri,
    dimensions,
  };
  return { steps: [step], result };
}

type ProcessFileSuccess = {|<commit_msg>[web] Include error when failing to render image<commit_after>// @flow

import type {
  MediaType,
  Dimensions,
  MediaMissionStep,
  MediaMissionFailure,
} from 'lib/types/media-types';

import { getMessageForException } from 'lib/utils/errors';

import { probeFile } from './blob-utils';
import { getOrientation } from './image-utils';

type PreloadImageSuccess = {|
  success: true,
  image: Image,
|};
async function preloadImage(
  uri: string,
): Promise<{|
  steps: $ReadOnlyArray<MediaMissionStep>,
  result: MediaMissionFailure | PreloadImageSuccess,
|}> {
  let image, exceptionMessage;
  const start = Date.now();
  try {
    image = await new Promise((resolve, reject) => {
      const img = new Image();
      img.src = uri;
      img.onload = () => {
        resolve(img);
      };
      img.onerror = e => {
        reject(e);
      };
    });
  } catch (e) {
    exceptionMessage = getMessageForException(e);
  }
  const result = image
    ? { success: true, image }
    : { success: false, reason: 'preload_image_failed' };
  const dimensions = image
    ? { height: image.height, width: image.width }
    : null;
  const step = {
    step: 'preload_image',
    success: !!image,
    exceptionMessage,
    time: Date.now() - start,
    uri,
    dimensions,
  };
  return { steps: [step], result };
}

type ProcessFileSuccess = {|
{'fraction_non_alphanumeric': 0.13943768996960487, 'fraction_numerical': 0.0, 'mean_word_length': 3.101246105919003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4299810', 'n_tokens_mistral': 839, 'n_tokens_neox': 835, 'n_words': 210}