diff --git a/src/core/utils.js b/src/core/utils.js index 1cee42b0001..7bd61314084 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -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 } @@ -790,4 +804,4 @@ export function numberToString(thing) { } return thing -} \ No newline at end of file +} diff --git a/test/core/utils.js b/test/core/utils.js index ce52f6dd68d..b292a6beef4 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -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"