Skip to content

Commit 086c862

Browse files
authored
fix(gatsby-core-utils): decode uri-encode filename for remote file (#35637)
Fixes #35636
1 parent ccf56d5 commit 086c862

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,23 @@ const server = setupServer(
133133
ctx.body(content)
134134
)
135135
}),
136+
// Should test with non-ascii word `개` (which means dog in Korean)
137+
rest.get(
138+
`http://external.com/${encodeURIComponent(`개`)}.jpg`,
139+
async (req, res, ctx) => {
140+
const { content, contentLength } = await getFileContent(
141+
path.join(__dirname, `./fixtures/dog-thumbnail.jpg`),
142+
req
143+
)
136144

145+
return res(
146+
ctx.set(`Content-Type`, `image/jpg`),
147+
ctx.set(`Content-Length`, contentLength),
148+
ctx.status(200),
149+
ctx.body(content)
150+
)
151+
}
152+
),
137153
rest.get(`http://external.com/dog`, async (req, res, ctx) => {
138154
const { content, contentLength } = await getFileContent(
139155
path.join(__dirname, `./fixtures/dog-thumbnail.jpg`),
@@ -333,6 +349,33 @@ describe(`fetch-remote-file`, () => {
333349
expect(gotStream).toBeCalledTimes(1)
334350
})
335351

352+
it(`downloads and create a jpg file for file with non-ascii url`, async () => {
353+
const filePath = await fetchRemoteFile({
354+
url: `http://external.com/${encodeURIComponent(`개`)}.jpg`,
355+
cache,
356+
})
357+
358+
expect(path.basename(filePath)).toBe(`개.jpg`)
359+
expect(getFileSize(filePath)).resolves.toBe(
360+
await getFileSize(path.join(__dirname, `./fixtures/dog-thumbnail.jpg`))
361+
)
362+
expect(gotStream).toBeCalledTimes(1)
363+
})
364+
365+
it(`downloads and create a jpg file for file with non-ascii filename`, async () => {
366+
const filePath = await fetchRemoteFile({
367+
url: `http://external.com/dog.jpg`,
368+
name: `${encodeURIComponent(`개`)}`,
369+
cache,
370+
})
371+
372+
expect(path.basename(filePath)).toBe(`개.jpg`)
373+
expect(getFileSize(filePath)).resolves.toBe(
374+
await getFileSize(path.join(__dirname, `./fixtures/dog-thumbnail.jpg`))
375+
)
376+
expect(gotStream).toBeCalledTimes(1)
377+
})
378+
336379
it(`downloads and create a jpg file for unknown extension`, async () => {
337380
const filePath = await fetchRemoteFile({
338381
url: `http://external.com/dog`,

packages/gatsby-core-utils/src/filename-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getRemoteFileExtension(url: string): string {
2929
*
3030
*/
3131
export function getRemoteFileName(url: string): string {
32-
return getParsedPath(url).name
32+
return decodeURIComponent(getParsedPath(url).name)
3333
}
3434

3535
export function createFileHash(input: string, length: number = 8): string {
@@ -52,6 +52,9 @@ export function createFilePath(
5252
filename: string,
5353
ext: string
5454
): string {
55+
directory = decodeURIComponent(directory)
56+
filename = decodeURIComponent(filename)
57+
5558
const purgedFileName = filename.replace(filenamePurgeRegex, `-`)
5659
const shouldAddHash = purgedFileName !== filename
5760

packages/gatsby-source-filesystem/src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getRemoteFileExtension(url) {
3838
* @return {String} filename
3939
*/
4040
export function getRemoteFileName(url) {
41-
return getParsedPath(url).name
41+
return decodeURIComponent(getParsedPath(url).name)
4242
}
4343

4444
// createFilePath should be imported from `gatsby-core-utils`

0 commit comments

Comments
 (0)