2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
2018-02-01 10:31:42 -05:00
|
|
|
fs = require('fs')
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
2018-02-01 10:31:42 -05:00
|
|
|
EditorController = require('../Editor/EditorController')
|
|
|
|
FileTypeManager = require('../Uploads/FileTypeManager')
|
2018-02-14 10:12:46 -05:00
|
|
|
FileWriter = require('../../infrastructure/FileWriter')
|
2018-10-23 09:05:50 -04:00
|
|
|
ProjectEntityHandler = require('../Project/ProjectEntityHandler')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-01-29 06:47:01 -05:00
|
|
|
module.exports = UpdateMerger =
|
2016-02-04 09:26:50 -05:00
|
|
|
mergeUpdate: (user_id, project_id, path, updateRequest, source, callback = (error) ->)->
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, path:path, "merging update from tpds"
|
2018-02-14 10:12:46 -05:00
|
|
|
FileWriter.writeStreamToDisk project_id, updateRequest, (err, fsPath)->
|
2018-01-29 11:08:41 -05:00
|
|
|
return callback(err) if err?
|
2018-02-01 10:31:42 -05:00
|
|
|
UpdateMerger._mergeUpdate user_id, project_id, path, fsPath, source, (mergeErr) ->
|
|
|
|
fs.unlink fsPath, (deleteErr) ->
|
|
|
|
if deleteErr?
|
|
|
|
logger.err project_id:project_id, fsPath:fsPath, "error deleting file"
|
|
|
|
callback mergeErr
|
2018-01-29 06:47:01 -05:00
|
|
|
|
2018-10-23 09:05:50 -04:00
|
|
|
_determineFileType: (project_id, path, fsPath, callback = (err, fileType) ->) ->
|
|
|
|
ProjectEntityHandler.getAllEntities project_id, (err, docs, files) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if _.some(files, (f) -> f.path is path)
|
|
|
|
return callback(null, "existing-file")
|
|
|
|
if _.some(docs, (d) -> d.path is path)
|
|
|
|
return callback(null, "existing-doc")
|
|
|
|
# existing file not found in project, fall back to extension check
|
|
|
|
FileTypeManager.isBinary path, fsPath, (err, isFile)->
|
|
|
|
return callback(err) if err?
|
|
|
|
if isFile
|
|
|
|
callback(null, "new-file") # extension was not text
|
|
|
|
else
|
|
|
|
callback(null, "new-doc")
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_mergeUpdate: (user_id, project_id, path, fsPath, source, callback = (error) ->)->
|
2018-10-23 09:05:50 -04:00
|
|
|
UpdateMerger._determineFileType project_id, path, fsPath, (err, fileType)->
|
2018-02-01 10:31:42 -05:00
|
|
|
return callback(err) if err?
|
2018-10-23 09:05:50 -04:00
|
|
|
if fileType in ["existing-file", "new-file"]
|
2018-02-01 10:31:42 -05:00
|
|
|
UpdateMerger.p.processFile project_id, fsPath, path, source, user_id, callback
|
2018-10-23 09:05:50 -04:00
|
|
|
else if fileType in ["existing-doc", "new-doc"]
|
2018-02-01 10:31:42 -05:00
|
|
|
UpdateMerger.p.processDoc project_id, user_id, fsPath, path, source, callback
|
2018-10-23 09:05:50 -04:00
|
|
|
else
|
|
|
|
callback new Error("unrecognized file")
|
2018-01-29 06:47:01 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
deleteUpdate: (user_id, project_id, path, source, callback = () ->)->
|
|
|
|
EditorController.deleteEntityWithPath project_id, path, source, user_id, () ->
|
|
|
|
logger.log project_id:project_id, path:path, "finished processing update to delete entity from tpds"
|
|
|
|
callback()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
p:
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
processDoc: (project_id, user_id, fsPath, path, source, callback)->
|
|
|
|
UpdateMerger.p.readFileIntoTextArray fsPath, (err, docLines)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if err?
|
2018-02-01 10:31:42 -05:00
|
|
|
logger.err project_id:project_id, "error reading file into text array for process doc update"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(err)
|
2018-02-01 10:31:42 -05:00
|
|
|
logger.log docLines:docLines, "processing doc update from tpds"
|
|
|
|
EditorController.upsertDocWithPath project_id, path, docLines, source, user_id, (err) ->
|
|
|
|
logger.log project_id:project_id, "completed processing file update from tpds"
|
|
|
|
callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
processFile: (project_id, fsPath, path, source, user_id, callback)->
|
|
|
|
logger.log project_id:project_id, "processing file update from tpds"
|
2018-02-14 10:12:46 -05:00
|
|
|
EditorController.upsertFileWithPath project_id, path, fsPath, null, source, user_id, (err) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
logger.log project_id:project_id, "completed processing file update from tpds"
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(err)
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
readFileIntoTextArray: (path, callback)->
|
|
|
|
fs.readFile path, "utf8", (error, content = "") ->
|
|
|
|
if error?
|
|
|
|
logger.err path:path, "error reading file into text array"
|
|
|
|
return callback(error)
|
|
|
|
lines = content.split(/\r\n|\n|\r/)
|
|
|
|
callback error, lines
|