Przeglądaj źródła

make getSafePathname a method in CompileController rather than Project

Hayden Faulds 8 lat temu
rodzic
commit
eb198cd8c3

+ 9 - 2
services/web/app/coffee/Features/Compile/CompileController.coffee

@@ -4,6 +4,7 @@ CompileManager = require("./CompileManager")
 ClsiManager = require("./ClsiManager")
 logger  = require "logger-sharelatex"
 request = require "request"
+sanitize = require('sanitizer')
 Settings = require "settings-sharelatex"
 AuthenticationController = require "../Authentication/AuthenticationController"
 UserGetter = require "../User/UserGetter"
@@ -85,12 +86,14 @@ module.exports = CompileController =
 
 		ProjectGetter.getProject project_id, name: 1, (err, project) ->
 			res.contentType("application/pdf")
+			filename = "#{CompileController._getSafeProjectName(project)}.pdf"
+
 			if !!req.query.popupDownload
 				logger.log project_id: project_id, "download pdf as popup download"
-				res.setContentDisposition('attachment', {filename: "#{project.getSafeProjectName()}.pdf"})
+				res.setContentDisposition('attachment', {filename})
 			else
 				logger.log project_id: project_id, "download pdf to embed in browser"
-				res.setContentDisposition('', {filename: "#{project.getSafeProjectName()}.pdf"})
+				res.setContentDisposition('', {filename})
 
 			rateLimit (err, canContinue)->
 				if err?
@@ -104,6 +107,10 @@ module.exports = CompileController =
 						url = CompileController._getFileUrl project_id, user_id, req.params.build_id, "output.pdf"
 						CompileController.proxyToClsi(project_id, url, req, res, next)
 
+	_getSafeProjectName: (project) ->
+		safeProjectName = project.name.replace(new RegExp("\\W", "g"), '_')
+		sanitize.escape(safeProjectName)
+
 	deleteAuxFiles: (req, res, next) ->
 		project_id = req.params.Project_id
 		CompileController._compileAsUser req, (error, user_id) ->

+ 0 - 5
services/web/app/coffee/models/Project.coffee

@@ -3,7 +3,6 @@ Settings = require 'settings-sharelatex'
 _ = require('underscore')
 FolderSchema = require('./Folder.js').FolderSchema
 logger = require('logger-sharelatex')
-sanitize = require('sanitizer')
 concreteObjectId = require('mongoose').Types.ObjectId
 Errors  = require "../Features/Errors/Errors"
 
@@ -74,10 +73,6 @@ applyToAllFilesRecursivly = ProjectSchema.statics.applyToAllFilesRecursivly = (f
 	_.each folder.folders, (folder)->
 		applyToAllFilesRecursivly(folder, fun)
 
-ProjectSchema.methods.getSafeProjectName = ->
-	safeProjectName = this.name.replace(new RegExp("\\W", "g"), '_')
-	return sanitize.escape(safeProjectName)
-
 conn = mongoose.createConnection(Settings.mongo.url, {
 	server: {poolSize: Settings.mongo.poolSize || 10},
 	config: {autoIndex: false}

+ 4 - 4
services/web/test/unit/coffee/Compile/CompileControllerTests.coffee

@@ -113,10 +113,9 @@ describe "CompileController", ->
 		beforeEach ->
 			@req.params =
 				Project_id: @project_id
-			@project =
-				getSafeProjectName: () => @safe_name = "safe-name"
 
 			@req.query = {pdfng:true}
+			@project = name: "test namè"
 			@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
 
 		describe "when downloading for embedding", ->
@@ -135,9 +134,10 @@ describe "CompileController", ->
 					.calledWith("application/pdf")
 					.should.equal true
 
-			it "should set the content-disposition header with the project name", ->
+			it "should set the content-disposition header with a safe version of the project name", ->
+				console.log @res.setContentDisposition.args[0]
 				@res.setContentDisposition
-					.calledWith('', {filename: "#{@safe_name}.pdf"})
+					.calledWith('', filename: "test_nam_.pdf")
 					.should.equal true
 
 			it "should increment the pdf-downloads metric", ->