save output files in a .cache directory

This commit is contained in:
Brian Gough 2015-02-24 14:40:05 +00:00
parent 4532cd14b0
commit 4002849e20
2 changed files with 52 additions and 1 deletions

View file

@ -1,6 +1,7 @@
ResourceWriter = require "./ResourceWriter" ResourceWriter = require "./ResourceWriter"
LatexRunner = require "./LatexRunner" LatexRunner = require "./LatexRunner"
OutputFileFinder = require "./OutputFileFinder" OutputFileFinder = require "./OutputFileFinder"
OutputCacheManager = require "./OutputCacheManager"
Settings = require("settings-sharelatex") Settings = require("settings-sharelatex")
Path = require "path" Path = require "path"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
@ -32,7 +33,8 @@ module.exports = CompileManager =
OutputFileFinder.findOutputFiles request.resources, compileDir, (error, outputFiles) -> OutputFileFinder.findOutputFiles request.resources, compileDir, (error, outputFiles) ->
return callback(error) if error? return callback(error) if error?
callback null, outputFiles OutputCacheManager.saveOutputFiles outputFiles, compileDir, (error, newOutputFiles) ->
callback null, newOutputFiles
clearProject: (project_id, _callback = (error) ->) -> clearProject: (project_id, _callback = (error) ->) ->
callback = (error) -> callback = (error) ->

View file

@ -0,0 +1,49 @@
async = require "async"
fs = require "fs"
fse = require "fs-extra"
Path = require "path"
logger = require "logger-sharelatex"
_ = require "underscore"
module.exports = OutputCacheManager =
CACHE_DIR: '.cache/clsi'
saveOutputFiles: (outputFiles, target, callback) ->
# make a target/build_id directory and
# copy all the output files into it
buildId = 'build-' + Date.now()
relDir = OutputCacheManager.CACHE_DIR + '/' + buildId
newDir = target + '/' + relDir
OutputCacheManager.expireOutputFiles target
fse.ensureDir newDir, (err) ->
if err?
callback(err, outputFiles)
else
async.mapSeries outputFiles, (file, cb) ->
newFile = _.clone(file)
newFile.path = relDir + '/' + file.path
src = target + '/' + file.path
dst = target + '/' + newFile.path
#console.log 'src', src, 'dst', dst
fs.stat src, (err, stats) ->
if err?
cb(err)
else if stats.isFile()
#console.log 'isFile: copying'
fse.copy src, dst, (err) ->
cb(err, newFile)
else
# other filetype - shouldn't happen
cb(new Error("output file is not a file"), file)
, (err, results) ->
if err?
callback err, outputFiles
else
callback(err, results)
expireOutputFiles: (target, callback) ->
# look in target for build dirs and delete if > N or age of mod time > T
cacheDir = target + '/' + OutputCacheManager.CACHE_DIR
fs.readdir cacheDir, (err, results) ->
console.log 'CACHEDIR', results
callback(err) if callback?