<commit_before>config    = require './config'

log4js    = require 'log4js'
log       = log4js.getLogger("[#{config.name}]")

nodePath  = require 'path'
{exec}    = require 'child_process'
{spawn}   = require 'child_process'
fs        = require 'fs'
hat       = require 'hat'
ldap      = require 'ldapjs'
Kite      = require 'kite-amqp'
{bash}    = require 'koding-bash-user-glue'

createTmpDir = require './createtmpdir'

module.exports = new Kite 'sharedHosting'

  timeout:({timeout}, callback)->
    setTimeout (-> callback null, timeout), timeout

  interval:({interval}, callback)->
    setInterval (-> callback null, interval), interval

  executeCommand: require './executecommand'

  fetchSafeFileName:(options,callback)->
    {filePath}    = options
    original      = filePath+""
    originalDir   = nodePath.dirname original
    originalExt   = nodePath.extname original
    originalName  = nodePath.basename original,originalExt
    start = (i)->
      fs.stat filePath, (err,stat)->
        if stat?.isFile() or stat?.isDirectory()
          i++
          filePath = nodePath.join originalDir,originalName+"_"+i+originalExt
          start i
        else
          callback? null,filePath
    start 0

  uploadFile:(options,callback)->
    #
    # options =
    #    contents   : String # file text content
    #
    # console.log 'attempting to upload file', options
    {usersPath,fileUrl} = config
    {username,path,contents} = options
    log.debug "uploadFile is called",options.path
    createTmpDir username, (err, tmpDir)=>
      filename = hat()
      tmpPath = "#{tmpDir}/#{filename}"
      fs.writeFile tmpPath,contents,'utf8', (err)=>
        if err
          callback err
        else
          @executeCommand {username, command:"cp #{tmpPath} #{path}"}, (err,res)->
            unless err
              callback? null,path
            else
              callback? "[ERROR] can't upload file : #{err}"

  checkUid:(options,callback)->
    #
    # This methid will check user's uid
    #
    #
    # options =
    #   username : String  #username of the unix user
    #
    {username,nrOfRecursion} = options

    getuid = exec "/usr/bin/id -u #{username}", (err,stdout,stderr)=>
      if err?
        log.error "[ERROR] unable to get user's UID: #{stderr}"
        if nrOfRecursion is 1
          callback?  "[ERROR] unable to get user's UID, can't create user: #{stderr}"
        else
          @createSystemUser {username,fullName:username,password:hat()},(err,res)=>
            unless err
              log.info "User is just created, run the command again, it will work this time."
              @checkUid {username,nrOfRecursion:1},callback
            else
              log.error "CANT CREATE THIS USER"
              callback?  "[ERROR] unable to get user's UID, can't create user: #{stderr}"


      else if stdout < config.minAllowedUid
        stdout = stdout.replace(/(\r\n|\n|\r)/gm," ")
        log.error e = "[ERROR]  min UID for commands is #{config.minAllowedUid}, your #{stdout}"
        callback? e
      else
        stdout = stdout.replace(/(\r\n|\n|\r)/gm," ")
        log.debug "[OK] func:checkUid: user's #{username} UID #{stdout} allowed"
        callback? null
<commit_msg>Remove checkUid, its moved to executeCommand<commit_after>config    = require './config'

log4js    = require 'log4js'
log       = log4js.getLogger("[#{config.name}]")

nodePath  = require 'path'
{exec}    = require 'child_process'
{spawn}   = require 'child_process'
fs        = require 'fs'
hat       = require 'hat'
ldap      = require 'ldapjs'
Kite      = require 'kite-amqp'
{bash}    = require 'koding-bash-user-glue'

createTmpDir = require './createtmpdir'

module.exports = new Kite 'sharedHosting'

  timeout:({timeout}, callback)->
    setTimeout (-> callback null, timeout), timeout

  interval:({interval}, callback)->
    setInterval (-> callback null, interval), interval

  executeCommand: require './executecommand'

  fetchSafeFileName:(options,callback)->
    {filePath}    = options
    original      = filePath+""
    originalDir   = nodePath.dirname original
    originalExt   = nodePath.extname original
    originalName  = nodePath.basename original,originalExt
    start = (i)->
      fs.stat filePath, (err,stat)->
        if stat?.isFile() or stat?.isDirectory()
          i++
          filePath = nodePath.join originalDir,originalName+"_"+i+originalExt
          start i
        else
          callback? null,filePath
    start 0

  uploadFile:(options,callback)->
    #
    # options =
    #    contents   : String # file text content
    #
    # console.log 'attempting to upload file', options
    {usersPath,fileUrl} = config
    {username,path,contents} = options
    log.debug "uploadFile is called",options.path
    createTmpDir username, (err, tmpDir)=>
      filename = hat()
      tmpPath = "#{tmpDir}/#{filename}"
      fs.writeFile tmpPath,contents,'utf8', (err)=>
        if err
          callback err
        else
          @executeCommand {username, command:"cp #{tmpPath} #{path}"}, (err,res)->
            unless err
              callback? null,path
            else
              callback? "[ERROR] can't upload file : #{err}"

{'fraction_non_alphanumeric': 0.1260326609029779, 'fraction_numerical': 0.0023054755043227667, 'mean_word_length': 2.5318860244233377, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 28, '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': '4097513', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1524, 'n_words': 411}