<commit_before>import { remote } from 'electron';
const mediaFlashcards = remote.getGlobal('globalObj').mediaFlashcards;
const desktopDir = remote.getGlobal('globalObj').desktopDir;
import { processing } from './files';
import { push } from 'react-router-redux';

export const RESET_MEDIA = 'RESET_MEDIA';
export const UPDATE_FILTER = 'UPDATE_FILTER';
export const UPDATE_MEDIA = 'UPDATE_MEDIA';
export const REMOVE_MEDIA = 'REMOVE_MEDIA';

// ACTION CREATORS

/**
 * Creates action to set media in store
 * @param {array} media - An array of objects containing subtitle text, times, ids and media file names
 */

export function removeMedia(index) {
  return {
    type: REMOVE_MEDIA,
    index
  }
}

export function resetMedia(media) {
  return {
    type: RESET_MEDIA,
    media
  };
}

export function updateFilter(newFilter) {
  return {
    type: UPDATE_FILTER,
    newFilter
  };
}

export function updateMedia(updatedMedia) {
  return {
    type: UPDATE_MEDIA,
    updatedMedia
  };
}

// ACTION CREATOR CREATORS

export function combineSubtitles(index1, index2) {
  if (index1 === index2) {
    return;
  }

  return (dispatch, getState) => {
    const { files, subtitles } = getState();

    let target, source;
    if (index1 < index2) {
      target = subtitles[index1];
      source = subtitles[index2];
    } else {
      target = subtitles[index2];
      source = subtitles[index1];
    }

    const merged = mediaFlashcards.combineSubtitles(target, source);
    mediaFlashcards.rmFile(source.media);

    const videoFile = files.videoFile.path;
    dispatch(processing(true));

    mediaFlashcards.updateAudio(videoFile, merged)
      .then(
        updatedMedia => dispatch(updateMedia(updatedMedia))
      )
      .then(
        () => dispatch(removeMedia(source.index))
      )
      .then(
        () => dispatch(processing(false))
      );
  };

}

export function createApkg() {
  return (dispatch, getState) => {
    const { files, subtitles } = getState();
    const videoFile = files.videoFile.path;
    dispatch(processing(true));

    mediaFlashcards.createAnkiDb(videoFile, mediaToArray(subtitles))
      .then(
        dbFile => mediaFlashcards.createApkg(dbFile, mediaFlashcards.quickName(videoFile), desktopDir)
      )
      .then(
        () => mediaFlashcards.rmFiles('./pkg')
      )
      .then(
        () => dispatch(processing(false))
      )
      .then(
        () => dispatch(push('/'))
      );
  };
}

export function updateMediaTimes(newMedia) {
  return (dispatch, getState) => {
    const { files } = getState();
    const videoFile = files.videoFile.path;
    dispatch(processing(true));

    mediaFlashcards.updateAudio(videoFile, newMedia)
      .then(
        updatedMedia => dispatch(updateMedia(updatedMedia))
      )
      .then(
      )
      .then(
        () => dispatch(processing(false))
      );
  };
}

function mediaToArray(mediaObject) {
  return Object.keys(mediaObject).map(key => mediaObject[key]);
}<commit_msg>Don't detroy any audio files when updating, add random version hash to each revision when combining and updating audio<commit_after>import { remote } from 'electron';
const mediaFlashcards = remote.getGlobal('globalObj').mediaFlashcards;
const desktopDir = remote.getGlobal('globalObj').desktopDir;
import { processing } from './files';
import { push } from 'react-router-redux';

export const RESET_MEDIA = 'RESET_MEDIA';
export const UPDATE_FILTER = 'UPDATE_FILTER';
export const UPDATE_MEDIA = 'UPDATE_MEDIA';
export const REMOVE_MEDIA = 'REMOVE_MEDIA';

// ACTION CREATORS

/**
 * Creates action to set media in store
 * @param {array} media - An array of objects containing subtitle text, times, ids and media file names
 */

export function removeMedia(index) {
  return {
    type: REMOVE_MEDIA,
    index
  }
}

export function resetMedia(media) {
  return {
    type: RESET_MEDIA,
    media
  };
}

export function updateFilter(newFilter) {
  return {
    type: UPDATE_FILTER,
    newFilter
  };
}

export function updateMedia(updatedMedia) {
  return {
    type: UPDATE_MEDIA,
    updatedMedia
  };
}

// ACTION CREATOR CREATORS

export function combineSubtitles(index1, index2) {
  if (index1 === index2) {
    return;
  }

  return (dispatch, getState) => {
    const { files, subtitles } = getState();

    let target, source;
    if (index1 < index2) {
      target = subtitles[index1];
      source = subtitles[index2];
    } else {
      target = subtitles[index2];
      source = subtitles[index1];
    }

    const merged = mediaFlashcards.combineSubtitles(target, source, {replaceMedia: false});
    // mediaFlashcards.rmFile(source.media);
    console.log('combined subtitles media file ', merged.media)

    const videoFile = files.videoFile.path;
    dispatch(processing(true));

    mediaFlashcards.updateAudio(videoFile, merged)
      .then(
        updatedMedia => dispatch(updateMedia(updatedMedia))
      )
      .then(
        () => dispatch(removeMedia(source.index))
      )
      .then(
        () => dispatch(processing(false))
      );
  };

}

export function createApkg() {
  return (dispatch, getState) => {
    const { files, subtitles } = getState();
    const videoFile = files.videoFile.path;
    dispatch(processing(true));

    mediaFlashcards.createAnkiDb(videoFile, mediaToArray(subtitles))
      .then(
        dbFile => mediaFlashcards.createApkg(dbFile, mediaFlashcards.quickName(videoFile), desktopDir)
      )
      .then(
        () => mediaFlashcards.rmFiles('./pkg')
      )
      .then(
        () => dispatch(processing(false))
      )
      .then(
        () => dispatch(push('/'))
      );
  };
}

export function updateMediaTimes(newMedia) {
  return (dispatch, getState) => {
    const { files } = getState()
    const videoFile = files.videoFile.path
    const subtitleData = {
      ...newMedia, 
      media: mediaFlashcards.updateFileVersionHash(newMedia.media)
    }

    console.log('updateMediaTimes action creator')
    console.log({subtitleData})

    dispatch(processing(true))

    mediaFlashcards.updateAudio(videoFile, subtitleData)
      .then(
        updatedMedia => dispatch(updateMedia(updatedMedia))
      )
      .then(
        () => dispatch(processing(false))
      )
  }
}

function mediaToArray(mediaObject) {
  return Object.keys(mediaObject).map(key => mediaObject[key])
}
{'fraction_non_alphanumeric': 0.12217837411207577, 'fraction_numerical': 0.0031570639305445935, 'mean_word_length': 3.1932495036399735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '5152334', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1904, 'n_words': 415}