Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,27 @@ export function mapToList(map, keyNames="key", collectedKeys=Im.Map()) {
}

export function extractFileNameFromContentDispositionHeader(value){
let responseFilename = /filename="([^;]*);?"/i.exec(value)
if (responseFilename === null) {
responseFilename = /filename=([^;]*);?/i.exec(value)
}
let patterns = [
/filename\*=[^']+'\w*'"([^"]+)";?/i,
/filename\*=[^']+'\w*'([^;]+);?/i,
/filename="([^;]*);?"/i,
/filename=([^;]*);?/i
]

let responseFilename
patterns.some(regex => {
responseFilename = regex.exec(value)
return responseFilename !== null
})

if (responseFilename !== null && responseFilename.length > 1) {
return responseFilename[1]
try {
return decodeURIComponent(responseFilename[1])
} catch(e) {
console.error(e)
}
}

return null
}

Expand Down Expand Up @@ -790,4 +804,4 @@ export function numberToString(thing) {
}

return thing
}
}
12 changes: 12 additions & 0 deletions test/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ describe("utils", function() {
let expectedResult = "filename.jpg"
expect(extractFileNameFromContentDispositionHeader(cdHeader)).toEqual(expectedResult)
})

it("should extract quoted filename in utf-8", function(){
let cdHeader = "attachment; filename*=UTF-8''\"%D1%84%D0%B0%D0%B9%D0%BB.txt\""
let expectedResult = "файл.txt"
expect(extractFileNameFromContentDispositionHeader(cdHeader)).toEqual(expectedResult)
})

it("should extract filename in utf-8", function(){
let cdHeader = "attachment; filename*=utf-8'ru'%D1%84%D0%B0%D0%B9%D0%BB.txt"
let expectedResult = "файл.txt"
expect(extractFileNameFromContentDispositionHeader(cdHeader)).toEqual(expectedResult)
})

it("should not extract filename and return null", function(){
let cdHeader = "attachment; no file name provided"
Expand Down