diff --git a/src/http-api/gateway/resolver.js b/src/http-api/gateway/resolver.js index c164e8c9f9..f191419b43 100644 --- a/src/http-api/gateway/resolver.js +++ b/src/http-api/gateway/resolver.js @@ -10,22 +10,29 @@ const PathUtil = require('./utils/path') const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ] -const resolveDirectory = (ipfs, path, multihash) => { - return ipfs - .object - .get(multihash, { enc: 'base58' }) - .then((DAGNode) => { - const links = DAGNode.links - const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1) - - // found index file in links - if (indexFiles.length > 0) { - return indexFiles - } +const resolveDirectory = promisify((ipfs, path, callback) => { + if (!callback) { + callback = noop + } - return html.build(path, links) - }) -} + const parts = PathUtil.splitPath(path) + const multihash = parts[0] + + ipfs + .object + .get(multihash, { enc: 'base58' }) + .then((DAGNode) => { + const links = DAGNode.links + const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1) + + // found index file in links + if (indexFiles.length > 0) { + return callback(null, indexFiles) + } + + return callback(null, html.build(path, links)) + }) +}) const noop = function () {} @@ -47,6 +54,13 @@ const resolveMultihash = promisify((ipfs, path, callback) => { .object .get(currentMultihash, { enc: 'base58' }) .then((DAGNode) => { + // console.log('DAGNode: ', DAGNode) + if (DAGNode.links && DAGNode.links.length > 0 && DAGNode.links[0].name.length > 0) { + // this is a directory. + // fire directory error here. + return next(new Error('This dag node is a directory')) + } + if (currentIndex === partsLength - 1) { // leaf node console.log('leaf node: ', currentMultihash) @@ -76,7 +90,9 @@ const resolveMultihash = promisify((ipfs, path, callback) => { } }) }, (err) => { - if (err) throw err + if (err) { + return callback(err) + } callback(null, {multihash: currentMultihash}) }) // Original implementation diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index b0e97241fa..c56ef64980 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -301,38 +301,39 @@ exports.gateway = { } }) .catch((err) => { - if (err.toString() === 'Error: This dag node is a directory') { - return GatewayResolver - .resolveDirectory(ipfs, ref, data.multihash) - .then((data) => { - if (typeof data === 'string') { - // no index file found - if (!ref.endsWith('/')) { - // for a directory, if URL doesn't end with a / - // append / and redirect permanent to that URL - return reply.redirect(`${ref}/`).permanent(true) - } else { - // send directory listing - return reply(data) - } - } else { - // found index file - // redirect to URL/ - return reply.redirect(PathUtils.joinURLParts(ref, data[0].name)) - } - }).catch((err) => { - log.error(err) - return reply(err.toString()).code(500) - }) - } else { + if (err) { log.error(err) return reply(err.toString()).code(500) } }) }).catch((err) => { - const errorToString = err.toString() + console.log('err: ', err.toString()) - if (errorToString.startsWith('Error: no link named')) { + const errorToString = err.toString() + if (errorToString === 'Error: This dag node is a directory') { + return GatewayResolver + .resolveDirectory(ipfs, ref) + .then((data) => { + if (typeof data === 'string') { + // no index file found + if (!ref.endsWith('/')) { + // for a directory, if URL doesn't end with a / + // append / and redirect permanent to that URL + return reply.redirect(`${ref}/`).permanent(true) + } else { + // send directory listing + return reply(data) + } + } else { + // found index file + // redirect to URL/ + return reply.redirect(PathUtils.joinURLParts(ref, data[0].name)) + } + }).catch((err) => { + log.error(err) + return reply(err.toString()).code(500) + }) + } else if (errorToString.startsWith('Error: no link named')) { return reply(errorToString).code(404) } else if (errorToString.startsWith('Error: multihash length inconsistent') || errorToString.startsWith('Error: Non-base58 character')) {