add in the calls to block large projects

This commit is contained in:
Henry Oswald 2016-02-29 13:05:17 +00:00
parent 8aec86af4e
commit 8f0d1dc73e
9 changed files with 56 additions and 30 deletions

View file

@ -70,8 +70,12 @@ module.exports = EditorHttpController =
if !EditorHttpController._nameIsAcceptableLength(name) if !EditorHttpController._nameIsAcceptableLength(name)
return res.sendStatus 400 return res.sendStatus 400
EditorController.addDoc project_id, parent_folder_id, name, [], "editor", (error, doc) -> EditorController.addDoc project_id, parent_folder_id, name, [], "editor", (error, doc) ->
return next(error) if error? if error == "project_has_to_many_files"
res.json doc res.status(400).json(req.i18n.translate("project_has_to_many_files"))
else if error?
next(error)
else
res.json doc
addFolder: (req, res, next) -> addFolder: (req, res, next) ->
project_id = req.params.Project_id project_id = req.params.Project_id
@ -80,8 +84,12 @@ module.exports = EditorHttpController =
if !EditorHttpController._nameIsAcceptableLength(name) if !EditorHttpController._nameIsAcceptableLength(name)
return res.sendStatus 400 return res.sendStatus 400
EditorController.addFolder project_id, parent_folder_id, name, "editor", (error, doc) -> EditorController.addFolder project_id, parent_folder_id, name, "editor", (error, doc) ->
return next(error) if error? if error == "project_has_to_many_files"
res.json doc res.status(400).json(req.i18n.translate("project_has_to_many_files"))
else if error?
next(error)
else
res.json doc
renameEntity: (req, res, next) -> renameEntity: (req, res, next) ->
project_id = req.params.Project_id project_id = req.params.Project_id

View file

@ -35,7 +35,9 @@ module.exports =
self._buildTemplate "mainbasic.tex", owner_id, projectName, (error, docLines)-> self._buildTemplate "mainbasic.tex", owner_id, projectName, (error, docLines)->
return callback(error) if error? return callback(error) if error?
ProjectEntityHandler.addDoc project._id, project.rootFolder[0]._id, "main.tex", docLines, (error, doc)-> ProjectEntityHandler.addDoc project._id, project.rootFolder[0]._id, "main.tex", docLines, (error, doc)->
return callback(error) if error? if error?
logger.err err:error, "error adding doc"
return callback(error)
ProjectEntityHandler.setRootDoc project._id, doc._id, (error) -> ProjectEntityHandler.setRootDoc project._id, doc._id, (error) ->
callback(error, project) callback(error, project)

View file

@ -35,7 +35,7 @@ module.exports = ProjectDeleter =
archiveProject: (project_id, callback = (error) ->)-> archiveProject: (project_id, callback = (error) ->)->
logger.log project_id:project_id, "deleting project" logger.log project_id:project_id, "deleting project"
ProjectGetter.getProject project_id, {owner_ref:1, collaberator_refs:1, readOnly_refs:1}, (err, project)=> ProjectGetter.getProject project_id, {owner_ref:true, collaberator_refs:true, readOnly_refs:true}, (err, project)=>
if err? or !project? if err? or !project?
logger.err err:err, project_id:project_id, "error getting project to delete it" logger.err err:err, project_id:project_id, "error getting project to delete it"
callback(err) callback(err)

View file

@ -8,7 +8,7 @@ _ = require("underscore")
module.exports = module.exports =
getDetails: (project_id, callback)-> getDetails: (project_id, callback)->
ProjectGetter.getProject project_id, {name:1, description:1, compiler:1, features:1, owner_ref:1}, (err, project)-> ProjectGetter.getProject project_id, {name:true, description:true, compiler:true, features:true, owner_ref:true}, (err, project)->
if err? if err?
logger.err err:err, project_id:project_id, "error getting project" logger.err err:err, project_id:project_id, "error getting project"
return callback(err) return callback(err)
@ -37,7 +37,7 @@ module.exports =
renameProject: (project_id, newName, callback = ->)-> renameProject: (project_id, newName, callback = ->)->
logger.log project_id: project_id, newName:newName, "renaming project" logger.log project_id: project_id, newName:newName, "renaming project"
ProjectGetter.getProject project_id, {"name":1}, (err, project)-> ProjectGetter.getProject project_id, {name:true}, (err, project)->
if err? or !project? if err? or !project?
logger.err err:err, project_id:project_id, "error getting project or could not find it todo project rename" logger.err err:err, project_id:project_id, "error getting project or could not find it todo project rename"
return callback(err) return callback(err)

View file

@ -1,4 +1,5 @@
Project = require('../../models/Project').Project Project = require('../../models/Project').Project
settings = require "settings-sharelatex"
Doc = require('../../models/Doc').Doc Doc = require('../../models/Doc').Doc
Folder = require('../../models/Folder').Folder Folder = require('../../models/Folder').Folder
File = require('../../models/File').File File = require('../../models/File').File
@ -436,6 +437,8 @@ module.exports = ProjectEntityHandler =
if subfolderCounts?.length > 0 if subfolderCounts?.length > 0
total = _.reduce subfolderCounts, (a, b)-> return a + b total = _.reduce subfolderCounts, (a, b)-> return a + b
if folder?.folders?.length?
total += folder?.folders?.length
if folder?.docs?.length? if folder?.docs?.length?
total += folder?.docs?.length total += folder?.docs?.length
if folder?.fileRefs?.length? if folder?.fileRefs?.length?
@ -458,30 +461,34 @@ module.exports = ProjectEntityHandler =
logger.err project_id:project_id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null" logger.err project_id:project_id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
return callback(e) return callback(e)
type = sanitizeTypeOfElement type type = sanitizeTypeOfElement type
ProjectGetter.getProject project_id, "rootFolder", (err, project)=> ProjectGetter.getProject project_id, {rootFolder:true}, (err, project)=>
if err? if err?
return callback(err) return callback(err)
if !folder_id? if !folder_id?
folder_id = project.rootFolder[0]._id folder_id = project.rootFolder[0]._id
projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=> ProjectEntityHandler._countElements project, (err, count)->
newPath = if count > settings.maxFilesPerProject
fileSystem: "#{path.fileSystem}/#{element.name}" logger.warn project_id:project_id, "project too big, stopping insertions"
mongo: path.mongo return callback("project_has_to_many_files")
if err? projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
logger.err err:err, project_id:project_id, folder_id:folder_id, type:type, element:element, "error finding folder for _putElement" newPath =
return callback(err) fileSystem: "#{path.fileSystem}/#{element.name}"
logger.log project_id: project_id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project" mongo: path.mongo
id = element._id+''
element._id = require('mongoose').Types.ObjectId(id)
conditions = _id:project_id
mongopath = "#{path.mongo}.#{type}"
update = "$push":{}
update["$push"][mongopath] = element
Project.update conditions, update, {}, (err)->
if err? if err?
logger.err err: err, project_id: project_id, 'error saving in putElement project' logger.err err:err, project_id:project_id, folder_id:folder_id, type:type, element:element, "error finding folder for _putElement"
return callback(err) return callback(err)
callback(err, {path:newPath}) logger.log project_id: project_id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project"
id = element._id+''
element._id = require('mongoose').Types.ObjectId(id)
conditions = _id:project_id
mongopath = "#{path.mongo}.#{type}"
update = "$push":{}
update["$push"][mongopath] = element
Project.update conditions, update, {}, (err)->
if err?
logger.err err: err, project_id: project_id, 'error saving in putElement project'
return callback(err)
callback(err, {path:newPath})
confirmFolder = (project, folder_id, callback)-> confirmFolder = (project, folder_id, callback)->
logger.log folder_id:folder_id, project_id:project._id, "confirming folder in project" logger.log folder_id:folder_id, project_id:project._id, "confirming folder in project"

View file

@ -311,6 +311,7 @@ script(type='text/ng-template', id='newDocModalTemplate')
h3 #{translate("new_file")} h3 #{translate("new_file")}
.modal-body .modal-body
form(novalidate, name="newDocForm") form(novalidate, name="newDocForm")
div.alert.alert-danger(ng-if="error") {{error}}
input.form-control( input.form-control(
type="text", type="text",
placeholder="File Name", placeholder="File Name",
@ -335,6 +336,7 @@ script(type='text/ng-template', id='newFolderModalTemplate')
.modal-header .modal-header
h3 #{translate("new_folder")} h3 #{translate("new_folder")}
.modal-body .modal-body
div.alert.alert-danger(ng-if="error") {{error}}
form(novalidate, name="newFolderForm") form(novalidate, name="newFolderForm")
input.form-control( input.form-control(
type="text", type="text",

View file

@ -129,6 +129,9 @@ module.exports =
# Same, but with http auth credentials. # Same, but with http auth credentials.
httpAuthSiteUrl: 'http://#{httpAuthUser}:#{httpAuthPass}@localhost:3000' httpAuthSiteUrl: 'http://#{httpAuthUser}:#{httpAuthPass}@localhost:3000'
maxFilesPerProject: 200
# Security # Security
# -------- # --------
security: security:

View file

@ -61,6 +61,8 @@ define [
$scope.state.inflight = true $scope.state.inflight = true
ide.fileTreeManager ide.fileTreeManager
.createDoc(name, parent_folder) .createDoc(name, parent_folder)
.error (e)->
$scope.error = e
.success () -> .success () ->
$scope.state.inflight = false $scope.state.inflight = false
$modalInstance.close() $modalInstance.close()
@ -90,6 +92,8 @@ define [
$scope.state.inflight = true $scope.state.inflight = true
ide.fileTreeManager ide.fileTreeManager
.createFolder(name, parent_folder) .createFolder(name, parent_folder)
.error (e)->
$scope.error = e
.success () -> .success () ->
$scope.state.inflight = false $scope.state.inflight = false
$modalInstance.close() $modalInstance.close()

View file

@ -1101,25 +1101,25 @@ describe 'ProjectEntityHandler', ->
it "should return the correct number", (done)-> it "should return the correct number", (done)->
@ProjectEntityHandler._countElements @project, (err, count)-> @ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 21 count.should.equal 26
done() done()
it "should deal with null folders", (done)-> it "should deal with null folders", (done)->
@project.rootFolder[0].folders[0].folders = undefined @project.rootFolder[0].folders[0].folders = undefined
@ProjectEntityHandler._countElements @project, (err, count)-> @ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 14 count.should.equal 17
done() done()
it "should deal with null docs", (done)-> it "should deal with null docs", (done)->
@project.rootFolder[0].folders[0].docs = undefined @project.rootFolder[0].folders[0].docs = undefined
@ProjectEntityHandler._countElements @project, (err, count)-> @ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 18 count.should.equal 23
done() done()
it "should deal with null fileRefs", (done)-> it "should deal with null fileRefs", (done)->
@project.rootFolder[0].folders[0].folders[0].fileRefs = undefined @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
@ProjectEntityHandler._countElements @project, (err, count)-> @ProjectEntityHandler._countElements @project, (err, count)->
count.should.equal 18 count.should.equal 23
done() done()