mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-02 20:46:21 +00: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?
|
||||
parentFolder_id = parentFolder._id
|
||||
builtUpPath = "#{builtUpPath}/#{folderName}"
|
||||
ProjectLocator.findElementByPath project, builtUpPath, (err, foundFolder)=>
|
||||
ProjectLocator.findElementByPath project: project, path: builtUpPath, (err, foundFolder)=>
|
||||
if !foundFolder?
|
||||
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)->
|
||||
|
|
|
@ -267,7 +267,7 @@ module.exports = ProjectEntityUpdateHandler = self =
|
|||
callback null, entity_id
|
||||
|
||||
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(new Errors.NotFoundError("project not found")) if !element?
|
||||
self.deleteEntity.withoutLock project_id, element._id, type, userId, callback
|
||||
|
|
|
@ -4,7 +4,6 @@ Errors = require "../Errors/Errors"
|
|||
_ = require('underscore')
|
||||
logger = require('logger-sharelatex')
|
||||
async = require('async')
|
||||
ProjectGetter = require "./ProjectGetter"
|
||||
|
||||
module.exports = ProjectLocator =
|
||||
findElement: (options, _callback = (err, element, path, parentFolder)->)->
|
||||
|
@ -83,8 +82,19 @@ module.exports = ProjectLocator =
|
|||
else
|
||||
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)->
|
||||
if foldersList.length == 0
|
||||
return cb null, haystackFolder
|
||||
|
@ -98,7 +108,7 @@ module.exports = ProjectLocator =
|
|||
else
|
||||
return getParentFolder(folder, foldersList, level+1, cb)
|
||||
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)->
|
||||
if !entityName?
|
||||
|
@ -119,35 +129,34 @@ module.exports = ProjectLocator =
|
|||
if result?
|
||||
cb null, result, type
|
||||
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?
|
||||
logger.err err:err, project_or_id:project_or_id, "error getting project for finding element"
|
||||
return callback(err)
|
||||
if !project?
|
||||
return callback("project could not be found for finding a element #{project_or_id}")
|
||||
if needlePath == '' || needlePath == '/'
|
||||
return callback(null, project.rootFolder[0], "folder")
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, "error getting project for finding element"
|
||||
return callback(err)
|
||||
if !project?
|
||||
return callback("project could not be found for finding a element #{project._id}")
|
||||
if needlePath == '' || needlePath == '/'
|
||||
return callback(null, project.rootFolder[0], "folder")
|
||||
|
||||
if needlePath.indexOf('/') == 0
|
||||
needlePath = needlePath.substring(1)
|
||||
foldersList = needlePath.split('/')
|
||||
needleName = foldersList.pop()
|
||||
rootFolder = project.rootFolder[0]
|
||||
if needlePath.indexOf('/') == 0
|
||||
needlePath = needlePath.substring(1)
|
||||
foldersList = needlePath.split('/')
|
||||
needleName = foldersList.pop()
|
||||
rootFolder = project.rootFolder[0]
|
||||
|
||||
logger.log project_id:project._id, path:needlePath, foldersList:foldersList, "looking for element by path"
|
||||
jobs = new Array()
|
||||
jobs.push(
|
||||
(cb)->
|
||||
getParentFolder rootFolder, foldersList, 0, cb
|
||||
)
|
||||
jobs.push(
|
||||
(folder, cb)->
|
||||
getEntity folder, needleName, cb
|
||||
)
|
||||
async.waterfall jobs, callback
|
||||
logger.log project_id:project._id, path:needlePath, foldersList:foldersList, "looking for element by path"
|
||||
jobs = new Array()
|
||||
jobs.push(
|
||||
(cb)->
|
||||
getParentFolder rootFolder, foldersList, 0, cb
|
||||
)
|
||||
jobs.push(
|
||||
(folder, cb)->
|
||||
getEntity folder, needleName, cb
|
||||
)
|
||||
async.waterfall jobs, callback
|
||||
|
||||
findUsersProjectByName: (user_id, projectName, callback)->
|
||||
ProjectGetter.findAllUsersProjects user_id, 'name archived', (err, allProjects)->
|
||||
|
|
|
@ -138,7 +138,8 @@ describe 'ProjectEntityMongoUpdateHandler', ->
|
|||
@project = _id: project_id, rootFolder: [@rootFolder]
|
||||
|
||||
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
|
||||
@ProjectLocator.findElementByPath = (project_id, path, cb) =>
|
||||
@ProjectLocator.findElementByPath = (options, cb) =>
|
||||
{path} = options
|
||||
@parentFolder = {_id:"parentFolder_id_here"}
|
||||
lastFolder = path.substring(path.lastIndexOf("/"))
|
||||
if lastFolder.indexOf("level1") == -1
|
||||
|
|
|
@ -643,7 +643,7 @@ describe 'ProjectEntityUpdateHandler', ->
|
|||
|
||||
it 'finds the entity', ->
|
||||
@ProjectLocator.findElementByPath
|
||||
.calledWith(project_id, @path)
|
||||
.calledWith({project_id, @path})
|
||||
.should.equal true
|
||||
|
||||
it 'deletes the entity', ->
|
||||
|
|
|
@ -33,12 +33,9 @@ project.rootDoc_id = rootDoc._id
|
|||
describe 'ProjectLocator', ->
|
||||
|
||||
beforeEach ->
|
||||
Project.getProject = (project_id, fields, callback)=>
|
||||
callback(null, project)
|
||||
|
||||
Project.findById = (project_id, callback)=>
|
||||
callback(null, project)
|
||||
@ProjectGetter =
|
||||
@ProjectGetter =
|
||||
getProject: sinon.stub().callsArgWith(2, null, project)
|
||||
@locator = SandboxedModule.require modulePath, requires:
|
||||
'../../models/Project':{Project:Project}
|
||||
|
@ -162,14 +159,14 @@ describe 'ProjectLocator', ->
|
|||
assert !err?
|
||||
doc._id.should.equal rootDoc._id
|
||||
done()
|
||||
|
||||
|
||||
it 'should return null when the project has no rootDoc', (done) ->
|
||||
project.rootDoc_id = null
|
||||
@locator.findRootDoc project, (err, doc)->
|
||||
assert !err?
|
||||
expect(doc).to.equal null
|
||||
done()
|
||||
|
||||
|
||||
it 'should return null when the rootDoc_id no longer exists', (done) ->
|
||||
project.rootDoc_id = "doesntexist"
|
||||
@locator.findRootDoc project, (err, doc)->
|
||||
|
@ -181,68 +178,71 @@ describe 'ProjectLocator', ->
|
|||
|
||||
it 'should take a doc path and return the element for a root level document', (done)->
|
||||
path = "#{doc1.name}"
|
||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
||||
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||
element.should.deep.equal doc1
|
||||
expect(type).to.equal "doc"
|
||||
done()
|
||||
|
||||
it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
|
||||
path = "/#{doc1.name}"
|
||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
||||
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||
element.should.deep.equal doc1
|
||||
expect(type).to.equal "doc"
|
||||
done()
|
||||
|
||||
|
||||
it 'should take a doc path and return the element for a nested document', (done)->
|
||||
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
|
||||
expect(type).to.equal "doc"
|
||||
done()
|
||||
|
||||
it 'should take a file path and return the element for a root level document', (done)->
|
||||
path = "#{file1.name}"
|
||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
||||
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||
element.should.deep.equal file1
|
||||
expect(type).to.equal "file"
|
||||
done()
|
||||
|
||||
it 'should take a file path and return the element for a nested document', (done)->
|
||||
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
|
||||
expect(type).to.equal "file"
|
||||
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()}"
|
||||
@locator.findElementByPath project._id, path, (err, element, type)->
|
||||
@locator.findElementByPath {project, path}, (err, element, type)->
|
||||
element.should.deep.equal subSubFile
|
||||
expect(type).to.equal "file"
|
||||
done()
|
||||
|
||||
it 'should take a file path and return the element for a nested folder', (done)->
|
||||
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
|
||||
expect(type).to.equal "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
|
||||
expect(type).to.equal "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
|
||||
assert.equal element, undefined
|
||||
expect(type).to.be.undefined
|
||||
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
|
||||
assert.equal element, undefined
|
||||
expect(type).to.be.undefined
|
||||
|
@ -250,7 +250,6 @@ describe 'ProjectLocator', ->
|
|||
|
||||
|
||||
describe "where duplicate folder exists", ->
|
||||
|
||||
beforeEach ->
|
||||
@duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{
|
||||
name: "1"
|
||||
|
@ -264,17 +263,15 @@ describe 'ProjectLocator', ->
|
|||
fileRefs: []
|
||||
docs: []
|
||||
]
|
||||
Project.getProject = sinon.stub()
|
||||
Project.getProject.callsArgWith(2, null, @project)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
|
||||
describe "with a null doc", ->
|
||||
|
@ -285,33 +282,34 @@ describe 'ProjectLocator', ->
|
|||
fileRefs: []
|
||||
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)->
|
||||
callback = sinon.stub()
|
||||
@locator.findElementByPath project._id, "/other.tex", (err, element)->
|
||||
path = "/other.tex"
|
||||
@locator.findElementByPath {@project, path}, (err, element)->
|
||||
element.name.should.equal "other.tex"
|
||||
done()
|
||||
|
||||
|
||||
describe "with a null project", ->
|
||||
beforeEach ->
|
||||
@project =
|
||||
rootFolder:[
|
||||
folders: []
|
||||
fileRefs: []
|
||||
docs: [{name:"main.tex"}, null, {name:"other.tex"}]
|
||||
]
|
||||
Project.getProject = sinon.stub()
|
||||
Project.getProject.callsArgWith(2, null)
|
||||
@ProjectGetter =
|
||||
getProject: sinon.stub().callsArg(2)
|
||||
|
||||
it "should not crash with a null", (done)->
|
||||
callback = sinon.stub()
|
||||
@locator.findElementByPath project._id, "/other.tex", (err, element)->
|
||||
path = "/other.tex"
|
||||
@locator.findElementByPath {project_id: @project._id, path}, (err, element)->
|
||||
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', ()->
|
||||
it 'should return the project from an array case insenstive', (done)->
|
||||
|
|
Loading…
Reference in a new issue