|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const mh = require('multihashes') |
| 4 | +const debug = require('debug') |
| 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 |
| 11 | + |
| 12 | +exports = module.exports |
| 13 | + |
| 14 | +const fileTypeMap = { |
| 15 | + file: 'File', |
| 16 | + dir: 'Directory' |
| 17 | +} |
| 18 | + |
| 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 | + |
| 31 | +// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args` |
| 32 | +exports.parseKey = (request, reply) => { |
| 33 | + if (!request.query.arg) { |
| 34 | + return reply({ |
| 35 | + Message: "Argument 'key' is required", |
| 36 | + Code: 0 |
| 37 | + }).code(400).takeover() |
| 38 | + } |
| 39 | + |
| 40 | + let key = request.query.arg |
| 41 | + if (key.indexOf('/ipfs/') === 0) { |
| 42 | + key = key.substring(6) |
| 43 | + } |
| 44 | + |
| 45 | + let hash = key |
| 46 | + const slashIndex = hash.indexOf('/') |
| 47 | + if (slashIndex > 0) { |
| 48 | + hash = hash.substring(0, slashIndex) |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + mh.fromB58String(hash) |
| 53 | + } catch (err) { |
| 54 | + log.error(err) |
| 55 | + return reply({ |
| 56 | + Message: 'invalid ipfs ref path', |
| 57 | + Code: 0 |
| 58 | + }).code(500).takeover() |
| 59 | + } |
| 60 | + |
| 61 | + const subpaths = key.split('/') |
| 62 | + subpaths.shift() |
| 63 | + reply({ |
| 64 | + path: request.query.arg, |
| 65 | + subpaths: subpaths, |
| 66 | + key: key, |
| 67 | + hash: hash |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +exports.ls = { |
| 72 | + // uses common parseKey method that returns a `key` |
| 73 | + parseArgs: exports.parseKey, |
| 74 | + |
| 75 | + // main route handler which is called after the above `parseArgs`, but only if the args were valid |
| 76 | + handler: (request, reply) => { |
| 77 | + const path = request.pre.args.path |
| 78 | + const ipfs = request.server.app.ipfs |
| 79 | + const subpaths = request.pre.args.subpaths |
| 80 | + const rootDepth = subpaths.length |
| 81 | + |
| 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 | + } |
| 91 | + |
| 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 | + ) |
| 110 | + } |
| 111 | +} |
0 commit comments