<commit_before>
Components.utils.import("resource://gre/modules/NetUtil.jsm");  
Components.utils.import("resource://gre/modules/FileUtils.jsm"); 
/**
 * XULfhte namespace.
 */
if ("undefined" == typeof(XULFHtEChrome)) {
  var XULFHtEChrome = {};
};

/**
 * Controls the browser overlay for the Hello World extension.
 */
XULFHtEChrome.BrowserOverlay = {
  /**
   * Save current web page to epub format
   */
  doWork : function(aEvent) {

    /**
     * Ask for file name and location
     */
    var nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    fp.init(window, "Save webpage as epub", nsIFilePicker.modeSave);
    fp.appendFilter("EPUB File","*.html");

    var rv = fp.show();
    if (rv == fp.returnCancel) return;

    if(rv == nsIFilePicker.returnOK){
      epub = fp.file;
      var epubPath = epub.path;

    /**
     * Save html page to working directory
     */
      var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
      var tmp_D = dirService.get("TmpD", Components.interfaces.nsIFile);
      //TODO: concatenate FHtE with a random number
      tmp_D.append("FHtE");
      if( !tmp_D.exists() || !tmp_D.isDirectory() ) {   // if it doesn't exist, create  
        tmp_D.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }
      //clone nsIFile then append
      var OEBPS_D = tmp_D.clone();
      OEBPS_D.append("OEBPS");
      if( !OEBPS_D.exists() || !OEBPS_D.isDirectory() ) {   // if it doesn't exist, create  
        OEBPS_D.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }  
      //clone nsIFile then append
      var METAINF_D = tmp_D.clone();
      METAINF_D.append("META-INF");
      if( !METAINF_D.exists() || !METAINF_D.isDirectory() ) {   // if it doesn't exist, create  
        METAINF_D.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }  
    
      //save web page
      var webPage = OEBPS_D.clone();
      //var webFiles = OEBPS_D.clone();
      webPage.append("webPage.html");
      var webPageLocal = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
      webPageLocal.initWithPath(webPage.path);
      //webPage.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);
      //webPage.QueryInterface(Components.interfaces.nsILocalFile);
      
      var webFiles = OEBPS_D.clone();
      webFiles.append("Files");
      var webFilesLocal = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
      webFilesLocal.initWithPath(webFiles.path);
      /*webFiles.append("Files");
      if( !webFiles.exists() || !webFiles.isDirectory() ) {   // if it doesn't exist, create  
        webFiles.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }*/
      var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']  
		      .createInstance(Components.interfaces.nsIWebBrowserPersist);  
	  wbp.saveDocument(window.content.document, webPageLocal, webFilesLocal, null, wbp.ENCODE_FLAGS_RAW, null);  

    /**
     * Collect files informations
     */  
      var entries = webFiles.directoryEntries;  
      var array = [];  
      while(entries.hasMoreElements())  
      {  
        var entry = entries.getNext();  
        entry.QueryInterface(Components.interfaces.nsIFile);  
        array.push(entry);  
      }  
    /**
     * Create XML files which are mandatory for Epub such as mimetype, toc, content, container .. 
     */
    // mimetype
      tmp_D.append("mimetype");
      var ostream = FileUtils.openSafeFileOutputStream(tmp_D);  
  
      var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].  
                      createInstance(Components.interfaces.nsIScriptableUnicodeConverter);  
      converter.charset = "UTF-8";  
      var istream = converter.convertToInputStream("application/epub+zip");    
      NetUtil.asyncCopy(istream, ostream);

    // var file will be reused for each files
      var file;

    // container.xml
      file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
      file.initWithPath(METAINF.path);
      var oFOStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                      .createInstance(Components.interfaces.nsIFileOutputStream);  
      var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties)
                 .get("ProfD", Components.interfaces.nsILocalFile); // get profile folder  
      file.append("container.xml"); // filename  
      oFOStream.init(oFile, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate

      //create DOM doc
      
      (new XMLSerializer()).serializeToStream(doc, oFOStream, "");  
      oFOStream.close();  

    /**
     * Zip the whole structure without compressing the first file (mimetype)
     */
//TODO

    /**
     * Delete all temporary files
     */
        //All file have been created in temporary directory 

      /*

//TODO conservé pour ref, a nettoyer une fois devenu inutile  
      var file_mimetype = new File(filePath + "/mimetype");
	  	file_mimetype.create();
	  	file_mimetype.open('w')
	  	file_mimetype.write("application/epub+zip");
	  	file_mimetype.close();
  
      
  <commit_msg>correction d'un bug si la page web ne contenait pas de fichiers<commit_after>
Components.utils.import("resource://gre/modules/NetUtil.jsm");  
Components.utils.import("resource://gre/modules/FileUtils.jsm"); 
/**
 * XULfhte namespace.
 */
if ("undefined" == typeof(XULFHtEChrome)) {
  var XULFHtEChrome = {};
};

/**
 * Controls the browser overlay for the Hello World extension.
 */
XULFHtEChrome.BrowserOverlay = {
  /**
   * Save current web page to epub format
   */
  doWork : function(aEvent) {

    /**
     * Ask for file name and location
     */
    var nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    fp.init(window, "Save webpage as epub", nsIFilePicker.modeSave);
    fp.appendFilter("EPUB File","*.html");

    var rv = fp.show();
    if (rv == fp.returnCancel) return;

    if(rv == nsIFilePicker.returnOK){
      epub = fp.file;
      var epubPath = epub.path;

    /**
     * Save html page to working directory
     */
      var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
      var tmp_D = dirService.get("TmpD", Components.interfaces.nsIFile);
      //TODO: concatenate FHtE with a random number
      tmp_D.append("FHtE");
      if( !tmp_D.exists() || !tmp_D.isDirectory() ) {   // if it doesn't exist, create  
        tmp_D.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }
      //clone nsIFile then append
      var OEBPS_D = tmp_D.clone();
      OEBPS_D.append("OEBPS");
      if( !OEBPS_D.exists() || !OEBPS_D.isDirectory() ) {   // if it doesn't exist, create  
        OEBPS_D.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }  
      //clone nsIFile then append
      var METAINF_D = tmp_D.clone();
      METAINF_D.append("META-INF");
      if( !METAINF_D.exists() || !METAINF_D.isDirectory() ) {   // if it doesn't exist, create  
        METAINF_D.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }  
    
      //save web page
      var webPage = OEBPS_D.clone();
      //var webFiles = OEBPS_D.clone();
      webPage.append("webPage.html");
      var webPageLocal = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
      webPageLocal.initWithPath(webPage.path);
      //webPage.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);
      //webPage.QueryInterface(Components.interfaces.nsILocalFile);
      
      var webFiles = OEBPS_D.clone();
      webFiles.append("Files");
      var webFilesLocal = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
      webFilesLocal.initWithPath(webFiles.path);
      /*webFiles.append("Files");
      if( !webFiles.exists() || !webFiles.isDirectory() ) {   // if it doesn't exist, create  
        webFiles.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
      }*/
      var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']  
		      .createInstance(Components.interfaces.nsIWebBrowserPersist);  
	  wbp.saveDocument(window.content.document, webPageLocal, webFilesLocal, null, wbp.ENCODE_FLAGS_RAW, null);

    /**
     * Collect files informations
     */
      if (webFiles.exists()){
	var entries = webFiles.directoryEntries;
	var array = [];  
	while(entries.hasMoreElements())  
	{  
	  var entry = entries.getNext();  
	  entry.QueryInterface(Components.interfaces.nsIFile);  
	  array.push(entry);  
	}
      }
    /**
     * Create XML files which are mandatory for Epub such as mimetype, toc, content, container .. 
     */
    // mimetype
      tmp_D.append("mimetype");
      var ostream = FileUtils.openSafeFileOutputStream(tmp_D);  
  
      var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].  
                      createInstance(Components.interfaces.nsIScriptableUnicodeConverter);  
      converter.charset = "UTF-8";  
      var istream = converter.convertToInputStream("application/epub+zip");    
      NetUtil.asyncCopy(istream, ostream);

    // var file will be reused for each files
      var file;
      
    // container.xml
      file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
      file.initWithPath(METAINF_D.path);
      var oFOStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                      .createInstance(Components.interfaces.nsIFileOutputStream);   
      file.append("container.xml"); // filename  
      oFOStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate

      //create DOM doc
      var doc = document.implementation.createDocument("", "", null);
      var container = doc.createElement("container");
      container.setAttribute("version", "1.0");
      var rootfiles = doc.createElement("rootfiles");
      var rootfile1 = doc.createElement("rootfile");
      rootfile1.setAttribute("full-path", "OEBPS/content.opf");
      rootfile1.setAttribute("media-type", "application/oebps-package+xml");
      rootfiles.appendChild(rootfile1);
      container.appendChild(rootfiles);
      doc.appendChild(container);

      (new XMLSerializer()).serializeToStream(doc, oFOStream, "");  
      oFOStream.close();
      
    // content.opf
      file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
      file.initWithPath(OEBPS_D.path);
      var oFOStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                      .createInstance(Components.interfaces.nsIFileOutputStream);   
      file.append("content.opf"); // filename  
      oFOStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
      //doc
      (new XMLSerializer()).serializeToStream(doc, oFOStream, "");  
      oFOStream.close();
      
    // toc.ncx
      file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);
      file.initWithPath(OEBPS_D.path);
      var oFOStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                      .createInstance(Components.interfaces.nsIFileOutputStream);   
      file.append("toc.ncx"); // filename  
      oFOStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
      //doc
      (new XMLSerializer()).serializeToStream(doc, oFOStream, "");  
      oFOStream.close();
      
    /**
     * Zip the whole structure without compressing the first file (mimetype)
     */
//TODO

    /**
     * Delete all temporary files
     */
        //All file have been created in temporary directory 

      /*

//TODO conservé pour ref, a nettoyer une fois devenu inutile  
      var file_mimetype = new File(filePath + "/mimetype");
	  	file_mimetype.create();
	  	file_mimetype.open('w')
	  	file_mimetype.write("application/epub+zip");
	  	file_mimetype.close();
  
      
  
{'fraction_non_alphanumeric': 0.12946745562130177, 'fraction_numerical': 0.009704142011834319, 'mean_word_length': 2.854058984493767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '3719269', 'n_tokens_mistral': 4140, 'n_tokens_neox': 3953, 'n_words': 695}