From 5e529c38f917a0d4b56891b00475068c54d3afc9 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Thu, 3 Nov 2022 16:40:07 +0900 Subject: [PATCH] handle file path in other cases --- .../src/__tests__/fetch-remote-file.js | 16 +++++++++++++++- packages/gatsby-core-utils/src/filename-utils.ts | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js b/packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js index 5cbf985fcce67..ea60474c2c72a 100644 --- a/packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js +++ b/packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js @@ -349,7 +349,7 @@ describe(`fetch-remote-file`, () => { expect(gotStream).toBeCalledTimes(1) }) - it(`downloads and create a jpg file for file with non-ascii filename`, async () => { + it(`downloads and create a jpg file for file with non-ascii url`, async () => { const filePath = await fetchRemoteFile({ url: `http://external.com/${encodeURIComponent(`개`)}.jpg`, cache, @@ -362,6 +362,20 @@ describe(`fetch-remote-file`, () => { expect(gotStream).toBeCalledTimes(1) }) + it(`downloads and create a jpg file for file with non-ascii filename`, async () => { + const filePath = await fetchRemoteFile({ + url: `http://external.com/dog.jpg`, + name: `${encodeURIComponent(`개`)}.jpg`, + cache, + }) + + expect(path.basename(filePath)).toBe(`개.jpg`) + expect(getFileSize(filePath)).resolves.toBe( + await getFileSize(path.join(__dirname, `./fixtures/dog-thumbnail.jpg`)) + ) + expect(gotStream).toBeCalledTimes(1) + }) + it(`downloads and create a jpg file for unknown extension`, async () => { const filePath = await fetchRemoteFile({ url: `http://external.com/dog`, diff --git a/packages/gatsby-core-utils/src/filename-utils.ts b/packages/gatsby-core-utils/src/filename-utils.ts index 03cc37d07b613..463a2bea50c70 100644 --- a/packages/gatsby-core-utils/src/filename-utils.ts +++ b/packages/gatsby-core-utils/src/filename-utils.ts @@ -52,6 +52,9 @@ export function createFilePath( filename: string, ext: string ): string { + directory = decodeURIComponent(directory) + filename = decodeURIComponent(filename) + const purgedFileName = filename.replace(filenamePurgeRegex, `-`) const shouldAddHash = purgedFileName !== filename