<commit_before>/* Copyright (c) 2012-2013 The TagSpaces Authors. All rights reserved.
 * Use of this source code is governed by a AGPL3 license that
 * can be found in the LICENSE file. */
define(function(require, exports, module) {
  "use strict";

  // Activating browser specific exports modul
  console.log("Loading web.js..");

  var TSCORE = require("tscore");
  var TSPOSTIO = require("tspostioapi");

  require("webdavlib/webdavlib");

  var davClient;
  //exact copy of getAjax with timeout added 
  nl.sara.webdav.Client.prototype.getAjax = function(method, url, callback, headers) {
    var /** @type XMLHttpRequest */ ajax = (((typeof Components !== 'undefined') && (typeof Components.classes !== 'undefined')) ? Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest) : new XMLHttpRequest());
    if (this._username !== null) {
      ajax.open(method, url, true, this._username, this._password);
    } else {
      ajax.open(method, url, true);
    }
    ajax.onreadystatechange = function() {
      nl.sara.webdav.Client.ajaxHandler(ajax, callback);
    };
    
    ajax.ontimeout = function() {
      ajax.readyState = 4;
      ajax.ajax.status = -1;
      nl.sara.webdav.Client.ajaxHandler(ajax, callback);
    };

    if (headers === undefined) {
      headers = {};
    }
    for (var header in this._headers) {
      if (headers[header] === undefined) {
        ajax.setRequestHeader(header, this._headers[header]);
      }
    }
    for (var header in headers) {
      ajax.setRequestHeader(header, headers[header]);
    }
    return ajax;
  };

  function connectDav() {
    console.log("Connecting webdav...");
    var useHTTPS = false;
    if (location.href.indexOf("https") === 0) {
      useHTTPS = true;
    }
    davClient = new nl.sara.webdav.Client(location.hostname, useHTTPS, location.port);
  }

  //window.setTimeout(
  connectDav();
  //, 2000);

  function getNameForPath(path) {
    if (path.lastIndexOf("/") == path.length - 1) {
      path = path.substring(0, path.lastIndexOf("/"));
    }
    var encodedName = path.substring(path.lastIndexOf("/") + 1, path.length);
    return decodeURI(encodedName);
  }

  function isDirectory(path) {
    // TODO find a better solution
    return path.lastIndexOf("/") == path.length - 1;
  }

  function checkStatusCode(code) {
    var status = parseInt(code / 100);
    if (status === 2) {
      return true;
    }
    return false;
  }


  function focusWindow() {
    // Bring the TagSpaces window on top of the windows
    window.focus();
  }

  function checkNewVersion() {
    //
    console.log("Checking for new version not relevant fot the webdav version");
  }


  function createDirectoryTree(dirPath) {
    console.log("Creating directory index for: " + dirPath);
    TSCORE.showLoadingAnimation();

    var directoyTree = [];
    //console.log(JSON.stringify(directoyTree));
    TSPOSTIO.createDirectoryTree(directoyTree);
  }

  function listDirectoryPromise(dirPath) {
    console.log("Listing directory: " + dirPath);
    return new Promise(function(resolve, reject) {
      var anotatedDirList = [];
      dirPath = encodeURI(dirPath + "/");
      davClient.propfind(
        dirPath, //encodeURI(dirPath),
        function(status, data) {
          console.log("Dirlist Status:  " + status);
          if (!checkStatusCode(status)) { 
            reject("Listing directory " + dirPath + " failed " + status);
            console.warn("Listing directory " + dirPath + " failed " + status);
            return;
          }
          var dirList = data._responses,
          fileName, isDir, filesize, lmdt;

          for (var entry in dirList) {
            var path = dirList[entry].href;
            if (dirPath.toLowerCase() !== path.toLowerCase()) {
              isDir = false;
              filesize = undefined;
              lmdt = undefined;
              //console.log(dirList[entry]._namespaces["DAV:"]);
              if (typeof dirList[entry]._namespaces["DAV:"].getcontentlength === 'undefined' ||
                dirList[entry]._namespaces["DAV:"].getcontentlength._xmlvalue.length === 0
              ) {
                isDir = true;
              } else {
                filesize = dirList[entry]._namespaces["DAV:"].getcontentlength;
                lmdt = data._responses[entry]._namespaces["DAV:"].getlastmodified._xmlvalue[0].data;
              }
              fileName = getNameForPath(path);
              anotatedDirList.push({
                "name": fileName,
                "isFile": !isDir,
                "size": filesize,
                "lmdt": lmdt,
                "path": decodeURI(path)
              });
            }
          }
          resolve(anotatedDirList);
        },
        1 //1 , davClient.INFINITY
      );
    });
  }


  function getPropertiesPromise(filePath) {
    return new Promise(function(resolve, reject) {
      davClient.propfind(encodeURI(filePath), function(status, data) {
        console.log("Properties Status / Content: " + status + " / " + JSON.stringify(data._responses));
        var fileProperties = {};
        if (checkStatusCode(status)) {
          for (var entry in data._responses) {
            fileProperties.path = filePath;<commit_msg>correcting search in webdav<commit_after>/* Copyright (c) 2012-2013 The TagSpaces Authors. All rights reserved.
 * Use of this source code is governed by a AGPL3 license that
 * can be found in the LICENSE file. */
define(function(require, exports, module) {
  "use strict";

  // Activating browser specific exports modul
  console.log("Loading web.js..");

  var TSCORE = require("tscore");
  var TSPOSTIO = require("tspostioapi");

  require("webdavlib/webdavlib");

  var davClient;
  //exact copy of getAjax with timeout added 
  nl.sara.webdav.Client.prototype.getAjax = function(method, url, callback, headers) {
    var /** @type XMLHttpRequest */ ajax = (((typeof Components !== 'undefined') && (typeof Components.classes !== 'undefined')) ? Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest) : new XMLHttpRequest());
    if (this._username !== null) {
      ajax.open(method, url, true, this._username, this._password);
    } else {
      ajax.open(method, url, true);
    }
    ajax.onreadystatechange = function() {
      nl.sara.webdav.Client.ajaxHandler(ajax, callback);
    };
    
    ajax.ontimeout = function() {
      ajax.readyState = 4;
      ajax.ajax.status = -1;
      nl.sara.webdav.Client.ajaxHandler(ajax, callback);
    };

    if (headers === undefined) {
      headers = {};
    }
    for (var header in this._headers) {
      if (headers[header] === undefined) {
        ajax.setRequestHeader(header, this._headers[header]);
      }
    }
    for (var header in headers) {
      ajax.setRequestHeader(header, headers[header]);
    }
    return ajax;
  };

  function connectDav() {
    console.log("Connecting webdav...");
    var useHTTPS = false;
    if (location.href.indexOf("https") === 0) {
      useHTTPS = true;
    }
    davClient = new nl.sara.webdav.Client(location.hostname, useHTTPS, location.port);
  }

  //window.setTimeout(
  connectDav();
  //, 2000);

  function getNameForPath(path) {
    if (path.lastIndexOf("/") == path.length - 1) {
      path = path.substring(0, path.lastIndexOf("/"));
    }
    var encodedName = path.substring(path.lastIndexOf("/") + 1, path.length);
    return decodeURI(encodedName);
  }

  function isDirectory(path) {
    // TODO find a better solution
    return path.lastIndexOf("/") == path.length - 1;
  }

  function checkStatusCode(code) {
    var status = parseInt(code / 100);
    if (status === 2) {
      return true;
    }
    return false;
  }


  function focusWindow() {
    // Bring the TagSpaces window on top of the windows
    window.focus();
  }

  function checkNewVersion() {
    //
    console.log("Checking for new version not relevant fot the webdav version");
  }


  function createDirectoryTree(dirPath) {
    console.log("Creating directory index for: " + dirPath);
    TSCORE.showLoadingAnimation();

    var directoyTree = [];
    //console.log(JSON.stringify(directoyTree));
    TSPOSTIO.createDirectoryTree(directoyTree);
  }

  function listDirectoryPromise(dirPath) {
    dirPath = dirPath.split("//").join("/");
    console.log("Listing directory: " + dirPath);
    return new Promise(function(resolve, reject) {
      var anotatedDirList;

      var davSuccess = function(status, data) {
        console.log("Dirlist Status:  " + status);
        if (!checkStatusCode(status)) {
          console.warn("Listing directory " + dirPath + " failed " + status);
          reject("Listing directory " + dirPath + " failed " + status);
        }
        var dirList = data._responses, fileName, isDir, filesize, lmdt, path;

        anotatedDirList = [];
        for (var entry in dirList) {
          path = dirList[entry].href;
          console.log("---dP" + dirPath.toLowerCase());
          console.log("---p" + path.toLowerCase());
          if (dirPath.toLowerCase() === path.toLowerCase()) {
            console.log("Skipping current folder");
          } else {
            isDir = false;
            filesize = undefined;
            lmdt = undefined;
            //console.log(dirList[entry]._namespaces["DAV:"]);
            if (typeof dirList[entry]._namespaces["DAV:"].getcontentlength === 'undefined' ||
              dirList[entry]._namespaces["DAV:"].getcontentlength._xmlvalue.length === 0
            ) {
              isDir = true;
            } else {
              filesize = dirList[entry]._namespaces["DAV:"].getcontentlength._xmlvalue[0].data;
              lmdt = data._responses[entry]._namespaces["DAV:"].getlastmodified._xmlvalue[0].data;
            }
            fileName = getNameForPath(path);
            anotatedDirList.push({
              "name": fileName,
              "isFile": !isDir,
              "size": filesize,
              "lmdt": lmdt,
              "path": decodeURI(path)
            });
          }
        }
        resolve(anotatedDirList);
      }

      if(dirPath.substring(dirPath.length-1) !== "/") {
        dirPath = dirPath + "/"
      }
      dirPath = encodeURI(dirPath);

      davClient.propfind(
        dirPath,
        davSuccess,
        1 //1 , davClient.INFINITY
      );
    });
  }


  function getPropertiesPromise(filePath) {
    return new Promise(function(resolve, reject) {
      davClient.propfind(encodeURI(filePath), function(status, data) {
        console.log("Properties Status / Content: " + status + " / " + JSON.stringify(data._responses));
        var fileProperties = {};
        if (checkStatusCode(status)) {
          for (var entry in data._responses) {
            fileProperties.path = filePath;
{'fraction_non_alphanumeric': 0.1362840018441678, 'fraction_numerical': 0.005532503457814661, 'mean_word_length': 2.48970398970399, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, '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': '190145', 'n_tokens_mistral': 3294, 'n_tokens_neox': 3179, 'n_words': 681}