2014-04-28 11:45:59 -04:00
|
|
|
MongoManager = require "./MongoManager"
|
2014-04-28 12:43:19 -04:00
|
|
|
Errors = require "./Errors"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
_ = require "underscore"
|
2015-01-20 12:09:51 -05:00
|
|
|
async = require "async"
|
2015-06-01 17:24:40 -04:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
crypto = require("crypto")
|
|
|
|
thirtySeconds = 30 * 1000
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
module.exports = DocManager =
|
2015-02-20 09:28:16 -05:00
|
|
|
|
|
|
|
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}")
|
2015-06-02 07:36:38 -04:00
|
|
|
else if doc?.inS3
|
|
|
|
DocManager.unarchiveDoc project_id, doc_id, (err)->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
|
|
|
MongoManager.findDoc doc_id, callback
|
|
|
|
else
|
|
|
|
callback err, doc
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
getAllDocs: (project_id, callback = (error, docs) ->) ->
|
2015-06-01 18:36:26 -04:00
|
|
|
DocManager.unArchiveAllDocs project_id, (error) ->
|
|
|
|
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 08:06:12 -04:00
|
|
|
|
2014-05-16 08:06:35 -04:00
|
|
|
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
|
2015-06-02 07:36:38 -04:00
|
|
|
DocManager.getDoc doc_id, (err, doc)->
|
|
|
|
if err? and !(error instanceof Errors.NotFoundError)
|
2015-02-26 11:00:28 -05:00
|
|
|
logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update"
|
|
|
|
return callback(err)
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2015-02-27 09:50:08 -05:00
|
|
|
isNewDoc = lines.length == 0
|
2015-02-27 09:58:38 -05:00
|
|
|
linesAreSame = _.isEqual(doc?.lines, lines)
|
|
|
|
|
2015-02-27 09:50:08 -05:00
|
|
|
if linesAreSame and !isNewDoc
|
2015-02-20 09:28:16 -05:00
|
|
|
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
|
2015-02-26 11:00:28 -05:00
|
|
|
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 09:06:06 -05:00
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, (error)->
|
2015-02-26 11:00:28 -05:00
|
|
|
return callback(callback) if error?
|
|
|
|
callback null, true, oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2014-04-29 10:07:22 -04:00
|
|
|
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
|
|
|
return callback(error) if error?
|
2015-01-22 10:05:48 -05:00
|
|
|
return callback new Errors.NotFoundError("No such project/doc to delete: #{project_id}/#{doc_id}") if !doc?
|
2015-02-27 09:06:06 -05:00
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, (error) ->
|
2015-01-21 07:31:06 -05:00
|
|
|
return callback(error) if error?
|
2015-01-20 12:09:51 -05:00
|
|
|
MongoManager.markDocAsDeleted doc_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2015-06-02 07:36:38 -04:00
|
|
|
|
|
|
|
#DOC ARCHIVER
|
2015-06-01 17:24:40 -04:00
|
|
|
archiveAllDocs: (project_id, callback = (error, docs) ->) ->
|
|
|
|
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}")
|
2015-06-02 05:59:35 -04:00
|
|
|
jobs = _.map docs, (doc) ->
|
2015-06-02 07:36:38 -04:00
|
|
|
(cb)-> DocManager.archiveDoc project_id, doc, cb
|
2015-06-01 18:36:26 -04:00
|
|
|
async.series jobs, callback
|
|
|
|
|
2015-06-02 07:36:38 -04:00
|
|
|
|
|
|
|
archiveDoc: (project_id, doc, callback)->
|
|
|
|
logger.log project_id: project_id, doc_id: doc._id, "sending doc to s3"
|
|
|
|
options = buildS3Options(doc.lines, project_id+"/"+doc._id)
|
|
|
|
request.put options, (err, res)->
|
|
|
|
if err? || res.statusCode != 200
|
|
|
|
logger.err err:err, res:res, "something went wrong archiving doc in aws"
|
|
|
|
callback(err)
|
2015-06-02 07:39:49 -04:00
|
|
|
MongoManager.markDocAsArchived doc._id, doc.rev, (error) ->
|
2015-06-02 07:36:38 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
2015-06-01 18:36:26 -04:00
|
|
|
unArchiveAllDocs: (project_id, callback = (error) ->) ->
|
|
|
|
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}")
|
2015-06-02 05:59:35 -04:00
|
|
|
jobs = _.map docs, (doc) ->
|
|
|
|
(cb)->
|
2015-06-01 18:36:26 -04:00
|
|
|
if !doc.inS3?
|
2015-06-02 05:59:35 -04:00
|
|
|
return cb()
|
2015-06-01 18:36:26 -04:00
|
|
|
else
|
2015-06-02 07:36:38 -04:00
|
|
|
unarchiveDoc project_id, doc_id, cb
|
2015-06-01 17:24:40 -04:00
|
|
|
async.series jobs, callback
|
|
|
|
|
2015-06-02 07:36:38 -04:00
|
|
|
unarchiveDoc: (project_id, doc_id, callback)->
|
|
|
|
logger.log project_id: project_id, doc_id: doc._id, "getting doc from s3"
|
|
|
|
options = buildS3Options(true, project_id+"/"+doc._id)
|
|
|
|
request.get options, (err, res, lines)->
|
|
|
|
if err? || res.statusCode != 200
|
|
|
|
logger.err err:err, res:res, "something went wrong unarchiving doc from aws"
|
|
|
|
callback(err)
|
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc._id.toString(), lines, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
2015-06-01 18:36:26 -04:00
|
|
|
|
2015-06-01 17:24:40 -04:00
|
|
|
buildS3Options = (content, key)->
|
|
|
|
return {
|
|
|
|
aws:
|
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
|
|
|
bucket: settings.filestore.stores.user_files
|
|
|
|
timeout: thirtySeconds
|
|
|
|
json: content
|
|
|
|
#headers:
|
|
|
|
# 'content-md5': crypto.createHash("md5").update(content).digest("hex")
|
|
|
|
uri:"https://#{settings.filestore.stores.user_files}.s3.amazonaws.com/#{key}"
|
|
|
|
}
|