<commit_before>async = require "async"
fs = require "fs"
fse = require "fs-extra"
Path = require "path"
logger = require "logger-sharelatex"
_ = require "underscore"

OutputFileOptimiser = require "./OutputFileOptimiser"

module.exports = OutputCacheManager =
	CACHE_SUBDIR: '.cache/clsi'
	BUILD_REGEX: /^[0-9a-f]+$/  # build id is Date.now() converted to hex
	CACHE_LIMIT: 2  # maximum number of cache directories
	CACHE_AGE: 60*60*1000 # up to one hour old

	path: (buildId, file) ->
		# used by static server, given build id return '.cache/clsi/buildId'
		if buildId.match OutputCacheManager.BUILD_REGEX
			return Path.join(OutputCacheManager.CACHE_SUBDIR, buildId, file)
		else
			# for invalid build id, return top level
			return file

	saveOutputFiles: (outputFiles, compileDir, callback = (error) ->) ->
		# make a compileDir/CACHE_SUBDIR/build_id directory and
		# copy all the output files into it
		cacheRoot = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR)
		# Put the files into a new cache subdirectory
		buildId = Date.now().toString(16)
		cacheDir = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR, buildId)
		# let file expiry run in the background
		OutputCacheManager.expireOutputFiles cacheRoot, {keep: buildId}

		checkFile = (src, callback) ->
			# check if we have a valid file to copy into the cache
			fs.stat src, (err, stats) ->
				if err?
					# some problem reading the file
					logger.error err: err, file: src, "stat error for file in cache"
					callback(err)
				else if not stats.isFile()
					# other filetype - reject it
					logger.error err: err, src: src, dst: dst, stat: stats, "nonfile output - refusing to copy to cache"
					callback(new Error("output file is not a file"), file)
				else
					# it's a plain file, ok to copy
					callback(null)

		copyFile = (src, dst, callback) ->
			# copy output file into the cache
			fse.copy src, dst, (err) ->
				if err?
					logger.error err: err, src: src, dst: dst, "copy error for file in cache"
					callback(err)
				else<commit_msg>fixed missing value in logger<commit_after>async = require "async"
fs = require "fs"
fse = require "fs-extra"
Path = require "path"
logger = require "logger-sharelatex"
_ = require "underscore"

OutputFileOptimiser = require "./OutputFileOptimiser"

module.exports = OutputCacheManager =
	CACHE_SUBDIR: '.cache/clsi'
	BUILD_REGEX: /^[0-9a-f]+$/  # build id is Date.now() converted to hex
	CACHE_LIMIT: 2  # maximum number of cache directories
	CACHE_AGE: 60*60*1000 # up to one hour old

	path: (buildId, file) ->
		# used by static server, given build id return '.cache/clsi/buildId'
		if buildId.match OutputCacheManager.BUILD_REGEX
			return Path.join(OutputCacheManager.CACHE_SUBDIR, buildId, file)
		else
			# for invalid build id, return top level
			return file

	saveOutputFiles: (outputFiles, compileDir, callback = (error) ->) ->
		# make a compileDir/CACHE_SUBDIR/build_id directory and
		# copy all the output files into it
		cacheRoot = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR)
		# Put the files into a new cache subdirectory
		buildId = Date.now().toString(16)
		cacheDir = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR, buildId)
		# let file expiry run in the background
		OutputCacheManager.expireOutputFiles cacheRoot, {keep: buildId}

		checkFile = (src, callback) ->
			# check if we have a valid file to copy into the cache
			fs.stat src, (err, stats) ->
				if err?
					# some problem reading the file
					logger.error err: err, file: src, "stat error for file in cache"
					callback(err)
				else if not stats.isFile()
					# other filetype - reject it
					logger.error err: err, src: src, stat: stats, "nonfile output - refusing to copy to cache"
					callback(new Error("output file is not a file"), file)
				else
					# it's a plain file, ok to copy
					callback(null)

		copyFile = (src, dst, callback) ->
			# copy output file into the cache
			fse.copy src, dst, (err) ->
				if err?
					logger.error err: err, src: src, dst: dst, "copy error for file in cache"
					callback(err)
				else
{'fraction_non_alphanumeric': 0.09882583170254403, 'fraction_numerical': 0.006360078277886497, 'mean_word_length': 3.5995500562429696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5378166', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1278, 'n_words': 463}