Skip to content

Commit

Permalink
fix(gatsby-source-filesystem): report when downloading fails (#10980)
Browse files Browse the repository at this point in the history
  • Loading branch information
oorestisime authored and pieh committed Jan 31, 2019
1 parent 2f52c92 commit eff2cf4
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ async function pushToQueue(task, cb) {
const node = await processRemoteNode(task)
return cb(null, node)
} catch (e) {
return cb(null, e)
console.warn(`Failed to process remote content ${task.url}`)
return cb(e)
}
}

Expand Down Expand Up @@ -222,47 +223,38 @@ async function processRemoteNode({
const tmpFilename = createFilePath(pluginCacheDir, `tmp-${digest}`, ext)

// Fetch the file.
try {
const response = await requestRemoteNode(url, headers, tmpFilename)
// Save the response headers for future requests.
await cache.set(cacheId(url), response.headers)

// If the user did not provide an extension and we couldn't get one from remote file, try and guess one
if (ext === ``) {
const buffer = readChunk.sync(tmpFilename, 0, fileType.minimumBytes)
const filetype = fileType(buffer)
if (filetype) {
ext = `.${filetype.ext}`
}
}

const filename = createFilePath(
path.join(pluginCacheDir, digest),
name,
ext
)
// If the status code is 200, move the piped temp file to the real name.
if (response.statusCode === 200) {
await fs.move(tmpFilename, filename, { overwrite: true })
// Else if 304, remove the empty response.
} else {
await fs.remove(tmpFilename)
const response = await requestRemoteNode(url, headers, tmpFilename)
// Save the response headers for future requests.
await cache.set(cacheId(url), response.headers)

// If the user did not provide an extension and we couldn't get one from remote file, try and guess one
if (ext === ``) {
const buffer = readChunk.sync(tmpFilename, 0, fileType.minimumBytes)
const filetype = fileType(buffer)
if (filetype) {
ext = `.${filetype.ext}`
}
}

// Create the file node.
const fileNode = await createFileNode(filename, createNodeId, {})
fileNode.internal.description = `File "${url}"`
// Override the default plugin as gatsby-source-filesystem needs to
// be the owner of File nodes or there'll be conflicts if any other
// File nodes are created through normal usages of
// gatsby-source-filesystem.
createNode(fileNode, { name: `gatsby-source-filesystem` })

return fileNode
} catch (err) {
// ignore
const filename = createFilePath(path.join(pluginCacheDir, digest), name, ext)
// If the status code is 200, move the piped temp file to the real name.
if (response.statusCode === 200) {
await fs.move(tmpFilename, filename, { overwrite: true })
// Else if 304, remove the empty response.
} else {
await fs.remove(tmpFilename)
}
return null

// Create the file node.
const fileNode = await createFileNode(filename, createNodeId, {})
fileNode.internal.description = `File "${url}"`
// Override the default plugin as gatsby-source-filesystem needs to
// be the owner of File nodes or there'll be conflicts if any other
// File nodes are created through normal usages of
// gatsby-source-filesystem.
createNode(fileNode, { name: `gatsby-source-filesystem` })

return fileNode
}

/**
Expand Down

0 comments on commit eff2cf4

Please sign in to comment.