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

Commit bdc0729

Browse files
richardschneiderdaviddias
authored andcommitted
refactor: talk directly to ipfs-unixfs-engine
ipfs.ls does not provide enough information
1 parent 5c2231e commit bdc0729

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

src/http/api/resources/file.js

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
const mh = require('multihashes')
44
const debug = require('debug')
5-
const log = debug('jsipfs:http-api:files')
6-
log.error = debug('jsipfs:http-api:files:error')
5+
const log = debug('jsipfs:http-api:file')
6+
log.error = debug('jsipfs:http-api:file:error')
7+
const unixfsEngine = require('ipfs-unixfs-engine')
8+
const exporter = unixfsEngine.exporter
9+
const pull = require('pull-stream')
10+
const toB58String = require('multihashes').toB58String
711

812
exports = module.exports
913

@@ -12,6 +16,18 @@ const fileTypeMap = {
1216
dir: 'Directory'
1317
}
1418

19+
function toFileObject(file) {
20+
const fo = {
21+
Hash: toB58String(file.hash),
22+
Size: file.size,
23+
Type: fileTypeMap[file.type] || file.type
24+
}
25+
if (fo.Hash !== file.name) {
26+
fo.Name = file.name
27+
}
28+
return fo;
29+
}
30+
1531
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
1632
exports.parseKey = (request, reply) => {
1733
if (!request.query.arg) {
@@ -42,8 +58,11 @@ exports.parseKey = (request, reply) => {
4258
}).code(500).takeover()
4359
}
4460

61+
const subpaths = key.split('/')
62+
subpaths.shift()
4563
reply({
4664
path: request.query.arg,
65+
subpaths: subpaths,
4766
key: key,
4867
hash: hash
4968
})
@@ -55,36 +74,38 @@ exports.ls = {
5574

5675
// main route handler which is called after the above `parseArgs`, but only if the args were valid
5776
handler: (request, reply) => {
58-
const key = request.pre.args.key
5977
const path = request.pre.args.path
60-
const hash = request.pre.args.hash
6178
const ipfs = request.server.app.ipfs
79+
const subpaths = request.pre.args.subpaths
80+
const rootDepth = subpaths.length
6281

63-
ipfs.ls(key, (err, files) => {
64-
if (err) {
65-
return reply({
66-
Message: 'Failed to list dir: ' + err.message,
67-
Code: 0
68-
}).code(500)
69-
}
82+
pull(
83+
exporter(path, ipfs._ipldResolver, { maxDepth: rootDepth + 1 }),
84+
pull.collect((err, files) => {
85+
if (err) {
86+
return reply({
87+
Message: 'Failed to list dir: ' + err.message,
88+
Code: 0
89+
}).code(500)
90+
}
7091

71-
let res = {
72-
Arguments: {},
73-
Objects: {}
74-
}
75-
res.Arguments[path] = key
76-
res.Objects[key] = {
77-
Hash: hash,
78-
Size: 0,
79-
Type: 'Directory',
80-
Links: files.map((file) => ({
81-
Name: file.name,
82-
Hash: file.hash,
83-
Size: file.size,
84-
Type: fileTypeMap[file.type] || file.type
85-
}))
86-
}
87-
reply(res)
88-
})
92+
let res = {
93+
Arguments: {},
94+
Objects: {}
95+
}
96+
const links = []
97+
files.forEach((file) => {
98+
if (file.depth === rootDepth) {
99+
let id = toB58String(file.hash)
100+
res.Arguments[path] = id
101+
res.Objects[id] = toFileObject(file)
102+
res.Objects[id].Links = file.type === 'file' ? null : links;
103+
} else {
104+
links.push(toFileObject(file))
105+
}
106+
})
107+
return reply(res)
108+
})
109+
)
89110
}
90111
}

0 commit comments

Comments
 (0)