|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const ipfs = require('./../index.js').ipfs |
| 4 | +const bs58 = require('bs58') |
| 5 | +const multipart = require('ipfs-multipart') |
| 6 | +const Block = require('ipfs-blocks').Block |
| 7 | +const debug = require('debug') |
| 8 | +const log = debug('http-api:block') |
| 9 | +log.error = debug('http-api:block:error') |
| 10 | + |
| 11 | +exports = module.exports |
| 12 | + |
| 13 | +// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args` |
| 14 | +exports.parseKey = (request, reply) => { |
| 15 | + if (!request.query.arg) { |
| 16 | + return reply("Argument 'key' is required").code(400).takeover() |
| 17 | + } |
| 18 | + |
| 19 | + try { |
| 20 | + return reply({ |
| 21 | + key: new Buffer(bs58.decode(request.query.arg)) |
| 22 | + }) |
| 23 | + } catch (err) { |
| 24 | + log.error(err) |
| 25 | + return reply({ |
| 26 | + Message: 'Not a valid hash', |
| 27 | + Code: 0 |
| 28 | + }).code(500).takeover() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +exports.get = { |
| 33 | + // uses common parseKey method that returns a `key` |
| 34 | + parseArgs: exports.parseKey, |
| 35 | + |
| 36 | + // main route handler which is called after the above `parseArgs`, but only if the args were valid |
| 37 | + handler: (request, reply) => { |
| 38 | + const key = request.pre.args.key |
| 39 | + |
| 40 | + ipfs.block.get(key, (err, block) => { |
| 41 | + if (err) { |
| 42 | + log.error(err) |
| 43 | + return reply({ |
| 44 | + Message: 'Failed to get block: ' + err, |
| 45 | + Code: 0 |
| 46 | + }).code(500) |
| 47 | + } |
| 48 | + |
| 49 | + return reply(block.data.toString()) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +exports.put = { |
| 55 | + // pre request handler that parses the args and returns `data` which is assigned to `request.pre.args` |
| 56 | + parseArgs: (request, reply) => { |
| 57 | + if (!request.payload) { |
| 58 | + return reply("File argument 'data' is required").code(400).takeover() |
| 59 | + } |
| 60 | + |
| 61 | + const parser = multipart.reqParser(request.payload) |
| 62 | + var file |
| 63 | + |
| 64 | + parser.on('file', (fileName, fileStream) => { |
| 65 | + fileStream.on('data', (data) => { |
| 66 | + file = data |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + parser.on('end', () => { |
| 71 | + if (!file) { |
| 72 | + return reply("File argument 'data' is required").code(400).takeover() |
| 73 | + } |
| 74 | + |
| 75 | + return reply({ |
| 76 | + data: file.toString() |
| 77 | + }) |
| 78 | + }) |
| 79 | + }, |
| 80 | + |
| 81 | + // main route handler which is called after the above `parseArgs`, but only if the args were valid |
| 82 | + handler: (request, reply) => { |
| 83 | + const data = request.pre.args.data |
| 84 | + |
| 85 | + const block = new Block(data) |
| 86 | + |
| 87 | + ipfs.block.put(block, (err) => { |
| 88 | + if (err) { |
| 89 | + log.error(err) |
| 90 | + return reply({ |
| 91 | + Message: 'Failed to put block: ' + err, |
| 92 | + Code: 0 |
| 93 | + }).code(500) |
| 94 | + } |
| 95 | + |
| 96 | + return reply({ |
| 97 | + Key: bs58.encode(block.key).toString(), |
| 98 | + Size: block.data.length |
| 99 | + }) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +exports.del = { |
| 105 | + // uses common parseKey method that returns a `key` |
| 106 | + parseArgs: exports.parseKey, |
| 107 | + |
| 108 | + // main route handler which is called after the above `parseArgs`, but only if the args were valid |
| 109 | + handler: (request, reply) => { |
| 110 | + const key = request.pre.args.key |
| 111 | + |
| 112 | + ipfs.block.del(key, (err, block) => { |
| 113 | + if (err) { |
| 114 | + log.error(err) |
| 115 | + return reply({ |
| 116 | + Message: 'Failed to get block stats: ' + err, |
| 117 | + Code: 0 |
| 118 | + }).code(500) |
| 119 | + } |
| 120 | + |
| 121 | + return reply() |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +exports.stat = { |
| 127 | + // uses common parseKey method that returns a `key` |
| 128 | + parseArgs: exports.parseKey, |
| 129 | + |
| 130 | + // main route handler which is called after the above `parseArgs`, but only if the args were valid |
| 131 | + handler: (request, reply) => { |
| 132 | + const key = request.pre.args.key |
| 133 | + |
| 134 | + ipfs.block.stat(key, (err, block) => { |
| 135 | + if (err) { |
| 136 | + log.error(err) |
| 137 | + return reply({ |
| 138 | + Message: 'Failed to get block stats: ' + err, |
| 139 | + Code: 0 |
| 140 | + }).code(500) |
| 141 | + } |
| 142 | + |
| 143 | + return reply({ |
| 144 | + Key: bs58.encode(block.Key).toString(), |
| 145 | + Size: block.Size |
| 146 | + }) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments