Skip to content

Commit

Permalink
fix(gatsby-core-utils): decode uri-encode filename for remote file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Nov 16, 2022
1 parent ccf56d5 commit 086c862
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
43 changes: 43 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js
Expand Up @@ -133,7 +133,23 @@ const server = setupServer(
ctx.body(content)
)
}),
// Should test with non-ascii word `개` (which means dog in Korean)
rest.get(
`http://external.com/${encodeURIComponent(`개`)}.jpg`,
async (req, res, ctx) => {
const { content, contentLength } = await getFileContent(
path.join(__dirname, `./fixtures/dog-thumbnail.jpg`),
req
)

return res(
ctx.set(`Content-Type`, `image/jpg`),
ctx.set(`Content-Length`, contentLength),
ctx.status(200),
ctx.body(content)
)
}
),
rest.get(`http://external.com/dog`, async (req, res, ctx) => {
const { content, contentLength } = await getFileContent(
path.join(__dirname, `./fixtures/dog-thumbnail.jpg`),
Expand Down Expand Up @@ -333,6 +349,33 @@ describe(`fetch-remote-file`, () => {
expect(gotStream).toBeCalledTimes(1)
})

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,
})

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 file with non-ascii filename`, async () => {
const filePath = await fetchRemoteFile({
url: `http://external.com/dog.jpg`,
name: `${encodeURIComponent(`개`)}`,
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`,
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-core-utils/src/filename-utils.ts
Expand Up @@ -29,7 +29,7 @@ export function getRemoteFileExtension(url: string): string {
*
*/
export function getRemoteFileName(url: string): string {
return getParsedPath(url).name
return decodeURIComponent(getParsedPath(url).name)
}

export function createFileHash(input: string, length: number = 8): string {
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-filesystem/src/utils.js
Expand Up @@ -38,7 +38,7 @@ export function getRemoteFileExtension(url) {
* @return {String} filename
*/
export function getRemoteFileName(url) {
return getParsedPath(url).name
return decodeURIComponent(getParsedPath(url).name)
}

// createFilePath should be imported from `gatsby-core-utils`
Expand Down

0 comments on commit 086c862

Please sign in to comment.