Move content-disposition setting into a method on res

This commit is contained in:
Shane Kilkelly 2017-04-12 16:00:02 +01:00
parent 02d75deaa0
commit a9b8b864df
8 changed files with 35 additions and 21 deletions

View file

@ -85,12 +85,10 @@ module.exports = CompileController =
res.contentType("application/pdf")
if !!req.query.popupDownload
logger.log project_id: project_id, "download pdf as popup download"
res.header(
'Content-Disposition', "attachment; filename=#{encodeURIComponent(project.getSafeProjectName())}.pdf"
)
res.setContentDisposition('attachment', {filename: "#{project.getSafeProjectName()}.pdf"})
else
logger.log project_id: project_id, "download pdf to embed in browser"
res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
res.setContentDisposition('', {filename: "#{project.getSafeProjectName()}.pdf"})
rateLimit (err, canContinue)->
if err?

View file

@ -15,9 +15,9 @@ module.exports = ProjectDownloadsController =
return next(error) if error?
ProjectZipStreamManager.createZipStreamForProject project_id, (error, stream) ->
return next(error) if error?
res.header(
"Content-Disposition",
"attachment; filename=#{encodeURIComponent(project.name)}.zip"
res.setContentDisposition(
'attachment',
{filename: "#{project.name}.zip"}
)
res.contentType('application/zip')
stream.pipe(res)
@ -30,9 +30,9 @@ module.exports = ProjectDownloadsController =
return next(error) if error?
ProjectZipStreamManager.createZipStreamForMultipleProjects project_ids, (error, stream) ->
return next(error) if error?
res.header(
"Content-Disposition",
"attachment; filename=ShareLaTeX Projects (#{project_ids.length} items).zip"
res.setContentDisposition(
'attachment',
{filename: "ShareLaTeX Projects (#{project_ids.length} items).zip"}
)
res.contentType('application/zip')
stream.pipe(res)

View file

@ -35,5 +35,5 @@ module.exports =
if (is_mobile_safari(user_agent) and is_html(file))
logger.log filename: file.name, user_agent: user_agent, "sending html file to mobile-safari as plain text"
res.setHeader('Content-Type', 'text/plain')
res.setHeader("Content-Disposition", "attachment; filename=#{encodeURIComponent(file.name)}")
res.setContentDisposition('attachment', {filename: file.name})
stream.pipe res

View file

@ -71,6 +71,19 @@ module.exports = (app, webRouter, apiRouter)->
res.locals.session = req.session
next()
addSetContentDisposition = (req, res, next) ->
res.setContentDisposition = (type, opts) ->
directives = for k, v of opts
"#{k}=\"#{encodeURIComponent(v)}\""
contentDispositionValue = "#{type}; #{directives.join('; ')}"
res.setHeader(
'Content-Disposition',
contentDispositionValue
)
next()
webRouter.use addSetContentDisposition
apiRouter.use addSetContentDisposition
webRouter.use (req, res, next)->
cdnBlocked = req.query.nocdn == 'true' or req.session.cdnBlocked

View file

@ -136,8 +136,8 @@ describe "CompileController", ->
.should.equal true
it "should set the content-disposition header with the project name", ->
@res.header
.calledWith("Content-Disposition", "filename=#{encodeURIComponent(@safe_name)}.pdf")
@res.setContentDisposition
.calledWith('', {filename: "#{@safe_name}.pdf"})
.should.equal true
it "should increment the pdf-downloads metric", ->

View file

@ -59,10 +59,10 @@ describe "ProjectDownloadsController", ->
@Project.findById.calledWith(@project_id, "name").should.equal(true)
it "should name the downloaded file after the project", ->
@res.header
@res.setContentDisposition
.calledWith(
"Content-Disposition",
"attachment; filename=#{encodeURIComponent(@project_name)}.zip")
'attachment',
{filename: "#{@project_name}.zip"})
.should.equal true
it "should record the action via Metrics", ->
@ -107,10 +107,10 @@ describe "ProjectDownloadsController", ->
.should.equal true
it "should name the downloaded file after the project", ->
@res.header
@res.setContentDisposition
.calledWith(
"Content-Disposition",
"attachment; filename=ShareLaTeX Projects (2 items).zip")
'attachment',
{filename: "ShareLaTeX Projects (2 items).zip"})
.should.equal true
it "should record the action via Metrics", ->

View file

@ -29,6 +29,7 @@ describe "FileStoreController", ->
get: (key) -> undefined
@res =
setHeader: sinon.stub()
setContentDisposition: sinon.stub()
@file =
name: "myfile.png"
@ -62,8 +63,8 @@ describe "FileStoreController", ->
it "should set the Content-Disposition header", (done)->
@stream.pipe = (des)=>
@res.setHeader.calledWith(
"Content-Disposition", "attachment; filename=#{encodeURIComponent(@file.name)}"
@res.setContentDisposition.calledWith(
"attachment", {filename: @file.name}
).should.equal true
done()
@controller.getFile @req, @res

View file

@ -70,6 +70,8 @@ class MockResponse
setHeader: (header, value) ->
@headers[header] = value
setContentDisposition: sinon.stub()
setTimeout: (@timout)->
header: sinon.stub()