Skip to content

Commit

Permalink
fix: only add Content-Disposition header if not already exists (#40)
Browse files Browse the repository at this point in the history
Allows other middlewares to set the header.
  • Loading branch information
Alan Shaw authored Jul 24, 2023
1 parent 91a153e commit f3cda76
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ export function withContentDispositionHeader (handler) {
return async (request, env, ctx) => {
const response = await handler(request, env, ctx)
const { searchParams } = new URL(request.url)
const fileName = searchParams.get('filename')
const download = searchParams.get('download')
if (fileName && download) {
response.headers.set('Content-Disposition', `attachment; filename="${fileName}"`)
} else if (download) {
response.headers.set('Content-Disposition', 'attachment')
} else if (fileName) {
response.headers.set('Content-Disposition', `inline; filename="${fileName}"`)
if (!response.headers.has('Content-Disposition')) {
const fileName = searchParams.get('filename')
const download = searchParams.get('download')
if (fileName && download) {
response.headers.set('Content-Disposition', `attachment; filename="${fileName}"`)
} else if (download) {
response.headers.set('Content-Disposition', 'attachment')
} else if (fileName) {
response.headers.set('Content-Disposition', `inline; filename="${fileName}"`)
}
}

return response
}
}
Expand Down

0 comments on commit f3cda76

Please sign in to comment.