fix invalid project names when opening templates

This commit is contained in:
Brian Gough 2018-09-25 11:15:32 +01:00
parent d6e0574be3
commit 1f6abd4e69
4 changed files with 30 additions and 1 deletions

View file

@ -99,6 +99,16 @@ module.exports = ProjectDetailsHandler =
# we couldn't make the name unique, something is wrong # we couldn't make the name unique, something is wrong
return callback new Errors.InvalidNameError("Project name could not be made unique") return callback new Errors.InvalidNameError("Project name could not be made unique")
fixProjectName: (name) ->
if name == ""
name = "Untitled"
if name.indexOf('/') > -1
# v2 does not allow / in a project name
name = name.replace(/\//g, '-')
if name.length > @MAX_PROJECT_NAME_LENGTH
name = name.substr(0, @MAX_PROJECT_NAME_LENGTH)
return name
setPublicAccessLevel : (project_id, newAccessLevel, callback = ->)-> setPublicAccessLevel : (project_id, newAccessLevel, callback = ->)->
logger.log project_id: project_id, level: newAccessLevel, "set public access level" logger.log project_id: project_id, level: newAccessLevel, "set public access level"
# DEPRECATED: `READ_ONLY` and `READ_AND_WRITE` are still valid in, but should no longer # DEPRECATED: `READ_ONLY` and `READ_AND_WRITE` are still valid in, but should no longer

View file

@ -3,6 +3,7 @@ Project = require('../../../js/models/Project').Project
ProjectUploadManager = require('../../../js/Features/Uploads/ProjectUploadManager') ProjectUploadManager = require('../../../js/Features/Uploads/ProjectUploadManager')
ProjectOptionsHandler = require('../../../js/Features/Project/ProjectOptionsHandler') ProjectOptionsHandler = require('../../../js/Features/Project/ProjectOptionsHandler')
ProjectRootDocManager = require('../../../js/Features/Project/ProjectRootDocManager') ProjectRootDocManager = require('../../../js/Features/Project/ProjectRootDocManager')
ProjectDetailsHandler = require('../../../js/Features/Project/ProjectDetailsHandler')
AuthenticationController = require('../../../js/Features/Authentication/AuthenticationController') AuthenticationController = require('../../../js/Features/Authentication/AuthenticationController')
settings = require('settings-sharelatex') settings = require('settings-sharelatex')
fs = require('fs') fs = require('fs')
@ -55,6 +56,8 @@ module.exports = TemplatesController =
) )
createFromZip: (zipReq, options, req, res)-> createFromZip: (zipReq, options, req, res)->
# remove any invalid characters from template name
projectName = ProjectDetailsHandler.fixProjectName(options.templateName)
dumpPath = "#{settings.path.dumpFolder}/#{uuid.v4()}" dumpPath = "#{settings.path.dumpFolder}/#{uuid.v4()}"
writeStream = fs.createWriteStream(dumpPath) writeStream = fs.createWriteStream(dumpPath)
@ -62,7 +65,7 @@ module.exports = TemplatesController =
logger.error err: error, "error getting zip from template API" logger.error err: error, "error getting zip from template API"
zipReq.pipe(writeStream) zipReq.pipe(writeStream)
writeStream.on 'close', -> writeStream.on 'close', ->
ProjectUploadManager.createProjectFromZipArchive options.currentUserId, options.templateName, dumpPath, (err, project)-> ProjectUploadManager.createProjectFromZipArchive options.currentUserId, projectName, dumpPath, (err, project)->
if err? if err?
logger.err err:err, zipReq:zipReq, "problem building project from zip" logger.err err:err, zipReq:zipReq, "problem building project from zip"
return res.sendStatus 500 return res.sendStatus 500

View file

@ -196,6 +196,20 @@ describe 'ProjectDetailsHandler', ->
expect(error).to.eql new Errors.InvalidNameError("Project name could not be made unique") expect(error).to.eql new Errors.InvalidNameError("Project name could not be made unique")
done() done()
describe "fixProjectName", ->
it "should change empty names to Untitled", () ->
expect(@handler.fixProjectName "").to.equal "Untitled"
it "should replace / with -", () ->
expect(@handler.fixProjectName "foo/bar").to.equal "foo-bar"
it "should truncate long names", () ->
expect(@handler.fixProjectName new Array(1000).join("a")).to.equal "a".repeat(150)
it "should accept normal names", () ->
expect(@handler.fixProjectName "foobar").to.equal "foobar"
describe "setPublicAccessLevel", -> describe "setPublicAccessLevel", ->
beforeEach -> beforeEach ->

View file

@ -32,12 +32,14 @@ describe 'TemplatesController', ->
} }
@ProjectDetailsHandler = @ProjectDetailsHandler =
getProjectDescription:sinon.stub() getProjectDescription:sinon.stub()
fixProjectName: sinon.stub().returns(@templateName)
@Project = @Project =
update: sinon.stub().callsArgWith(3, null) update: sinon.stub().callsArgWith(3, null)
@controller = SandboxedModule.require modulePath, requires: @controller = SandboxedModule.require modulePath, requires:
'../../../js/Features/Uploads/ProjectUploadManager':@ProjectUploadManager '../../../js/Features/Uploads/ProjectUploadManager':@ProjectUploadManager
'../../../js/Features/Project/ProjectOptionsHandler':@ProjectOptionsHandler '../../../js/Features/Project/ProjectOptionsHandler':@ProjectOptionsHandler
'../../../js/Features/Project/ProjectRootDocManager':@ProjectRootDocManager '../../../js/Features/Project/ProjectRootDocManager':@ProjectRootDocManager
'../../../js/Features/Project/ProjectDetailsHandler':@ProjectDetailsHandler
'../../../js/Features/Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()} '../../../js/Features/Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()}
'./TemplatesPublisher':@TemplatesPublisher './TemplatesPublisher':@TemplatesPublisher
"logger-sharelatex": "logger-sharelatex":