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

View file

@ -6,7 +6,6 @@ DocstoreManager = require "../Docstore/DocstoreManager"
DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler') DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
Errors = require '../Errors/Errors' Errors = require '../Errors/Errors'
Project = require('../../models/Project').Project Project = require('../../models/Project').Project
ProjectLocator = require('./ProjectLocator')
ProjectGetter = require "./ProjectGetter" ProjectGetter = require "./ProjectGetter"
TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender') TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
@ -105,14 +104,7 @@ module.exports = ProjectEntityHandler = self =
callback = options callback = options
options = {} options = {}
if options["pathname"] DocstoreManager.getDoc project_id, doc_id, options, callback
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
_getAllFolders: (project_id, callback) -> _getAllFolders: (project_id, callback) ->
logger.log project_id:project_id, "getting all folders for project" logger.log project_id:project_id, "getting all folders for project"

View file

@ -15,6 +15,7 @@ describe "DocumentController", ->
"logger-sharelatex": "logger-sharelatex":
log:-> log:->
err:-> err:->
"../Project/ProjectLocator": @ProjectLocator = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {} "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {} "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
@res = new MockResponse() @res = new MockResponse()
@ -36,12 +37,19 @@ describe "DocumentController", ->
describe "when the document exists", -> describe "when the document exists", ->
beforeEach -> 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) @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 @ProjectEntityHandler.getDoc
.calledWith(@project_id, @doc_id, pathname: true) .calledWith(@project_id, @doc_id)
.should.equal true .should.equal true
it "should return the document data to the client as JSON", -> it "should return the document data to the client as JSON", ->
@ -54,7 +62,7 @@ describe "DocumentController", ->
describe "when the document doesn't exist", -> describe "when the document doesn't exist", ->
beforeEach -> 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) @DocumentController.getDocument(@req, @res, @next)
it "should call next with the NotFoundError", -> it "should call next with the NotFoundError", ->
@ -94,7 +102,3 @@ describe "DocumentController", ->
it "should call next with the NotFoundError", -> it "should call next with the NotFoundError", ->
@next.calledWith(new Errors.NotFoundError("not found")) @next.calledWith(new Errors.NotFoundError("not found"))
.should.equal true .should.equal true

View file

@ -241,35 +241,12 @@ describe 'ProjectEntityHandler', ->
@ranges = {"mock": "ranges"} @ranges = {"mock": "ranges"}
@DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges) @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
@ProjectEntityHandler.getDoc project_id, doc_id, @callback
describe 'without pathname option', -> it "should call the docstore", ->
beforeEach -> @DocstoreManager.getDoc
@ProjectEntityHandler.getDoc project_id, doc_id, @callback .calledWith(project_id, doc_id)
.should.equal true
it "should call the docstore", -> it "should call the callback with the lines, version and rev", ->
@DocstoreManager.getDoc @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
.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