{"size": 1369, "ext": "js", "max_stars_repo_path": "src/providers/processor/fileProcessor.js", "max_stars_repo_name": "RajamArunachalam/formio.js", "max_stars_repo_stars_event_min_datetime": "2015-10-10T05:56:40.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T18:21:21.000Z", "max_issues_repo_path": "src/providers/processor/fileProcessor.js", "max_issues_repo_name": "RajamArunachalam/formio.js", "max_issues_repo_issues_event_min_datetime": "2015-10-19T21:50:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T06:07:13.000Z", "max_forks_repo_path": "src/providers/processor/fileProcessor.js", "max_forks_repo_name": "RajamArunachalam/formio.js", "max_forks_repo_forks_event_min_datetime": "2015-10-15T10:16:41.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-26T23:30:55.000Z"}
{"max_stars_count": 1399, "max_issues_count": 2136, "max_forks_count": 1006, "avg_line_length": 27.38, "max_line_length": 76, "alphanum_fraction": 0.5989773557}
import NativePromise from 'native-promise-only';

const fileProcessor = (formio, config) => (file, options) =>
  new NativePromise((resolve, reject) => {
    const xhr = new XMLHttpRequest();

    // Fire on network error.
    xhr.onerror = (err) => {
      err.networkError = true;
      reject(err);
    };

    // Fire on network abort.
    xhr.onabort = (err) => {
      err.networkError = true;
      reject(err);
    };

    // Fired when the response has made it back from the server.
    xhr.onload = () => {
      if (xhr.status >= 200 && xhr.status < 300) {
        const mimetype = xhr.getResponseHeader('Content-Type') || file.type;
        resolve(new File([xhr.response], file.name, { type: mimetype }));
      }
      else {
        reject(xhr.response || 'Unable to process file');
      }
    };

    // Set the onabort error callback.
    xhr.onabort = reject;

    xhr.open('POST', config.url);
    const token = formio.getToken();
    if (token) {
      xhr.setRequestHeader('x-jwt-token', token);
    }
    xhr.responseType = 'arraybuffer';

    const data = new FormData();
    data.append('file', file);
    data.append('processorOptions', JSON.stringify(config.options || {}));
    data.append('options', JSON.stringify(options || {}));

    // Get the request and send it to the server.
    xhr.send(data);
  });

export default fileProcessor;
