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

Commit

Permalink
gateway detecting directory fix and use eachOfSeries
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 8c04aca commit 9772dd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 25 additions & 12 deletions src/http-api/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
const mh = require('multihashes')
// const pf = require('promised-for')
const promisify = require('promisify-es6')
const eachOf = require('async/eachOf')
const eachOfSeries = require('async/eachOfSeries')

const html = require('./utils/html')
const PathUtil = require('./utils/path')

const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ]

const resolveDirectory = promisify((ipfs, path, callback) => {
const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
if (!callback) {
callback = noop
}

const parts = PathUtil.splitPath(path)
const multihash = parts[0]
mh.validate(mh.fromB58String(multihash))

ipfs
.object
Expand Down Expand Up @@ -46,24 +45,38 @@ const resolveMultihash = promisify((ipfs, path, callback) => {

let currentMultihash = parts[0]

eachOf(parts, (multihash, currentIndex, next) => {
eachOfSeries(parts, (multihash, currentIndex, next) => {
// throws error when invalid multihash is passed
mh.validate(mh.fromB58String(currentMultihash))
console.log('currentMultihash: ', currentMultihash)
console.log('currentIndex: ', currentIndex, '/', partsLength)

ipfs
.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'))
}

// console.log('DAGNode: ', DAGNode)
if (currentIndex === partsLength - 1) {
// leaf node
console.log('leaf node: ', currentMultihash)
console.log('DAGNode: ', DAGNode.links)

if (DAGNode.links &&
DAGNode.links.length > 0 &&
DAGNode.links[0].name.length > 0) {
for (let link of DAGNode.links) {
if (mh.toB58String(link.multihash) === currentMultihash) {
return next()
}
}

// this is a directory.
let isDirErr = new Error('This dag node is a directory')
// add currentMultihash as a fileName so it can be used by resolveDirectory
isDirErr.fileName = currentMultihash
return next(isDirErr)
}

next()
} else {
// find multihash of requested named-file
Expand Down
4 changes: 2 additions & 2 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ exports.gateway = {
}
})
}).catch((err) => {
console.log('err: ', err.toString())
console.log('err: ', err.toString(), ' fileName: ', err.fileName)

const errorToString = err.toString()
if (errorToString === 'Error: This dag node is a directory') {
return GatewayResolver
.resolveDirectory(ipfs, ref)
.resolveDirectory(ipfs, ref, err.fileName)
.then((data) => {
if (typeof data === 'string') {
// no index file found
Expand Down

0 comments on commit 9772dd8

Please sign in to comment.