<commit_before>/* global JSZip */
/* global _ */
'use strict';

angular.module('metadatamanagementApp').service('ZipWriterService',
function(FileReaderService, $q) {
  var rootFolderNames;
  var allFiles;
  var createFilesObject = function(file) {
      return FileReaderService.readAsArrayBuffer(file).then(function(result) {
        var pathAsArray;
        if (file.path) {
          pathAsArray = _.split(file.path, '/');
        } else {
          if (file.webkitRelativePath) {
            pathAsArray = _.split(file.webkitRelativePath, '/');
          }
        }
        if (!pathAsArray) {
          pathAsArray = [file.name];
        }
        if (_.last(rootFolderNames) !== pathAsArray[0]) {
          rootFolderNames.push(pathAsArray[0]);
        }
        allFiles.push({'result': result, 'pathAsArray': pathAsArray});
      });
    };
  this.createZipFileAsync = function(files, rootFolderShouldBeRemoved) {
    var deferred = $q.defer();
    var zip = new JSZip();
    rootFolderNames = [];
    allFiles = [];
    var chainedFilesReader = $q.when();

    files.forEach(function(file) {
      chainedFilesReader = chainedFilesReader.then(
        function() {
          return createFilesObject(file);
        }
      );
    });
    chainedFilesReader.finally(function() {
      var pathPosition = 0;
      if (rootFolderShouldBeRemoved) {
        if (rootFolderNames.length === 1) {
          pathPosition = 1;
        }
      }
      allFiles.forEach(function(file) {
        if (rootFolderShouldBeRemoved && rootFolderNames.length === 1) {
          file.pathAsArray.shift();
        }
        var pathToFile = '';
        if (file.pathAsArray.length === 1) {
          zip.file(file.pathAsArray[0], file.result);
        } else {
          var lastPath = _.last(file.pathAsArray);
          file.pathAsArray.pop();
          file.pathAsArray.forEach(function(pathName) {
            pathToFile = pathToFile + pathName + '/';
          });
          zip.folder(pathToFile).file(lastPath, file.result);
        }
      });
      var blob = zip.generate({type: 'blob'});
      if (rootFolderNames && rootFolderNames.length === 1) {
        blob.name =  rootFolderNames[0] + '.zip';
      }
      deferred.resolve(blob);
    });
    return deferred.promise;
  };
});<commit_msg>#1685 fixing JSzip upgrade issue<commit_after>/* global JSZip */
/* global _ */
'use strict';

angular.module('metadatamanagementApp').service('ZipWriterService',
function(FileReaderService, $q) {
  var rootFolderNames;
  var allFiles;
  var createFilesObject = function(file) {
      return FileReaderService.readAsArrayBuffer(file).then(function(result) {
        var pathAsArray;
        if (file.path) {
          pathAsArray = _.split(file.path, '/');
        } else {
          if (file.webkitRelativePath) {
            pathAsArray = _.split(file.webkitRelativePath, '/');
          }
        }
        if (!pathAsArray) {
          pathAsArray = [file.name];
        }
        if (_.last(rootFolderNames) !== pathAsArray[0]) {
          rootFolderNames.push(pathAsArray[0]);
        }
        allFiles.push({'result': result, 'pathAsArray': pathAsArray});
      });
    };
  this.createZipFileAsync = function(files, rootFolderShouldBeRemoved) {
    var deferred = $q.defer();
    var zip = new JSZip();
    rootFolderNames = [];
    allFiles = [];
    var chainedFilesReader = $q.when();

    files.forEach(function(file) {
      chainedFilesReader = chainedFilesReader.then(
        function() {
          return createFilesObject(file);
        }
      );
    });
    chainedFilesReader.finally(function() {
      var pathPosition = 0;
      if (rootFolderShouldBeRemoved) {
        if (rootFolderNames.length === 1) {
          pathPosition = 1;
        }
      }
      allFiles.forEach(function(file) {
        if (rootFolderShouldBeRemoved && rootFolderNames.length === 1) {
          file.pathAsArray.shift();
        }
        var pathToFile = '';
        if (file.pathAsArray.length === 1) {
          zip.file(file.pathAsArray[0], file.result);
        } else {
          var lastPath = _.last(file.pathAsArray);
          file.pathAsArray.pop();
          file.pathAsArray.forEach(function(pathName) {
            pathToFile = pathToFile + pathName + '/';
          });
          zip.folder(pathToFile).file(lastPath, file.result);
        }
      });
      zip.generateAsync({type: 'blob'}).then(function(blob) {
        if (rootFolderNames && rootFolderNames.length === 1) {
          blob.name =  rootFolderNames[0] + '.zip';
        }
        deferred.resolve(blob);
      }).catch(function(error) {
        console.log('Error while writing zip file:' + error);
        deferred.reject(error);
      });
    });
    return deferred.promise;
  };
});
{'fraction_non_alphanumeric': 0.13934942287513116, 'fraction_numerical': 0.005036726128016789, 'mean_word_length': 2.277854195323246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7060275', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1409, 'n_words': 232}