2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
projectLocator = require('../Project/ProjectLocator')
|
|
|
|
editorController = require('../Editor/EditorController')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
FileTypeManager = require('../Uploads/FileTypeManager')
|
2017-01-23 05:03:16 -05:00
|
|
|
uuid = require('uuid')
|
2014-02-12 05:23:40 -05:00
|
|
|
fs = require('fs')
|
|
|
|
|
|
|
|
module.exports =
|
2016-02-04 09:26:50 -05:00
|
|
|
mergeUpdate: (user_id, project_id, path, updateRequest, source, callback = (error) ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
self = @
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, path:path, "merging update from tpds"
|
2014-11-27 06:46:17 -05:00
|
|
|
projectLocator.findElementByPath project_id, path, (err, element)=>
|
|
|
|
# Returns an error if the element is not found
|
|
|
|
#return callback(err) if err?
|
|
|
|
logger.log project_id:project_id, path:path, "found element by path for merging update from tpds"
|
|
|
|
elementId = undefined
|
|
|
|
if element?
|
|
|
|
elementId = element._id
|
|
|
|
self.p.writeStreamToDisk project_id, elementId, updateRequest, (err, fsPath)->
|
2015-04-15 09:41:38 -04:00
|
|
|
callback = _.wrap callback, (cb, arg) ->
|
|
|
|
fs.unlink fsPath, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, fsPath:fsPath, "error deleting file"
|
|
|
|
cb(arg)
|
2014-11-27 06:46:17 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
FileTypeManager.isBinary path, fsPath, (err, isFile)->
|
2014-10-27 10:39:20 -04:00
|
|
|
return callback(err) if err?
|
2014-11-27 06:46:17 -05:00
|
|
|
if isFile
|
2017-11-13 07:20:14 -05:00
|
|
|
self.p.processFile project_id, elementId, fsPath, path, source, user_id, callback
|
2014-11-27 06:46:17 -05:00
|
|
|
else
|
2016-02-04 09:26:50 -05:00
|
|
|
self.p.processDoc project_id, elementId, user_id, fsPath, path, source, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-10-15 10:18:31 -04:00
|
|
|
deleteUpdate: (project_id, path, source, callback)->
|
2016-09-19 09:32:58 -04:00
|
|
|
projectLocator.findElementByPath project_id, path, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if err? || !element?
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log element:element, project_id:project_id, path:path, "could not find entity for deleting, assuming it was already deleted"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback()
|
2016-09-19 09:32:58 -04:00
|
|
|
logger.log project_id:project_id, path:path, type:type, element:element, "processing update to delete entity from tpds"
|
2014-11-27 06:46:17 -05:00
|
|
|
editorController.deleteEntity project_id, element._id, type, source, (err)->
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, path:path, "finished processing update to delete entity from tpds"
|
2014-02-12 05:23:40 -05:00
|
|
|
callback()
|
|
|
|
|
|
|
|
p:
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
processDoc: (project_id, doc_id, user_id, fsPath, path, source, callback)->
|
2014-02-12 05:23:40 -05:00
|
|
|
readFileIntoTextArray fsPath, (err, docLines)->
|
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, doc_id:doc_id, fsPath:fsPath, "error reading file into text array for process doc update"
|
|
|
|
return callback(err)
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log docLines:docLines, doc_id:doc_id, project_id:project_id, "processing doc update from tpds"
|
2014-02-12 05:23:40 -05:00
|
|
|
if doc_id?
|
2016-09-26 08:57:34 -04:00
|
|
|
editorController.setDoc project_id, doc_id, user_id, docLines, source, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
setupNewEntity project_id, path, (err, folder, fileName)->
|
2016-09-26 08:57:34 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, doc_id:doc_id, path:path, "error processing file"
|
|
|
|
return callback(err)
|
|
|
|
editorController.addDoc project_id, folder._id, fileName, docLines, source, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-11-13 07:20:14 -05:00
|
|
|
processFile: (project_id, file_id, fsPath, path, source, user_id, callback)->
|
2014-02-12 05:23:40 -05:00
|
|
|
finish = (err)->
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, file_id:file_id, path:path, "completed processing file update from tpds"
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(err)
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, file_id:file_id, path:path, "processing file update from tpds"
|
2014-02-12 05:23:40 -05:00
|
|
|
setupNewEntity project_id, path, (err, folder, fileName) =>
|
2016-09-26 08:57:34 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, file_id:file_id, path:path, "error processing file"
|
|
|
|
return callback(err)
|
|
|
|
else if file_id?
|
2014-10-15 10:18:31 -04:00
|
|
|
editorController.replaceFile project_id, file_id, fsPath, source, finish
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2017-11-13 07:20:14 -05:00
|
|
|
editorController.addFile project_id, folder?._id, fileName, fsPath, source, user_id, finish
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
writeStreamToDisk: (project_id, file_id, stream, callback = (err, fsPath)->)->
|
|
|
|
if !file_id?
|
2014-04-04 10:58:59 -04:00
|
|
|
file_id = uuid.v4()
|
2014-02-12 05:23:40 -05:00
|
|
|
dumpPath = "#{Settings.path.dumpFolder}/#{project_id}_#{file_id}"
|
|
|
|
|
|
|
|
writeStream = fs.createWriteStream(dumpPath)
|
|
|
|
stream.pipe(writeStream)
|
|
|
|
|
|
|
|
stream.on 'error', (err)->
|
|
|
|
logger.err err:err, project_id:project_id, file_id:file_id, dumpPath:dumpPath,
|
|
|
|
"something went wrong with incoming tpds update stream"
|
|
|
|
writeStream.on 'error', (err)->
|
|
|
|
logger.err err:err, project_id:project_id, file_id:file_id, dumpPath:dumpPath,
|
|
|
|
"something went wrong with writing tpds update to disk"
|
|
|
|
|
|
|
|
stream.on 'end', ->
|
|
|
|
logger.log project_id:project_id, file_id:file_id, dumpPath:dumpPath, "incoming tpds update stream ended"
|
|
|
|
writeStream.on "finish", ->
|
|
|
|
logger.log project_id:project_id, file_id:file_id, dumpPath:dumpPath, "tpds update write stream finished"
|
|
|
|
callback null, dumpPath
|
|
|
|
|
|
|
|
readFileIntoTextArray = (path, callback)->
|
|
|
|
fs.readFile path, "utf8", (error, content = "") ->
|
|
|
|
if error?
|
|
|
|
logger.err path:path, "error reading file into text array"
|
|
|
|
return callback(err)
|
2014-04-10 07:45:31 -04:00
|
|
|
lines = content.split(/\r\n|\n|\r/)
|
2014-02-12 05:23:40 -05:00
|
|
|
callback error, lines
|
|
|
|
|
|
|
|
|
|
|
|
setupNewEntity = (project_id, path, callback)->
|
|
|
|
lastIndexOfSlash = path.lastIndexOf("/")
|
|
|
|
fileName = path[lastIndexOfSlash+1 .. -1]
|
|
|
|
folderPath = path[0 .. lastIndexOfSlash]
|
2014-11-27 06:46:17 -05:00
|
|
|
editorController.mkdirp project_id, folderPath, (err, newFolders, lastFolder)->
|
2014-02-12 05:23:40 -05:00
|
|
|
callback err, lastFolder, fileName
|