<commit_before>import Ember from 'ember';
import TextEncoding from 'npm:text-encoding';
/* global $ */

export default Ember.Component.extend({
	
	tagName: "input",
	
	attributeBindings: ['multiple', 'type'],
	classNames: ["file-reader"],
	
	multiple: false,
	type: 'file',
	
	fReader: null,
  
  	didInsertElement() {
          
      var self = this;
    
      this.set('fReader', new FileReader());
      
      this.$().on("change", function() {
        self.read(this.files);
      });
          
      this.get('fReader').onload = (e) => {
        
        if (e.target.result instanceof ArrayBuffer) {
          let encoding = this.detectCharset(e.target.result);
          let s = new TextEncoding.TextDecoder(encoding).decode(new Uint8Array(e.target.result));
          this.sendAction('onread', s);
        }
        
      };
          
      this.sendAction('onready', this);
		
  	},
	
    read: function(files) {
      this.get('fReader').readAsArrayBuffer(files[0]);
    },
    
    detectCharset(buf) {
      
      let view = new Uint8Array(buf);
      for (var i = 0; i < view.byteLength; i++) {
        let c = view[i];
        console.log(c);
        if ( (c > 127) && (c < 2048) ) {
          console.log("utf");
          return "UTF-8";
        }
        console.log("iso");
        return "iso8859-1";
      }
      
    },
    
    actions: {
        
        trigger: function() {
            this.$().click();
        }
        
    }

});<commit_msg>Méthode maison (fix #32)<commit_after>import Ember from 'ember';
import TextEncoding from 'npm:text-encoding';
/* global $ */

export default Ember.Component.extend({
	
	tagName: "input",
	
	attributeBindings: ['multiple', 'type'],
	classNames: ["file-reader"],
	
	multiple: false,
	type: 'file',
	
	fReader: null,
  
  	didInsertElement() {
          
      var self = this;
    
      this.set('fReader', new FileReader());
      
      this.$().on("change", function() {
        self.read(this.files);
      });
          
      this.get('fReader').onload = (e) => {
        
        if (e.target.result instanceof ArrayBuffer) {
          
          //on décode une première fois en iso8859-1
          let buf = this.skipBom(new Uint8Array(e.target.result)),
              s = new TextEncoding.TextDecoder("UTF-8").decode(buf);
              
          let err = this.detectCharsetProblem(s);
          
          //et on réencode si besoin
          if (err > 0) {
            s = new TextEncoding.TextDecoder("iso8859-1").decode(buf);
          }
          
          this.sendAction('onread', s);
          
        }
        
      };
          
      this.sendAction('onready', this);
		
  	},
	
    read: function(files) {
      this.get('fReader').readAsArrayBuffer(files[0]);
    },
    
    detectCharsetProblem(s) {
      
      for (var i = 0, err = 0; i < s.length; i++) {
        let c = s.charCodeAt(i);
        if ( c === 65533 ) {
          err++;
        }
      }
      
      return err;
    
    },
    
    skipBom(buf) {
      // If we have a BOM skip it
      if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
        return buf.subarray(3);
      }
      return buf;
    },
    
    actions: {
        
        trigger: function() {
            this.$().click();
        }
        
    }

});
{'fraction_non_alphanumeric': 0.15315315315315314, 'fraction_numerical': 0.014414414414414415, 'mean_word_length': 1.6290449881610103, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '5001896', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1047, 'n_words': 210}