2014-02-12 12:27:43 -05:00
|
|
|
UrlCache = require "./UrlCache"
|
|
|
|
Path = require "path"
|
|
|
|
fs = require "fs"
|
|
|
|
async = require "async"
|
|
|
|
mkdirp = require "mkdirp"
|
|
|
|
OutputFileFinder = require "./OutputFileFinder"
|
2017-08-18 05:22:17 -04:00
|
|
|
ResourceStateManager = require "./ResourceStateManager"
|
2014-02-12 12:27:43 -05:00
|
|
|
Metrics = require "./Metrics"
|
2015-09-08 16:58:27 -04:00
|
|
|
logger = require "logger-sharelatex"
|
2016-05-23 09:31:27 -04:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
|
|
|
|
parallelFileDownloads = settings.parallelFileDownloads or 1
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
module.exports = ResourceWriter =
|
2017-08-01 09:35:55 -04:00
|
|
|
|
2017-08-17 09:53:35 -04:00
|
|
|
syncResourcesToDisk: (request, basePath, callback = (error, resourceList) ->) ->
|
2017-08-07 09:26:13 -04:00
|
|
|
if request.syncType is "incremental"
|
2017-08-29 07:09:31 -04:00
|
|
|
logger.log project_id: request.project_id, user_id: request.user_id, "incremental sync"
|
2017-09-08 08:56:40 -04:00
|
|
|
ResourceStateManager.checkProjectStateMatches request.syncState, basePath, (error, resourceList) ->
|
2017-08-18 05:22:17 -04:00
|
|
|
return callback(error) if error?
|
2017-09-07 08:51:36 -04:00
|
|
|
ResourceWriter._removeExtraneousFiles resourceList, basePath, (error, outputFiles, allFiles) ->
|
2017-08-17 09:53:35 -04:00
|
|
|
return callback(error) if error?
|
2017-09-07 08:51:36 -04:00
|
|
|
ResourceStateManager.checkResourceFiles resourceList, allFiles, basePath, (error) ->
|
2017-08-17 11:59:37 -04:00
|
|
|
return callback(error) if error?
|
2017-09-07 08:51:36 -04:00
|
|
|
ResourceWriter.saveIncrementalResourcesToDisk request.project_id, request.resources, basePath, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, resourceList)
|
2017-08-01 09:35:55 -04:00
|
|
|
else
|
2017-08-29 07:09:31 -04:00
|
|
|
logger.log project_id: request.project_id, user_id: request.user_id, "full sync"
|
2017-08-01 09:35:55 -04:00
|
|
|
@saveAllResourcesToDisk request.project_id, request.resources, basePath, (error) ->
|
|
|
|
return callback(error) if error?
|
2017-09-08 08:56:40 -04:00
|
|
|
ResourceStateManager.saveProjectState request.syncState, request.resources, basePath, (error) ->
|
2017-08-17 09:53:35 -04:00
|
|
|
return callback(error) if error?
|
2017-09-07 06:54:38 -04:00
|
|
|
callback(null, request.resources)
|
2017-08-01 09:35:55 -04:00
|
|
|
|
|
|
|
saveIncrementalResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
|
|
|
|
@_createDirectory basePath, (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = for resource in resources
|
|
|
|
do (resource) =>
|
|
|
|
(callback) => @_writeResourceToDisk(project_id, resource, basePath, callback)
|
|
|
|
async.parallelLimit jobs, parallelFileDownloads, callback
|
|
|
|
|
|
|
|
saveAllResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
|
2016-03-31 06:25:25 -04:00
|
|
|
@_createDirectory basePath, (error) =>
|
2014-02-12 12:27:43 -05:00
|
|
|
return callback(error) if error?
|
2016-03-31 06:25:25 -04:00
|
|
|
@_removeExtraneousFiles resources, basePath, (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = for resource in resources
|
|
|
|
do (resource) =>
|
|
|
|
(callback) => @_writeResourceToDisk(project_id, resource, basePath, callback)
|
2016-05-23 09:31:27 -04:00
|
|
|
async.parallelLimit jobs, parallelFileDownloads, callback
|
2016-03-31 06:25:25 -04:00
|
|
|
|
|
|
|
_createDirectory: (basePath, callback = (error) ->) ->
|
|
|
|
fs.mkdir basePath, (err) ->
|
|
|
|
if err?
|
|
|
|
if err.code is 'EEXIST'
|
|
|
|
return callback()
|
|
|
|
else
|
|
|
|
logger.log {err: err, dir:basePath}, "error creating directory"
|
|
|
|
return callback(err)
|
|
|
|
else
|
|
|
|
return callback()
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2017-09-07 08:51:36 -04:00
|
|
|
_removeExtraneousFiles: (resources, basePath, _callback = (error, outputFiles, allFiles) ->) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
timer = new Metrics.Timer("unlink-output-files")
|
2017-09-07 08:51:36 -04:00
|
|
|
callback = (error, result...) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
timer.done()
|
2017-09-07 08:51:36 -04:00
|
|
|
_callback(error, result...)
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2017-09-07 08:51:36 -04:00
|
|
|
OutputFileFinder.findOutputFiles resources, basePath, (error, outputFiles, allFiles) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
|
|
|
|
jobs = []
|
|
|
|
for file in outputFiles or []
|
|
|
|
do (file) ->
|
|
|
|
path = file.path
|
|
|
|
should_delete = true
|
2016-09-22 09:14:29 -04:00
|
|
|
if path.match(/^output\./) or path.match(/\.aux$/) or path.match(/^cache\//) # knitr cache
|
2014-02-12 12:27:43 -05:00
|
|
|
should_delete = false
|
2017-09-28 11:36:25 -04:00
|
|
|
if path.match(/^output-.*/) # Tikz cached figures
|
|
|
|
should_delete = false
|
2018-09-11 04:44:22 -04:00
|
|
|
if path.match(/-eps-converted-to\.pdf$/) # Epstopdf generated files
|
|
|
|
should_delete = false
|
2017-06-15 10:37:45 -04:00
|
|
|
if path == "output.pdf" or path == "output.dvi" or path == "output.log" or path == "output.xdv"
|
2014-02-12 12:27:43 -05:00
|
|
|
should_delete = true
|
2017-03-16 12:55:53 -04:00
|
|
|
if path == "output.tex" # created by TikzManager if present in output files
|
|
|
|
should_delete = true
|
2014-02-12 12:27:43 -05:00
|
|
|
if should_delete
|
|
|
|
jobs.push (callback) -> ResourceWriter._deleteFileIfNotDirectory Path.join(basePath, path), callback
|
|
|
|
|
2017-09-07 08:51:36 -04:00
|
|
|
async.series jobs, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, outputFiles, allFiles)
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
_deleteFileIfNotDirectory: (path, callback = (error) ->) ->
|
|
|
|
fs.stat path, (error, stat) ->
|
2016-04-04 11:22:48 -04:00
|
|
|
if error? and error.code is 'ENOENT'
|
|
|
|
return callback()
|
|
|
|
else if error?
|
|
|
|
logger.err {err: error, path: path}, "error stating file in deleteFileIfNotDirectory"
|
|
|
|
return callback(error)
|
|
|
|
else if stat.isFile()
|
|
|
|
fs.unlink path, (error) ->
|
|
|
|
if error?
|
|
|
|
logger.err {err: error, path: path}, "error removing file in deleteFileIfNotDirectory"
|
|
|
|
callback(error)
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-12 12:27:43 -05:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
_writeResourceToDisk: (project_id, resource, basePath, callback = (error) ->) ->
|
2017-03-21 07:29:37 -04:00
|
|
|
ResourceWriter.checkPath basePath, resource.path, (error, path) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
return callback(error) if error?
|
2017-03-21 07:29:37 -04:00
|
|
|
mkdirp Path.dirname(path), (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
# TODO: Don't overwrite file if it hasn't been modified
|
|
|
|
if resource.url?
|
|
|
|
UrlCache.downloadUrlToFile project_id, resource.url, path, resource.modified, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, path:path, resource_url:resource.url, modified:resource.modified, "error downloading file for resources"
|
|
|
|
callback() #try and continue compiling even if http resource can not be downloaded at this time
|
|
|
|
else
|
|
|
|
fs.writeFile path, resource.content, callback
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2017-03-21 07:29:37 -04:00
|
|
|
checkPath: (basePath, resourcePath, callback) ->
|
|
|
|
path = Path.normalize(Path.join(basePath, resourcePath))
|
2017-03-21 07:30:32 -04:00
|
|
|
if (path.slice(0, basePath.length + 1) != basePath + "/")
|
2017-03-21 07:29:37 -04:00
|
|
|
return callback new Error("resource path is outside root directory")
|
|
|
|
else
|
|
|
|
return callback(null, path)
|