<commit_before>/* eslint-disable
    handle-callback-err,
    no-unreachable,
    node/no-deprecated-api,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
const logger = require('logger-sharelatex')
const fs = require('fs')
const path = require('path')
const LocalFileWriter = require('./LocalFileWriter')
const Errors = require('./Errors')
const rimraf = require('rimraf')
const _ = require('underscore')

const filterName = key => key.replace(/\//g, '_')

module.exports = {
  sendFile(location, target, source, callback) {
    if (callback == null) {
      callback = function(err) {}
    }
    const filteredTarget = filterName(target)
    logger.log({ location, target: filteredTarget, source }, 'sending file')
    const done = _.once(function(err) {
      if (err != null) {
        logger.err(
          { err, location, target: filteredTarget, source },
          'Error on put of file'
        )
      }
      return callback(err)
    })
    // actually copy the file (instead of moving it) to maintain consistent behaviour
    // between the different implementations
    const sourceStream = fs.createReadStream(source)
    sourceStream.on('error', done)
    const targetStream = fs.createWriteStream(`${location}/${filteredTarget}`)
    targetStream.on('error', done)
    targetStream.on('finish', () => done())
    return sourceStream.pipe(targetStream)
  },

  sendStream(location, target, sourceStream, callback) {
    if (callback == null) {
      callback = function(err) {}
    }
    logger.log({ location, target }, 'sending file stream')
    sourceStream.on('error', err =>
      logger.err({ location, target, err: err('error on stream to send') })
    )
    return LocalFileWriter.writeStream(sourceStream, null, (err, fsPath) => {
      if (err != null) {
        logger.err(
          { location, target, fsPath, err },
          'something went wrong writing stream to disk'
        )
        return callback(err)
      }
      return this.sendFile(location, target, fsPath, (
        err // delete the temporary file created above and return the original error
      ) => LocalFileWriter.deleteFile(fsPath, () => callback(err)))
    })
  },

  // opts may be {start: Number, end: Number}
  getFileStream(location, name, opts, callback) {
    if (callback == null) {
      callback = function(err, res) {}
    }
    const filteredName = filterName(name)
    logger.log({ location, filteredName }, 'getting file')
    return fs.open(`${location}/${filteredName}`, 'r', function(err, fd) {
      if (err != null) {
        logger.err(
          { err, location, filteredName: name },
          'Error reading from file'
        )
      }
      if (err.code === 'ENOENT') {
        return callback(new Errors.NotFoundError(err.message), null)
      } else {
        return callback(err, null)
      }
      opts.fd = fd
      const sourceStream = fs.createReadStream(null, opts)
      return callback(null, sourceStream)
    })
  },

  getFileSize(location, filename, callback) {
    const fullPath = path.join(location, filterName(filename))
    return fs.stat(fullPath, function(err, stats) {
      if (err != null) {
        if (err.code === 'ENOENT') {
          logger.log({ location, filename }, 'file not found')
          callback(new Errors.NotFoundError(err.message))
        } else {
          logger.err({ err, location, filename }, 'failed to stat file')
          callback(err)
        }
        return
      }
      return callback(null, stats.size)
    })
  },

  copyFile(location, fromName, toName, callback) {
    if (callback == null) {
      callback = function(err) {}
    }<commit_msg>Fix decaf error in FSPersistorManager<commit_after>/* eslint-disable
    handle-callback-err,
    no-unreachable,
    node/no-deprecated-api,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
const logger = require('logger-sharelatex')
const fs = require('fs')
const path = require('path')
const LocalFileWriter = require('./LocalFileWriter')
const Errors = require('./Errors')
const rimraf = require('rimraf')
const _ = require('underscore')

const filterName = key => key.replace(/\//g, '_')

module.exports = {
  sendFile(location, target, source, callback) {
    if (callback == null) {
      callback = function(err) {}
    }
    const filteredTarget = filterName(target)
    logger.log({ location, target: filteredTarget, source }, 'sending file')
    const done = _.once(function(err) {
      if (err != null) {
        logger.err(
          { err, location, target: filteredTarget, source },
          'Error on put of file'
        )
      }
      return callback(err)
    })
    // actually copy the file (instead of moving it) to maintain consistent behaviour
    // between the different implementations
    const sourceStream = fs.createReadStream(source)
    sourceStream.on('error', done)
    const targetStream = fs.createWriteStream(`${location}/${filteredTarget}`)
    targetStream.on('error', done)
    targetStream.on('finish', () => done())
    return sourceStream.pipe(targetStream)
  },

  sendStream(location, target, sourceStream, callback) {
    if (callback == null) {
      callback = function(err) {}
    }
    logger.log({ location, target }, 'sending file stream')
    sourceStream.on('error', err =>
      logger.err({ location, target, err: err('error on stream to send') })
    )
    return LocalFileWriter.writeStream(sourceStream, null, (err, fsPath) => {
      if (err != null) {
        logger.err(
          { location, target, fsPath, err },
          'something went wrong writing stream to disk'
        )
        return callback(err)
      }
      return this.sendFile(location, target, fsPath, (
        err // delete the temporary file created above and return the original error
      ) => LocalFileWriter.deleteFile(fsPath, () => callback(err)))
    })
  },

  // opts may be {start: Number, end: Number}
  getFileStream(location, name, opts, callback) {
    if (callback == null) {
      callback = function(err, res) {}
    }
    const filteredName = filterName(name)
    logger.log({ location, filteredName }, 'getting file')
    return fs.open(`${location}/${filteredName}`, 'r', function(err, fd) {
      if (err != null) {
        logger.err(
          { err, location, filteredName: name },
          'Error reading from file'
        )
        if (err.code === 'ENOENT') {
          return callback(new Errors.NotFoundError(err.message), null)
        } else {
          return callback(err, null)
        }
      }
      opts.fd = fd
      const sourceStream = fs.createReadStream(null, opts)
      return callback(null, sourceStream)
    })
  },

  getFileSize(location, filename, callback) {
    const fullPath = path.join(location, filterName(filename))
    return fs.stat(fullPath, function(err, stats) {
      if (err != null) {
        if (err.code === 'ENOENT') {
          logger.log({ location, filename }, 'file not found')
          callback(new Errors.NotFoundError(err.message))
        } else {
          logger.err({ err, location, filename }, 'failed to stat file')
          callback(err)
        }
        return
      }
      return callback(null, stats.size)
    })
  },

  copyFile(location, fromName, toName, callback) {
    if (callback == null) {
      callback = function(err) {}
    }
{'fraction_non_alphanumeric': 0.13041276272474045, 'fraction_numerical': 0.0015193719929095975, 'mean_word_length': 2.8104196816208393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '6639138', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2224, 'n_words': 622}