diff --git a/src/uploadStorage/index.ts b/src/uploadStorage/index.ts index b9770816b..b3d691ea7 100644 --- a/src/uploadStorage/index.ts +++ b/src/uploadStorage/index.ts @@ -955,7 +955,7 @@ export class UploadStorage extends CrowdinApi { ): Promise> { const url = `${this.url}/storages`; const config = this.defaultConfig(); - config.headers['Crowdin-API-FileName'] = fileName; + config.headers['Crowdin-API-FileName'] = this.urlEncodeFileName(fileName); if (contentType) { config.headers['Content-Type'] = contentType; } else { @@ -970,6 +970,13 @@ export class UploadStorage extends CrowdinApi { return this.post(url, request, config); } + /** + * @param fileName file name + */ + urlEncodeFileName(fileName: string): string { + return encodeURIComponent(fileName); + } + /** * @param storageId storage identifier * @see https://support.crowdin.com/api/v2/#operation/api.storages.get diff --git a/tests/uploadStorage/api.test.ts b/tests/uploadStorage/api.test.ts index cd7745701..4ff6f1bff 100644 --- a/tests/uploadStorage/api.test.ts +++ b/tests/uploadStorage/api.test.ts @@ -10,6 +10,7 @@ describe('Upload Storage API', () => { const api: UploadStorage = new UploadStorage(credentials); const storageId = 2; const fileName = 'words.txt'; + const urlEncodedFileName = encodeURIComponent(fileName); const fileContent = 'test text.'; const limit = 25; @@ -36,7 +37,7 @@ describe('Upload Storage API', () => { }) .post('/storages', fileContent, { reqheaders: { - 'Crowdin-API-FileName': fileName, + 'Crowdin-API-FileName': urlEncodedFileName, Authorization: `Bearer ${api.token}`, }, }) @@ -87,4 +88,16 @@ describe('Upload Storage API', () => { it('Delete storage', async () => { await api.deleteStorage(storageId); }); + + it('URL encodes a Cyrillic filename', async () => { + const fileName = 'абвгд'; + const urlEncodeFileName = api.urlEncodeFileName(fileName); + expect(urlEncodeFileName).toBe('%D0%B0%D0%B1%D0%B2%D0%B3%D0%B4'); + }); + + it('URL encodes a non-Cyrillic filename', async () => { + const fileName = 'filename'; + const urlEncodeFileName = api.urlEncodeFileName(fileName); + expect(urlEncodeFileName).toBe('filename'); + }); });