overleaf/services/docstore/app/coffee/DocManager.coffee

73 lines
2.7 KiB
CoffeeScript
Raw Normal View History

MongoManager = require "./MongoManager"
2014-04-28 16:43:19 +00:00
Errors = require "./Errors"
logger = require "logger-sharelatex"
_ = require "underscore"
async = require "async"
2015-06-01 21:24:40 +00:00
settings = require("settings-sharelatex")
request = require("request")
crypto = require("crypto")
thirtySeconds = 30 * 1000
2015-06-02 19:29:32 +00:00
DocArchive = require "./DocArchiveManager"
module.exports = DocManager =
getDoc: (project_id, doc_id, callback = (error, doc) ->) ->
MongoManager.findDoc doc_id, (err, doc)->
if err?
return callback(err)
else if !doc?
return callback new Errors.NotFoundError("No such doc: #{doc_id} in project #{project_id}")
else if doc?.inS3
2015-06-02 18:55:22 +00:00
DocArchive.unarchiveDoc project_id, doc_id, (err)->
if err?
2015-08-13 12:59:12 +00:00
logger.err err:err, project_id:project_id, doc_id:doc_id, "error unarchiving doc"
return callback(err)
MongoManager.findDoc doc_id, callback
else
callback err, doc
2014-04-28 16:43:19 +00:00
2014-04-30 12:06:12 +00:00
getAllDocs: (project_id, callback = (error, docs) ->) ->
2015-06-02 18:55:22 +00:00
DocArchive.unArchiveAllDocs project_id, (error) ->
2015-06-01 22:36:26 +00:00
MongoManager.getProjectsDocs project_id, (error, docs) ->
if err?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
else
return callback(null, docs)
2014-04-30 12:06:12 +00:00
2014-05-16 12:06:35 +00:00
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
2015-06-02 18:13:28 +00:00
DocManager.getDoc project_id, doc_id, (err, doc)->
if err? and !(err instanceof Errors.NotFoundError)
logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update"
return callback(err)
2014-04-28 16:43:19 +00:00
isNewDoc = lines.length == 0
linesAreSame = _.isEqual(doc?.lines, lines)
if linesAreSame and !isNewDoc
logger.log project_id: project_id, doc_id: doc_id, rev: doc?.rev, "doc lines have not changed - not updating"
return callback null, false, doc?.rev
else
oldRev = doc?.rev || 0
logger.log {
project_id: project_id
doc_id: doc_id,
oldDocLines: doc?.lines
newDocLines: lines
rev: oldRev
}, "updating doc lines"
2015-02-27 14:06:06 +00:00
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, (error)->
return callback(callback) if error?
callback null, true, oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
2014-04-29 14:07:22 +00:00
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc to delete: #{project_id}/#{doc_id}") if !doc?
2015-02-27 14:06:06 +00:00
MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, (error) ->
2015-01-21 12:31:06 +00:00
return callback(error) if error?
MongoManager.markDocAsDeleted doc_id, (error) ->
return callback(error) if error?
callback()