mirror of
https://github.com/overleaf/overleaf.git
synced 2024-09-16 02:52:31 -04:00
use ProjectGetter in ProjectLocator.findElementByPath
This commit is contained in:
parent
cbc6fb232d
commit
95b82a3c9d
6 changed files with 80 additions and 72 deletions
|
@ -75,7 +75,7 @@ module.exports = ProjectEntityMongoUpdateHandler = self =
|
||||||
if parentFolder?
|
if parentFolder?
|
||||||
parentFolder_id = parentFolder._id
|
parentFolder_id = parentFolder._id
|
||||||
builtUpPath = "#{builtUpPath}/#{folderName}"
|
builtUpPath = "#{builtUpPath}/#{folderName}"
|
||||||
ProjectLocator.findElementByPath project, builtUpPath, (err, foundFolder)=>
|
ProjectLocator.findElementByPath project: project, path: builtUpPath, (err, foundFolder)=>
|
||||||
if !foundFolder?
|
if !foundFolder?
|
||||||
logger.log path:path, project_id:project._id, folderName:folderName, "making folder from mkdirp"
|
logger.log path:path, project_id:project._id, folderName:folderName, "making folder from mkdirp"
|
||||||
self.addFolder.withoutLock project_id, parentFolder_id, folderName, (err, newFolder, parentFolder_id)->
|
self.addFolder.withoutLock project_id, parentFolder_id, folderName, (err, newFolder, parentFolder_id)->
|
||||||
|
|
|
@ -267,7 +267,7 @@ module.exports = ProjectEntityUpdateHandler = self =
|
||||||
callback null, entity_id
|
callback null, entity_id
|
||||||
|
|
||||||
deleteEntityWithPath: wrapWithLock (project_id, path, userId, callback) ->
|
deleteEntityWithPath: wrapWithLock (project_id, path, userId, callback) ->
|
||||||
ProjectLocator.findElementByPath project_id, path, (err, element, type)->
|
ProjectLocator.findElementByPath project_id: project_id, path: path, (err, element, type)->
|
||||||
return callback(err) if err?
|
return callback(err) if err?
|
||||||
return callback(new Errors.NotFoundError("project not found")) if !element?
|
return callback(new Errors.NotFoundError("project not found")) if !element?
|
||||||
self.deleteEntity.withoutLock project_id, element._id, type, userId, callback
|
self.deleteEntity.withoutLock project_id, element._id, type, userId, callback
|
||||||
|
|
|
@ -4,7 +4,6 @@ Errors = require "../Errors/Errors"
|
||||||
_ = require('underscore')
|
_ = require('underscore')
|
||||||
logger = require('logger-sharelatex')
|
logger = require('logger-sharelatex')
|
||||||
async = require('async')
|
async = require('async')
|
||||||
ProjectGetter = require "./ProjectGetter"
|
|
||||||
|
|
||||||
module.exports = ProjectLocator =
|
module.exports = ProjectLocator =
|
||||||
findElement: (options, _callback = (err, element, path, parentFolder)->)->
|
findElement: (options, _callback = (err, element, path, parentFolder)->)->
|
||||||
|
@ -83,8 +82,19 @@ module.exports = ProjectLocator =
|
||||||
else
|
else
|
||||||
getRootDoc project
|
getRootDoc project
|
||||||
|
|
||||||
findElementByPath: (project_or_id, needlePath, callback = (err, foundEntity, type)->)->
|
findElementByPath: (options, callback = (err, foundEntity, type)->)->
|
||||||
|
{project, project_id, path} = options
|
||||||
|
if !path?
|
||||||
|
return new Error('no path provided for findElementByPath')
|
||||||
|
|
||||||
|
if project?
|
||||||
|
ProjectLocator._findElementByPathWithProject project, path, callback
|
||||||
|
else
|
||||||
|
ProjectGetter.getProject project_id, {rootFolder:true, rootDoc_id:true}, (err, project)->
|
||||||
|
return callback(err) if err?
|
||||||
|
ProjectLocator._findElementByPathWithProject project, path, callback
|
||||||
|
|
||||||
|
_findElementByPathWithProject: (project, needlePath, callback = (err, foundEntity, type)->)->
|
||||||
getParentFolder = (haystackFolder, foldersList, level, cb)->
|
getParentFolder = (haystackFolder, foldersList, level, cb)->
|
||||||
if foldersList.length == 0
|
if foldersList.length == 0
|
||||||
return cb null, haystackFolder
|
return cb null, haystackFolder
|
||||||
|
@ -98,7 +108,7 @@ module.exports = ProjectLocator =
|
||||||
else
|
else
|
||||||
return getParentFolder(folder, foldersList, level+1, cb)
|
return getParentFolder(folder, foldersList, level+1, cb)
|
||||||
if !found
|
if !found
|
||||||
cb("not found project_or_id: #{project_or_id} search path: #{needlePath}, folder #{foldersList[level]} could not be found")
|
cb("not found project: #{project._id} search path: #{needlePath}, folder #{foldersList[level]} could not be found")
|
||||||
|
|
||||||
getEntity = (folder, entityName, cb)->
|
getEntity = (folder, entityName, cb)->
|
||||||
if !entityName?
|
if !entityName?
|
||||||
|
@ -119,15 +129,14 @@ module.exports = ProjectLocator =
|
||||||
if result?
|
if result?
|
||||||
cb null, result, type
|
cb null, result, type
|
||||||
else
|
else
|
||||||
cb("not found project_or_id: #{project_or_id} search path: #{needlePath}, entity #{entityName} could not be found")
|
cb("not found project: #{project._id} search path: #{needlePath}, entity #{entityName} could not be found")
|
||||||
|
|
||||||
|
|
||||||
Project.getProject project_or_id, "", (err, project)->
|
|
||||||
if err?
|
if err?
|
||||||
logger.err err:err, project_or_id:project_or_id, "error getting project for finding element"
|
logger.err err:err, project_id:project._id, "error getting project for finding element"
|
||||||
return callback(err)
|
return callback(err)
|
||||||
if !project?
|
if !project?
|
||||||
return callback("project could not be found for finding a element #{project_or_id}")
|
return callback("project could not be found for finding a element #{project._id}")
|
||||||
if needlePath == '' || needlePath == '/'
|
if needlePath == '' || needlePath == '/'
|
||||||
return callback(null, project.rootFolder[0], "folder")
|
return callback(null, project.rootFolder[0], "folder")
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,8 @@ describe 'ProjectEntityMongoUpdateHandler', ->
|
||||||
@project = _id: project_id, rootFolder: [@rootFolder]
|
@project = _id: project_id, rootFolder: [@rootFolder]
|
||||||
|
|
||||||
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
|
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
|
||||||
@ProjectLocator.findElementByPath = (project_id, path, cb) =>
|
@ProjectLocator.findElementByPath = (options, cb) =>
|
||||||
|
{path} = options
|
||||||
@parentFolder = {_id:"parentFolder_id_here"}
|
@parentFolder = {_id:"parentFolder_id_here"}
|
||||||
lastFolder = path.substring(path.lastIndexOf("/"))
|
lastFolder = path.substring(path.lastIndexOf("/"))
|
||||||
if lastFolder.indexOf("level1") == -1
|
if lastFolder.indexOf("level1") == -1
|
||||||
|
|
|
@ -643,7 +643,7 @@ describe 'ProjectEntityUpdateHandler', ->
|
||||||
|
|
||||||
it 'finds the entity', ->
|
it 'finds the entity', ->
|
||||||
@ProjectLocator.findElementByPath
|
@ProjectLocator.findElementByPath
|
||||||
.calledWith(project_id, @path)
|
.calledWith({project_id, @path})
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
it 'deletes the entity', ->
|
it 'deletes the entity', ->
|
||||||
|
|
|
@ -33,9 +33,6 @@ project.rootDoc_id = rootDoc._id
|
||||||
describe 'ProjectLocator', ->
|
describe 'ProjectLocator', ->
|
||||||
|
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
Project.getProject = (project_id, fields, callback)=>
|
|
||||||
callback(null, project)
|
|
||||||
|
|
||||||
Project.findById = (project_id, callback)=>
|
Project.findById = (project_id, callback)=>
|
||||||
callback(null, project)
|
callback(null, project)
|
||||||
@ProjectGetter =
|
@ProjectGetter =
|
||||||
|
@ -181,68 +178,71 @@ describe 'ProjectLocator', ->
|
||||||
|
|
||||||
it 'should take a doc path and return the element for a root level document', (done)->
|
it 'should take a doc path and return the element for a root level document', (done)->
|
||||||
path = "#{doc1.name}"
|
path = "#{doc1.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal doc1
|
element.should.deep.equal doc1
|
||||||
expect(type).to.equal "doc"
|
expect(type).to.equal "doc"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
|
it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
|
||||||
path = "/#{doc1.name}"
|
path = "/#{doc1.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal doc1
|
element.should.deep.equal doc1
|
||||||
expect(type).to.equal "doc"
|
expect(type).to.equal "doc"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a doc path and return the element for a nested document', (done)->
|
it 'should take a doc path and return the element for a nested document', (done)->
|
||||||
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
|
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal subSubDoc
|
element.should.deep.equal subSubDoc
|
||||||
expect(type).to.equal "doc"
|
expect(type).to.equal "doc"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a file path and return the element for a root level document', (done)->
|
it 'should take a file path and return the element for a root level document', (done)->
|
||||||
path = "#{file1.name}"
|
path = "#{file1.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal file1
|
element.should.deep.equal file1
|
||||||
expect(type).to.equal "file"
|
expect(type).to.equal "file"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a file path and return the element for a nested document', (done)->
|
it 'should take a file path and return the element for a nested document', (done)->
|
||||||
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
|
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal subSubFile
|
element.should.deep.equal subSubFile
|
||||||
expect(type).to.equal "file"
|
expect(type).to.equal "file"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a file path and return the element for a nested document case insenstive', (done)->
|
it 'should take a file path and return the element for a nested document case insenstive', (done)->
|
||||||
path = "#{subFolder.name.toUpperCase()}/#{secondSubFolder.name.toUpperCase()}/#{subSubFile.name.toUpperCase()}"
|
path = "#{subFolder.name.toUpperCase()}/#{secondSubFolder.name.toUpperCase()}/#{subSubFile.name.toUpperCase()}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal subSubFile
|
element.should.deep.equal subSubFile
|
||||||
expect(type).to.equal "file"
|
expect(type).to.equal "file"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a file path and return the element for a nested folder', (done)->
|
it 'should take a file path and return the element for a nested folder', (done)->
|
||||||
path = "#{subFolder.name}/#{secondSubFolder.name}"
|
path = "#{subFolder.name}/#{secondSubFolder.name}"
|
||||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal secondSubFolder
|
element.should.deep.equal secondSubFolder
|
||||||
expect(type).to.equal "folder"
|
expect(type).to.equal "folder"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should take a file path and return the root folder', (done)->
|
it 'should take a file path and return the root folder', (done)->
|
||||||
@locator.findElementByPath project._id, "/", (err, element, type)->
|
path = "/"
|
||||||
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
element.should.deep.equal rootFolder
|
element.should.deep.equal rootFolder
|
||||||
expect(type).to.equal "folder"
|
expect(type).to.equal "folder"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should return an error if the file can not be found inside know folder', (done)->
|
it 'should return an error if the file can not be found inside know folder', (done)->
|
||||||
@locator.findElementByPath project._id, "#{subFolder.name}/#{secondSubFolder.name}/exist.txt", (err, element, type)->
|
path = "#{subFolder.name}/#{secondSubFolder.name}/exist.txt"
|
||||||
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
err.should.not.equal undefined
|
err.should.not.equal undefined
|
||||||
assert.equal element, undefined
|
assert.equal element, undefined
|
||||||
expect(type).to.be.undefined
|
expect(type).to.be.undefined
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should return an error if the file can not be found inside unknown folder', (done)->
|
it 'should return an error if the file can not be found inside unknown folder', (done)->
|
||||||
@locator.findElementByPath project._id, "this/does/not/exist.txt", (err, element, type)->
|
path = "this/does/not/exist.txt"
|
||||||
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||||
err.should.not.equal undefined
|
err.should.not.equal undefined
|
||||||
assert.equal element, undefined
|
assert.equal element, undefined
|
||||||
expect(type).to.be.undefined
|
expect(type).to.be.undefined
|
||||||
|
@ -250,7 +250,6 @@ describe 'ProjectLocator', ->
|
||||||
|
|
||||||
|
|
||||||
describe "where duplicate folder exists", ->
|
describe "where duplicate folder exists", ->
|
||||||
|
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{
|
@duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{
|
||||||
name: "1"
|
name: "1"
|
||||||
|
@ -264,17 +263,15 @@ describe 'ProjectLocator', ->
|
||||||
fileRefs: []
|
fileRefs: []
|
||||||
docs: []
|
docs: []
|
||||||
]
|
]
|
||||||
Project.getProject = sinon.stub()
|
|
||||||
Project.getProject.callsArgWith(2, null, @project)
|
|
||||||
|
|
||||||
|
|
||||||
it "should not call the callback more than once", (done)->
|
it "should not call the callback more than once", (done)->
|
||||||
@locator.findElementByPath project._id, "#{@duplicateFolder.name}/#{@doc.name}", ->
|
path = "#{@duplicateFolder.name}/#{@doc.name}"
|
||||||
|
@locator.findElementByPath {@project, path}, ->
|
||||||
done() #mocha will throw exception if done called multiple times
|
done() #mocha will throw exception if done called multiple times
|
||||||
|
|
||||||
|
|
||||||
it "should not call the callback more than once when the path is longer than 1 level below the duplicate level", (done)->
|
it "should not call the callback more than once when the path is longer than 1 level below the duplicate level", (done)->
|
||||||
@locator.findElementByPath project._id, "#{@duplicateFolder.name}/1/main.tex", ->
|
path = "#{@duplicateFolder.name}/1/main.tex"
|
||||||
|
@locator.findElementByPath {@project, path}, ->
|
||||||
done() #mocha will throw exception if done called multiple times
|
done() #mocha will throw exception if done called multiple times
|
||||||
|
|
||||||
describe "with a null doc", ->
|
describe "with a null doc", ->
|
||||||
|
@ -285,33 +282,34 @@ describe 'ProjectLocator', ->
|
||||||
fileRefs: []
|
fileRefs: []
|
||||||
docs: [{name:"main.tex"}, null, {name:"other.tex"}]
|
docs: [{name:"main.tex"}, null, {name:"other.tex"}]
|
||||||
]
|
]
|
||||||
Project.getProject = sinon.stub()
|
|
||||||
Project.getProject.callsArgWith(2, null, @project)
|
|
||||||
|
|
||||||
it "should not crash with a null", (done)->
|
it "should not crash with a null", (done)->
|
||||||
callback = sinon.stub()
|
path = "/other.tex"
|
||||||
@locator.findElementByPath project._id, "/other.tex", (err, element)->
|
@locator.findElementByPath {@project, path}, (err, element)->
|
||||||
element.name.should.equal "other.tex"
|
element.name.should.equal "other.tex"
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
|
||||||
describe "with a null project", ->
|
describe "with a null project", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@project =
|
@ProjectGetter =
|
||||||
rootFolder:[
|
getProject: sinon.stub().callsArg(2)
|
||||||
folders: []
|
|
||||||
fileRefs: []
|
|
||||||
docs: [{name:"main.tex"}, null, {name:"other.tex"}]
|
|
||||||
]
|
|
||||||
Project.getProject = sinon.stub()
|
|
||||||
Project.getProject.callsArgWith(2, null)
|
|
||||||
|
|
||||||
it "should not crash with a null", (done)->
|
it "should not crash with a null", (done)->
|
||||||
callback = sinon.stub()
|
path = "/other.tex"
|
||||||
@locator.findElementByPath project._id, "/other.tex", (err, element)->
|
@locator.findElementByPath {project_id: @project._id, path}, (err, element)->
|
||||||
expect(err).to.exist
|
expect(err).to.exist
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
describe "with a project_id", ->
|
||||||
|
it 'should take a doc path and return the element for a root level document', (done)->
|
||||||
|
path = "#{doc1.name}"
|
||||||
|
@locator.findElementByPath {project_id: project._id, path}, (err, element, type)=>
|
||||||
|
@ProjectGetter.getProject
|
||||||
|
.calledWith(project._id, {rootFolder:true, rootDoc_id: true})
|
||||||
|
.should.equal true
|
||||||
|
element.should.deep.equal doc1
|
||||||
|
expect(type).to.equal "doc"
|
||||||
|
done()
|
||||||
|
|
||||||
describe 'findUsersProjectByName finding a project by user_id and project name', ()->
|
describe 'findUsersProjectByName finding a project by user_id and project name', ()->
|
||||||
it 'should return the project from an array case insenstive', (done)->
|
it 'should return the project from an array case insenstive', (done)->
|
||||||
|
|
Loading…
Reference in a new issue