Skip to content

Commit 0a975ac

Browse files
DEVORTEX-5129: Fixed regular expression to find quoted file name
1 parent 95cae89 commit 0a975ac

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

api/file-translations/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export class SmartlingFileTranslationsApi extends SmartlingBaseApi {
155155
const contentDisposition = response.headers.get("content-disposition");
156156
let fileName;
157157
if (contentDisposition) {
158-
const fileNameMatch = contentDisposition.match(/filename="?([^"]+)"?/);
158+
const fileNameMatch = contentDisposition.match(/filename="((?:[^"\\]|\\.)*)"/);
159159
if (fileNameMatch) {
160-
fileName = fileNameMatch[1];
160+
fileName = fileNameMatch[1].replace(/\\"/g, "\"");
161161
}
162162
}
163163
return {

test/file-translations.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,35 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
417417
assert.ok(fileWithMetadata.fileContent.byteLength === 1);
418418
});
419419

420+
it("Download translated file with escaped quotes in the file name", async () => {
421+
const localeId = "de-DE";
422+
423+
getHeaderStub.onCall(0).returns("application/xml");
424+
getHeaderStub.onCall(1).returns("attachment; filename=\"test - \\\"phase 1\\\".xml\"");
425+
getHeaderStub.returns(null);
426+
427+
const fileWithMetadata = await fileTranslationsApi.downloadTranslatedFileWithMetadata(
428+
accountUid, fileUid, mtUid, localeId
429+
);
430+
431+
sinon.assert.calledOnceWithExactly(
432+
fileTranslationsApiFetchStub,
433+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/mt/${mtUid}/locales/${localeId}/file`,
434+
{
435+
method: "get",
436+
headers: {
437+
Authorization: "test_token_type test_access_token",
438+
"Content-Type": "application/json",
439+
"User-Agent": userAgent
440+
}
441+
}
442+
);
443+
444+
assert.ok(fileWithMetadata.contentType === "application/xml");
445+
assert.ok(fileWithMetadata.fileName === "test - \"phase 1\".xml");
446+
assert.ok(fileWithMetadata.fileContent.byteLength === 1);
447+
});
448+
420449
it("Download translated files", async () => {
421450
await fileTranslationsApi.downloadTranslatedFiles(accountUid, fileUid, mtUid);
422451

0 commit comments

Comments
 (0)