2014-04-28 11:45:59 -04:00
|
|
|
DocManager = require "./DocManager"
|
|
|
|
logger = require "logger-sharelatex"
|
2015-06-02 15:29:32 -04:00
|
|
|
DocArchive = require "./DocArchiveManager"
|
2015-10-16 05:13:54 -04:00
|
|
|
HealthChecker = require "./HealthChecker"
|
|
|
|
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
module.exports = HttpController =
|
|
|
|
getDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
2014-04-29 06:49:09 -04:00
|
|
|
doc_id = req.params.doc_id
|
2015-02-03 09:05:08 -05:00
|
|
|
include_deleted = req.query?.include_deleted == "true"
|
2014-04-28 11:45:59 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "getting doc"
|
2016-11-30 10:08:56 -05:00
|
|
|
DocManager.getDoc project_id, doc_id, {version: true}, (error, doc) ->
|
2014-04-28 11:45:59 -04:00
|
|
|
return next(error) if error?
|
2014-06-05 08:29:50 -04:00
|
|
|
logger.log doc: doc, "got doc"
|
2014-04-28 11:45:59 -04:00
|
|
|
if !doc?
|
|
|
|
res.send 404
|
2015-02-03 09:05:08 -05:00
|
|
|
else if doc.deleted && !include_deleted
|
|
|
|
res.send 404
|
2014-04-28 11:45:59 -04:00
|
|
|
else
|
2014-04-30 08:06:12 -04:00
|
|
|
res.json HttpController._buildDocView(doc)
|
|
|
|
|
2014-05-20 08:04:33 -04:00
|
|
|
getRawDoc: (req, res, next = (error)->)->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
|
2016-11-30 10:08:56 -05:00
|
|
|
DocManager.getDoc project_id, doc_id, {version: false}, (error, doc) ->
|
2014-05-20 08:04:33 -04:00
|
|
|
return next(error) if error?
|
|
|
|
if !doc?
|
|
|
|
res.send 404
|
|
|
|
else
|
|
|
|
res.setHeader('content-type', 'text/plain')
|
|
|
|
res.send HttpController._buildRawDocView(doc)
|
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
getAllDocs: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
logger.log project_id: project_id, "getting all docs"
|
2016-12-05 11:31:51 -05:00
|
|
|
DocManager.getAllNonDeletedDocs project_id, (error, docs = []) ->
|
2014-04-30 08:06:12 -04:00
|
|
|
return next(error) if error?
|
2014-05-07 11:24:47 -04:00
|
|
|
docViews = []
|
|
|
|
for doc in docs
|
|
|
|
if doc? # There can end up being null docs for some reason :( (probably a race condition)
|
|
|
|
docViews.push HttpController._buildDocView(doc)
|
2014-05-07 11:38:10 -04:00
|
|
|
else
|
|
|
|
logger.error err: new Error("null doc"), project_id: project_id, "encountered null doc"
|
2014-05-07 11:24:47 -04:00
|
|
|
res.json docViews
|
2014-04-29 06:49:09 -04:00
|
|
|
|
|
|
|
updateDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
lines = req.body?.lines
|
2016-11-28 09:55:16 -05:00
|
|
|
version = req.body?.version
|
2016-12-05 09:21:49 -05:00
|
|
|
ranges = req.body?.ranges
|
2014-04-29 06:49:09 -04:00
|
|
|
|
|
|
|
if !lines? or lines not instanceof Array
|
|
|
|
logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
|
|
|
|
res.send 400 # Bad Request
|
|
|
|
return
|
2016-12-02 10:17:38 -05:00
|
|
|
|
|
|
|
if !version? or typeof version is not "number"
|
|
|
|
logger.error project_id: project_id, doc_id: doc_id, "no doc version provided"
|
|
|
|
res.send 400 # Bad Request
|
|
|
|
return
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2015-01-20 08:46:57 -05:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "got http request to update doc"
|
2016-12-05 09:21:49 -05:00
|
|
|
DocManager.updateDoc project_id, doc_id, lines, version, ranges, (error, modified, rev) ->
|
2014-04-29 06:49:09 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.json {
|
|
|
|
modified: modified
|
2014-05-08 10:43:08 -04:00
|
|
|
rev: rev
|
2014-04-29 11:36:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
|
|
|
|
DocManager.deleteDoc project_id, doc_id, (error) ->
|
|
|
|
return next(error) if error?
|
2014-04-30 08:06:12 -04:00
|
|
|
res.send 204
|
|
|
|
|
|
|
|
_buildDocView: (doc) ->
|
2016-11-28 09:55:16 -05:00
|
|
|
doc_view = {
|
2014-05-29 07:04:18 -04:00
|
|
|
_id: doc._id?.toString()
|
2014-04-30 08:06:12 -04:00
|
|
|
lines: doc.lines
|
|
|
|
rev: doc.rev
|
2014-06-05 08:29:50 -04:00
|
|
|
deleted: !!doc.deleted
|
2014-05-20 08:04:33 -04:00
|
|
|
}
|
2016-11-28 09:55:16 -05:00
|
|
|
if doc.version?
|
|
|
|
doc_view.version = doc.version
|
2016-12-05 09:21:49 -05:00
|
|
|
if doc.ranges?
|
|
|
|
doc_view.ranges = doc.ranges
|
2016-11-28 09:55:16 -05:00
|
|
|
return doc_view
|
2014-05-20 08:04:33 -04:00
|
|
|
|
|
|
|
_buildRawDocView: (doc)->
|
2014-05-29 06:18:31 -04:00
|
|
|
return (doc?.lines or []).join("\n")
|
2015-06-01 17:24:40 -04:00
|
|
|
|
|
|
|
archiveAllDocs: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
logger.log project_id: project_id, "archiving all docs"
|
2015-06-02 14:55:22 -04:00
|
|
|
DocArchive.archiveAllDocs project_id, (error) ->
|
2015-06-01 17:24:40 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.send 204
|
2015-08-13 17:48:45 -04:00
|
|
|
|
|
|
|
unArchiveAllDocs: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
logger.log project_id: project_id, "unarchiving all docs"
|
|
|
|
DocArchive.unArchiveAllDocs project_id, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.send 200
|
|
|
|
|
2015-10-16 05:13:54 -04:00
|
|
|
healthCheck: (req, res)->
|
|
|
|
HealthChecker.check (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error performing health check"
|
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.send 200
|
|
|
|
|