<commit_before>'use strict';

angular.module('copayApp.services')
  .factory('fileStorageService', function(lodash, $log) {
    var root = {},
      fs;


    root.init = function(cb) {
      if (fs) return cb(null, fs);

      function onFileSystemSuccess(fileSystem) {
        console.log('File system started: ', fileSystem.name, fileSystem.root.name);
        fs = fileSystem;
        return cb(null, fs);
      }

      function fail(evt) {
        var msg = 'Could not init file system: ' + evt.target.error.code;
        console.log(msg);
        return cb(msg);
      };

      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
    };


    root.get = function(k, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          $log.debug(".get: Got main dir:", dir.nativeURL);
          dir.getFile(k, {
            create: false,
          }, function(fileEntry) {
            if (!fileEntry) return cb();
            fileEntry.file(function(file) {
              var reader = new FileReader();

              reader.onloadend = function(e) {
                if (this.result)
                  $log.debug("Read: ", this.result);
                return cb(null, this.result)
              }

              reader.readAsText(file);
            });
          }, function(err) {
            // Not found
            if (err.code == 1) return cb();
            else return cb(err);
          });
        });
      })
    };

    root.set = function(k, v, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          $log.debug(".set: Got main dir:", dir.nativeURL);
          dir.getFile(k, {
            create: true,
          }, function(fileEntry) {
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.createWriter(function(fileWriter) {

              fileWriter.onwriteend = function(e) {
                console.log('Write completed.');
                return cb();
              };

              fileWriter.onerror = function(e) {
                console.log('Write failed: ' + e.toString());
                return cb('Fail to write:', e.toString());
              };

              if (lodash.isObject(v))
                v = JSON.stringify(v);

              $log.debug('Writing:', k, v);
              fileWriter.write(v);

            }, cb);
          });
        });
      });
    };


    // See https://github.com/apache/cordova-plugin-file/#where-to-store-files
    root.getDir = function(cb) {
      if (!cordova.file) {
        return cb('Could not write on device storage');
      }

      var url = cordova.file.dataDirectory;
      // This could be needed for windows
      // if (cordova.file === undefined) {
      //   url = 'ms-appdata:///local/';
      window.resolveLocalFileSystemURL(url, function(dir) {
        return cb(null, dir);
      });
    };

    root.remove = function(k, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          dir.getFile(k, {
            create: false,
          }, function(fileEntry) {
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.remove(function() {
              console.log('File removed.');<commit_msg>add logs<commit_after>'use strict';

angular.module('copayApp.services')
  .factory('fileStorageService', function(lodash, $log) {
    var root = {},
      fs;


    root.init = function(cb) {
      if (fs) return cb(null, fs);

      function onFileSystemSuccess(fileSystem) {
        console.log('File system started: ', fileSystem.name, fileSystem.root.name);
        fs = fileSystem;
        return cb(null, fs);
      }

      function fail(evt) {
        var msg = 'Could not init file system: ' + evt.target.error.code;
        console.log(msg);
        return cb(msg);
      };

      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
    };


    root.get = function(k, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          $log.debug(".get: Got main dir:", dir.nativeURL);
          dir.getFile(k, {
            create: false,
          }, function(fileEntry) {
            if (!fileEntry) return cb();
            fileEntry.file(function(file) {
              var reader = new FileReader();

              reader.onloadend = function(e) {
                if (this.result)
                  $log.debug("Read: ", this.result);
                return cb(null, this.result)
              }

              reader.readAsText(file);
            });
          }, function(err) {
            // Not found
            if (err.code == 1) return cb();
            else return cb(err);
          });
        });
      })
    };

    root.set = function(k, v, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          $log.debug(".set: Got main dir:", dir.nativeURL);
          dir.getFile(k, {
            create: true,
          }, function(fileEntry) {
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.createWriter(function(fileWriter) {

              fileWriter.onwriteend = function(e) {
                console.log('Write completed.');
                return cb();
              };

              fileWriter.onerror = function(e) {
                console.log('Write failed: ' + e.toString());
                return cb('Fail to write:', e.toString());
              };

              if (lodash.isObject(v))
                v = JSON.stringify(v);

              $log.debug('Writing:', k, v);
              fileWriter.write(v);

            }, cb);
          });
        });
      });
    };


    // See https://github.com/apache/cordova-plugin-file/#where-to-store-files
    root.getDir = function(cb) {
      if (!cordova.file) {
        return cb('Could not write on device storage');
      }

      var url = cordova.file.dataDirectory;
      // This could be needed for windows
      // if (cordova.file === undefined) {
      //   url = 'ms-appdata:///local/';
      window.resolveLocalFileSystemURL(url, function(dir) {
        return cb(null, dir);
      }, function(err) {
        $log.warn(err);
        return cb(err || 'Could not resolve filesystem:' +  url);
      });
    };

    root.remove = function(k, cb) {
      root.init(function(err, fs) {
        if (err) return cb(err);
        root.getDir(function(err, dir) {
          if (err) return cb(err);
          dir.getFile(k, {
            create: false,
          }, function(fileEntry) {
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.remove(function() {
              console.log('File removed.');
{'fraction_non_alphanumeric': 0.14110861001688238, 'fraction_numerical': 0.0005627462014631402, 'mean_word_length': 1.6536020903322135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5605112', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2033, 'n_words': 449}