Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
gateway detecting directories and rendering file browser ui
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Yahya <ya7yaz@gmail.com>
  • Loading branch information
ya7ya committed Aug 28, 2017
1 parent c7f6383 commit 8c04aca
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
48 changes: 32 additions & 16 deletions src/http-api/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
53 changes: 27 additions & 26 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/<found-index-file>
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/<found-index-file>
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')) {
Expand Down

0 comments on commit 8c04aca

Please sign in to comment.