<commit_before>'use strict'

var fs = require('fs-extra')
var Path = require('path')
var Database = require(Path.join(__base, 'src/database/client.js'))
var DB = {
  directory: new Database('directory', '127.0.0.1', __config.database.port, __DBtoken)
}

var Log = require(Path.join(__base, 'src/worker/log.js'))
var LogWorker = new Log({
  module: 'Directory'
})
/**
 * Directory manager.
 * @constructor
 */
function Directory () {
  var self = this

  DB.directory.update({}, {
    $set: {
      downloading: 0
    }
  }, {}, function (err) {
    if (err) {
      LogWorker.error(err)
    }
  })
}

/**
 * Get the list and informations of files into a specific directory.
 * @param {string} dir - Directory to scan.
 * @return {object} - Directory informations.
*/
Directory.prototype.list = function (dir, cb) {
  var self = this
  var response = {
    'totalSize': 0,
    'files': {}
  }
  var list = function () {
    var childrens = []
    fs.walk(Path.join(__config.directory.path, dir))
      .on('data', function (child) {
        var reChild = new RegExp(Path.join(__config.directory.path, dir) + '[^\/]*$', 'g')

        if (child.path.match(reChild)) {
          var name = child.path.split('/')
          if (name[name.length - 1] === '' && parent.length > 2) name.pop()
          name = name[name.length - 1]
          child.stats.isfile = fs.lstatSync(child.path).isFile()
          child.stats.isdir = fs.lstatSync(child.path).isDirectory()
          response.files[name] = child.stats
        }

        response.totalSize += child.stats.size
      })
      .on('end', function () {
        var parent = dir.split('/')
        if (parent[parent.length - 1] === '' && parent.length > 1) parent.pop()
        parent = parent[parent.length - 1]

        DB.directory.find({
          parent: parent
        }, function (err, files) {
          if (err) {
            LogWorker.error(err)
            cb(false)
          } else {
            DB.directory.find({
              name: parent
            }, function(err, parents){
              for (var f in response.files) {
                var find = files.find(function (e) { return e.name == f && e.parent == parent })
                if (find == null) {
                  DB.directory.insert({
                    parent: parent,
                    name: f,
                    download: 0,
                    downloading: 0,
                    owner: parents[0].owner
                  })
                  response.files[f].download = 0
                  response.files[f].downloading = 0
                  response.files[f].owner = parents[0].owner
                } else {
                  response.files[f].download = find.download
                  response.files[f].downloading = find.downloading
                  response.files[f].owner = find.owner
                }
              }
              cb(response)
            })
          }
        })
      })
  }

  setTimeout(list)
}

/**
 * Lock a file.
 * @param {string} file - File to lock.
*/
Directory.prototype.setDownloading = function (file, cb) {
  var self = this
  var setDownloading = function () {
    file = file.split('/')
    if (file[file.length - 1] === '' && parent.length > 2) file.pop()
    var name = file[file.length - 1]
    var parent = file[file.length - 2] || ''

    DB.directory.update({
      name: name,
      parent: parent<commit_msg>some little fix if there are no parents<commit_after>'use strict'

var fs = require('fs-extra')
var Path = require('path')
var Database = require(Path.join(__base, 'src/database/client.js'))
var DB = {
  directory: new Database('directory', '127.0.0.1', __config.database.port, __DBtoken)
}

var Log = require(Path.join(__base, 'src/worker/log.js'))
var LogWorker = new Log({
  module: 'Directory'
})
/**
 * Directory manager.
 * @constructor
 */
function Directory () {
  var self = this

  DB.directory.update({}, {
    $set: {
      downloading: 0
    }
  }, {}, function (err) {
    if (err) {
      LogWorker.error(err)
    }
  })
}

/**
 * Get the list and informations of files into a specific directory.
 * @param {string} dir - Directory to scan.
 * @return {object} - Directory informations.
*/
Directory.prototype.list = function (dir, cb) {
  var self = this
  var response = {
    'totalSize': 0,
    'files': {}
  }
  var list = function () {
    var childrens = []
    fs.walk(Path.join(__config.directory.path, dir))
      .on('data', function (child) {
        var reChild = new RegExp(Path.join(__config.directory.path, dir) + '[^\/]*$', 'g')

        if (child.path.match(reChild)) {
          var name = child.path.split('/')
          if (name[name.length - 1] === '' && parent.length > 2) name.pop()
          name = name[name.length - 1]
          child.stats.isfile = fs.lstatSync(child.path).isFile()
          child.stats.isdir = fs.lstatSync(child.path).isDirectory()
          response.files[name] = child.stats
        }

        response.totalSize += child.stats.size
      })
      .on('end', function () {
        var parent = dir.split('/')
        if (parent[parent.length - 1] === '' && parent.length > 1) parent.pop()
        parent = parent[parent.length - 1]

        DB.directory.find({
          parent: parent
        }, function (err, files) {
          if (err) {
            LogWorker.error(err)
            cb(false)
          } else {
            DB.directory.find({
              name: parent
            }, function(err, parents){
              for (var f in response.files) {
                var find = files.find(function (e) { return e.name == f && e.parent == parent })
                if (find == null) {
                  DB.directory.insert({
                    parent: parent,
                    name: f,
                    download: 0,
                    downloading: 0,
                    owner: parents[0] ? parents[0].owner : ""
                  })
                  response.files[f].download = 0
                  response.files[f].downloading = 0
                  response.files[f].owner = parents[0] ? parents[0].owner : ""
                } else {
                  response.files[f].download = find.download
                  response.files[f].downloading = find.downloading
                  response.files[f].owner = find.owner
                }
              }
              cb(response)
            })
          }
        })
      })
  }

  setTimeout(list)
}

/**
 * Lock a file.
 * @param {string} file - File to lock.
*/
Directory.prototype.setDownloading = function (file, cb) {
  var self = this
  var setDownloading = function () {
    file = file.split('/')
    if (file[file.length - 1] === '' && parent.length > 2) file.pop()
    var name = file[file.length - 1]
    var parent = file[file.length - 2] || ''

    DB.directory.update({
      name: name,
      parent: parent
{'fraction_non_alphanumeric': 0.14819136522753792, 'fraction_numerical': 0.007292882147024504, 'mean_word_length': 1.7693861066235865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2191832', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1999, 'n_words': 470}