Skip to content

Commit

Permalink
fix(gatsby-core-utils): fix urls without extension (#34930) (#34931)
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Peeters <[email protected]>
  • Loading branch information
gatsbybot and wardpeet authored Feb 25, 2022
1 parent f6734b9 commit b33fb35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ const server = setupServer(
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`),
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/invalid:dog*name.jpg`,
async (req, res, ctx) => {
Expand Down Expand Up @@ -301,6 +314,19 @@ describe(`fetch-remote-file`, () => {
expect(gotStream).toBeCalledTimes(1)
})

it(`downloads and create a jpg file for unknown extension`, async () => {
const filePath = await fetchRemoteFile({
url: `http://external.com/dog`,
cache,
})

expect(path.basename(filePath)).toBe(`dog.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 that has invalid characters`, async () => {
const filePath = await fetchRemoteFile({
url: `http://external.com/invalid:dog*name.jpg`,
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-core-utils/src/fetch-remote-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async function fetchFile({
await fs.ensureDir(path.join(fileDirectory, digest))

const tmpFilename = createFilePath(fileDirectory, `tmp-${digest}`, ext)
const filename = createFilePath(path.join(fileDirectory, digest), name, ext)
let filename = createFilePath(path.join(fileDirectory, digest), name, ext)

// See if there's response headers for this url
// from a previous request.
Expand All @@ -193,6 +193,8 @@ async function fetchFile({
const filetype = await fileType.fromFile(tmpFilename)
if (filetype) {
ext = `.${filetype.ext}`

filename += ext
}
}

Expand Down

0 comments on commit b33fb35

Please sign in to comment.