<commit_before>/*!
 * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
 */

/* eslint no-unused-vars: ["error", { "vars": "local" }] */
/* global Uint8Array, FileReader */

const {Buffer} = require('safe-buffer');
const {ExifImage} = require('exif');

/**
* Updates the image file with exif information, required to correctly rotate the image activity
* @param {Object} file
* @param {Object} options
* @param {boolean} options.shouldNotAddExifData
* @returns {Promise<Object>}
*/
export function updateImageOrientation(file, options = {}) {
  return new Promise((resolve) => {
    const reader = new FileReader();

    reader.readAsArrayBuffer(file);
    reader.onload = function onload() {
      const arrayBuffer = reader.result;
      const buf = Buffer.from(arrayBuffer);

      resolve(buf);
    };
  })
    .then((buf) => {
      if (options.shouldNotAddExifData) {
        return buf;
      }

      return readExifData(file, buf);
    });
}

/**
* Adds exif orientation information on the image file
* @param {Object} file
* @param {Object} buf
* @returns {Promise<ExifImage>}
*/
export function readExifData(file, buf) {
  return new Promise((resolve) => {
    // For avatar images the file.type is set as image/jpeg, however for images shared in an activity file.mimeType is set as image/jpeg. Handling both conditions.
    if (file && (file.type === 'image/jpeg' || file.mimeType === 'image/jpeg')) {
      /* eslint-disable no-new */
      new ExifImage({image: buf}, (error, exifData) => {
        if (!error && exifData) {
          file.orientation = exifData.image.Orientation;
          file.exifWidth = exifData.exif.ExifImageWidth;
          file.exifHeight = exifData.exif.ExifImageHeight;
          if (file.image) {
            file.image.orientation = exifData.image.Orientation;
          }
        }
        resolve(buf);
      });
    }
    else {
      resolve(buf);
    }
  });
}

/* eslint-disable complexity */
/**
* Rotates/flips the image on the canvas as per exif information
* @param {Object} options(orientation: image exif orientation range from 1-8, img: Image object, x: start x-axis, y: start y-axis, width: width of the thumbnail, height: height of the thumbnail, ctx: canvas context)
* @param {Object} file
* @returns {Object}
*/
export function orient(options, file) {
  const {
    width, height, ctx, img, orientation, x, y
  } = options;

  if (file && file.orientation && file.orientation !== 1) {
    // explanation of orientation:
    // https://stackoverflow.com/questions/20600800/js-client-side-exif-orientation-rotate-and-mirror-jpeg-images
    switch (orientation) {
      case 2:<commit_msg>refactor(helper-image): replace exif with exifr<commit_after>/*!
 * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
 */

/* eslint no-unused-vars: ["error", { "vars": "local" }] */
/* global Uint8Array, FileReader */

const {Buffer} = require('safe-buffer');
const {parse} = require('exifr/dist/lite.umd');

/**
* Updates the image file with exif information, required to correctly rotate the image activity
* @param {Object} file
* @param {Object} options
* @param {boolean} options.shouldNotAddExifData
* @returns {Promise<Object>}
*/
export function updateImageOrientation(file, options = {}) {
  return new Promise((resolve) => {
    const reader = new FileReader();

    reader.readAsArrayBuffer(file);
    reader.onload = function onload() {
      const arrayBuffer = reader.result;
      const buf = Buffer.from(arrayBuffer);

      resolve(buf);
    };
  })
    .then((buf) => {
      if (options.shouldNotAddExifData) {
        return buf;
      }

      return readExifData(file, buf);
    });
}

/**
* Adds exif orientation information on the image file
* @param {Object} file
* @param {Object} buf
* @returns {Promise<ExifImage>}
*/
export function readExifData(file, buf) {
  return new Promise(async (resolve) => {
    // For avatar images the file.type is set as image/jpeg, however for images shared in an activity file.mimeType is set as image/jpeg. Handling both conditions.
    if (
      file &&
      (file.type === 'image/jpeg' || file.mimeType === 'image/jpeg')
    ) {
      const {
        Orientation,
        ExifImageHeight,
        ExifImageWidth
      } = await parse(buf, {translateValues: false});

      file.orientation = Orientation;
      file.exifWidth = ExifImageWidth;
      file.exifHeight = ExifImageHeight;

      if (file.image) {
        file.image.orientation = Orientation;
      }

      resolve(buf);
    }
    else {
      resolve(buf);
    }
  });
}

/* eslint-disable complexity */
/**
* Rotates/flips the image on the canvas as per exif information
* @param {Object} options(orientation: image exif orientation range from 1-8, img: Image object, x: start x-axis, y: start y-axis, width: width of the thumbnail, height: height of the thumbnail, ctx: canvas context)
* @param {Object} file
* @returns {Object}
*/
export function orient(options, file) {
  const {
    width, height, ctx, img, orientation, x, y
  } = options;

  if (file && file.orientation && file.orientation !== 1) {
    // explanation of orientation:
    // https://stackoverflow.com/questions/20600800/js-client-side-exif-orientation-rotate-and-mirror-jpeg-images
    switch (orientation) {
      case 2:
{'fraction_non_alphanumeric': 0.1347678369195923, 'fraction_numerical': 0.007927519818799546, 'mean_word_length': 3.15282131661442, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956538', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1594, 'n_words': 460}