push doc pathname logic into DocController

This commit is contained in:
Hayden Faulds 2018-04-06 14:50:56 +01:00
parent 2d3f169c49
commit 50686090c8
4 changed files with 37 additions and 59 deletions

View file

@ -1,3 +1,4 @@
ProjectLocator = require "../Project/ProjectLocator"
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
ProjectEntityUpdateHandler = require "../Project/ProjectEntityUpdateHandler"
logger = require("logger-sharelatex")
@ -8,21 +9,25 @@ module.exports =
doc_id = req.params.doc_id
plain = req?.query?.plain == 'true'
logger.log doc_id:doc_id, project_id:project_id, "receiving get document request from api (docupdater)"
ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, (error, lines, rev, version, ranges, pathname) ->
ProjectLocator.findElement {project_id: project_id, element_id: doc_id, type: 'doc'}, (error, doc, path) =>
if error?
logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument"
return next(error)
if plain
res.type "text/plain"
res.send lines.join('\n')
else
res.type "json"
res.send JSON.stringify {
lines: lines
version: version
ranges: ranges
pathname: pathname
}
ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, rev, version, ranges) ->
if error?
logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding doc contents for getDocument"
return next(error)
if plain
res.type "text/plain"
res.send lines.join('\n')
else
res.type "json"
res.send JSON.stringify {
lines: lines
version: version
ranges: ranges
pathname: path.fileSystem
}
setDocument: (req, res, next = (error) ->) ->
project_id = req.params.Project_id

View file

@ -6,7 +6,6 @@ DocstoreManager = require "../Docstore/DocstoreManager"
DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
Errors = require '../Errors/Errors'
Project = require('../../models/Project').Project
ProjectLocator = require('./ProjectLocator')
ProjectGetter = require "./ProjectGetter"
TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
@ -105,14 +104,7 @@ module.exports = ProjectEntityHandler = self =
callback = options
options = {}
if options["pathname"]
delete options["pathname"]
ProjectLocator.findElement {project_id: project_id, element_id: doc_id, type: 'doc'}, (error, doc, path) =>
return callback(error) if error?
DocstoreManager.getDoc project_id, doc_id, options, (error, lines, rev, version, ranges) =>
callback(error, lines, rev, version, ranges, path.fileSystem)
else
DocstoreManager.getDoc project_id, doc_id, options, callback
DocstoreManager.getDoc project_id, doc_id, options, callback
_getAllFolders: (project_id, callback) ->
logger.log project_id:project_id, "getting all folders for project"

View file

@ -15,6 +15,7 @@ describe "DocumentController", ->
"logger-sharelatex":
log:->
err:->
"../Project/ProjectLocator": @ProjectLocator = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
@res = new MockResponse()
@ -36,12 +37,19 @@ describe "DocumentController", ->
describe "when the document exists", ->
beforeEach ->
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @doc_lines, @rev, @version, @ranges, @pathname)
@doc = _id: @doc_id
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
@DocumentController.getDocument(@req, @res, @next)
it "should get the document from Mongo", ->
it "should get the pathname of the document", ->
@ProjectLocator.findElement
.calledWith({project_id: @project_id, element_id: @doc_id, type: 'doc'})
.should.equal true
it "should get the document content", ->
@ProjectEntityHandler.getDoc
.calledWith(@project_id, @doc_id, pathname: true)
.calledWith(@project_id, @doc_id)
.should.equal true
it "should return the document data to the client as JSON", ->
@ -54,7 +62,7 @@ describe "DocumentController", ->
describe "when the document doesn't exist", ->
beforeEach ->
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, new Errors.NotFoundError("not found"), null)
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"))
@DocumentController.getDocument(@req, @res, @next)
it "should call next with the NotFoundError", ->
@ -94,7 +102,3 @@ describe "DocumentController", ->
it "should call next with the NotFoundError", ->
@next.calledWith(new Errors.NotFoundError("not found"))
.should.equal true

View file

@ -241,35 +241,12 @@ describe 'ProjectEntityHandler', ->
@ranges = {"mock": "ranges"}
@DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
@ProjectEntityHandler.getDoc project_id, doc_id, @callback
describe 'without pathname option', ->
beforeEach ->
@ProjectEntityHandler.getDoc project_id, doc_id, @callback
it "should call the docstore", ->
@DocstoreManager.getDoc
.calledWith(project_id, doc_id)
.should.equal true
it "should call the docstore", ->
@DocstoreManager.getDoc
.calledWith(project_id, doc_id)
.should.equal true
it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
describe 'with pathname option', ->
beforeEach ->
@project = 'a project'
@path = mongo: "mongo.path", fileSystem: "/file/system/path"
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, {}, @path)
@ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, @callback
it "should call the project locator", ->
@ProjectLocator.findElement
.calledWith({project_id: project_id, element_id: doc_id, type: 'doc'})
.should.equal true
it "should call the docstore", ->
@DocstoreManager.getDoc
.calledWith(project_id, doc_id)
.should.equal true
it "should return the pathname if option given", ->
@callback.calledWith(null, @lines, @rev, @version, @ranges, @path.fileSystem).should.equal true
it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true