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

Commit c8dbf6b

Browse files
authored
Merge pull request #499 from ipfs/fix/files-get
Fix/files get
2 parents 9afd69b + 028a98c commit c8dbf6b

34 files changed

+533
-1001
lines changed

gulpfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
const gulp = require('gulp')
4-
const parallel = require('run-parallel')
5-
const series = require('run-series')
4+
const parallel = require('async/parallel')
5+
const series = require('async/series')
66
const createTempNode = require('./test/utils/temp-node')
77
const API = require('./src/http-api')
88

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"aegir": "^8.0.1",
4444
"buffer-loader": "0.0.1",
4545
"chai": "^3.5.0",
46+
"execa": "^0.4.0",
4647
"expose-loader": "^0.7.1",
4748
"form-data": "^2.0.0",
4849
"fs-pull-blob-store": "^0.3.0",
@@ -59,6 +60,7 @@
5960
"transform-loader": "^0.2.3"
6061
},
6162
"dependencies": {
63+
"async": "^2.0.1",
6264
"babel-runtime": "^6.11.6",
6365
"bl": "^1.1.2",
6466
"boom": "^4.0.0",

src/cli/commands/bitswap/stat.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ module.exports = {
2323
stats.Wantlist = stats.Wantlist || []
2424
stats.Peers = stats.Peers || []
2525

26-
console.log(`
27-
bitswap status
26+
console.log(`bitswap status
2827
blocks received: ${stats.BlocksReceived}
2928
dup blocks received: ${stats.DupBlksReceived}
3029
dup data received: ${stats.DupDataReceived}B

src/cli/commands/block/stat.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const utils = require('../../utils')
4-
const bs58 = require('bs58')
54
const debug = require('debug')
65
const log = debug('cli:block')
76
log.error = debug('cli:block:error')
@@ -24,7 +23,7 @@ module.exports = {
2423
throw err
2524
}
2625

27-
console.log('Key:', bs58.encode(stats.key).toString())
26+
console.log('Key:', stats.key)
2827
console.log('Size:', stats.size)
2928
})
3029
})

src/cli/commands/bootstrap/add.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
if (err) {
1818
throw err
1919
}
20+
2021
ipfs.bootstrap.add(argv.peer, (err, list) => {
2122
if (err) {
2223
throw err

src/cli/commands/config/edit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const spawn = require('child_process').spawn
44
const fs = require('fs')
55
const temp = require('temp')
6-
const waterfall = require('run-waterfall')
6+
const waterfall = require('async/waterfall')
77
const debug = require('debug')
88
const log = debug('cli:config')
99
log.error = debug('cli:config:error')

src/cli/commands/files/cat.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const waterfall = require('async/waterfall')
34
const debug = require('debug')
45
const utils = require('../../utils')
56
const log = debug('cli:files')
@@ -14,19 +15,16 @@ module.exports = {
1415

1516
handler (argv) {
1617
const path = argv['ipfs-path']
17-
utils.getIPFS((err, ipfs) => {
18+
19+
waterfall([
20+
(cb) => utils.getIPFS(cb),
21+
(ipfs, cb) => ipfs.files.cat(path, cb)
22+
], (err, file) => {
1823
if (err) {
1924
throw err
2025
}
2126

22-
ipfs.files.cat(path, onFile)
27+
file.pipe(process.stdout)
2328
})
2429
}
2530
}
26-
27-
function onFile (err, file) {
28-
if (err) {
29-
throw (err)
30-
}
31-
file.pipe(process.stdout)
32-
}

src/cli/commands/files/get.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ log.error = debug('cli:files:error')
77
var fs = require('fs')
88
const path = require('path')
99
const pathExists = require('path-exists')
10+
const pull = require('pull-stream')
11+
const toPull = require('stream-to-pull-stream')
1012

1113
function checkArgs (hash, outPath) {
1214
// format the output directory
@@ -33,30 +35,39 @@ function ensureDir (dir, cb) {
3335
.catch(cb)
3436
}
3537

36-
function fileHandler (result, dir) {
37-
return function onFile (file) {
38+
function fileHandler (dir) {
39+
return function onFile (file, cb) {
40+
const lastSlash = file.path.lastIndexOf('/')
3841
// Check to see if the result is in a directory
39-
if (file.path.lastIndexOf('/') === -1) {
42+
if (lastSlash === -1) {
4043
const dirPath = path.join(dir, file.path)
4144
// Check to see if the result is a directory
42-
if (file.dir === false) {
45+
if (file.content) {
4346
file.content.pipe(fs.createWriteStream(dirPath))
47+
.once('error', cb)
48+
.once('end', cb)
4449
} else {
45-
ensureDir(dirPath, (err) => {
46-
if (err) {
47-
throw err
48-
}
49-
})
50+
ensureDir(dirPath, cb)
5051
}
5152
} else {
52-
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
53+
const filePath = file.path.substring(0, lastSlash + 1)
5354
const dirPath = path.join(dir, filePath)
55+
5456
ensureDir(dirPath, (err) => {
5557
if (err) {
56-
throw err
58+
return cb(err)
5759
}
5860

59-
file.content.pipe(fs.createWriteStream(dirPath))
61+
if (file.content) {
62+
const filename = file.path.substring(lastSlash)
63+
const target = path.join(dirPath, filename)
64+
65+
file.content.pipe(fs.createWriteStream(target))
66+
.once('error', cb)
67+
.once('end', cb)
68+
return
69+
}
70+
cb()
6071
})
6172
}
6273
}
@@ -76,17 +87,28 @@ module.exports = {
7687
},
7788

7889
handler (argv) {
79-
const dir = checkArgs(argv.ipfsPath, argv.output)
90+
const ipfsPath = argv['ipfs-path']
91+
const dir = checkArgs(ipfsPath, argv.output)
8092

8193
utils.getIPFS((err, ipfs) => {
8294
if (err) {
8395
throw err
8496
}
85-
ipfs.files.get(argv.ipfsPath, (err, result) => {
97+
98+
ipfs.files.get(ipfsPath, (err, stream) => {
8699
if (err) {
87100
throw err
88101
}
89-
result.on('data', fileHandler(result, dir))
102+
console.log(`Saving file(s) to ${ipfsPath}`)
103+
pull(
104+
toPull.source(stream),
105+
pull.asyncMap(fileHandler(dir)),
106+
pull.onEnd((err) => {
107+
if (err) {
108+
throw err
109+
}
110+
})
111+
)
90112
})
91113
})
92114
}

src/core/components/block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = function block (self) {
3333
return callback(err)
3434
}
3535
callback(null, {
36-
key: hash,
36+
key: multihash.toB58String(hash),
3737
size: block.data.length
3838
})
3939
})

src/core/components/go-online.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const series = require('run-series')
3+
const series = require('async/series')
44
const Bitswap = require('ipfs-bitswap')
55

66
module.exports = function goOnline (self) {

src/core/components/object.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const mDAG = require('ipfs-merkle-dag')
4-
const waterfall = require('run-waterfall')
4+
const waterfall = require('async/waterfall')
55
const promisify = require('promisify-es6')
66
const bs58 = require('bs58')
77
const DAGNode = mDAG.DAGNode
@@ -69,12 +69,7 @@ module.exports = function object (self) {
6969
cb(err, node)
7070
})
7171
}
72-
], (err, node) => {
73-
if (err) {
74-
return cb(err)
75-
}
76-
cb(null, node)
77-
})
72+
], cb)
7873
}
7974
}
8075

src/http-api/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const parallel = require('run-parallel')
3+
const parallel = require('async/parallel')
44
const Hapi = require('hapi')
55
const debug = require('debug')
66
const fs = require('fs')

src/http-api/resources/block.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const bs58 = require('bs58')
3+
const mh = require('multihashes')
44
const multipart = require('ipfs-multipart')
55
const Block = require('ipfs-block')
66
const debug = require('debug')
@@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {
1717

1818
try {
1919
return reply({
20-
key: new Buffer(bs58.decode(request.query.arg))
20+
key: mh.fromB58String(request.query.arg)
2121
})
2222
} catch (err) {
2323
log.error(err)
@@ -93,7 +93,7 @@ exports.put = {
9393
}
9494

9595
return reply({
96-
Key: bs58.encode(block.key).toString(),
96+
Key: mh.toB58String(block.key),
9797
Size: block.data.length
9898
})
9999
})
@@ -112,7 +112,7 @@ exports.del = {
112112
if (err) {
113113
log.error(err)
114114
return reply({
115-
Message: 'Failed to get block stats: ' + err,
115+
Message: 'Failed to delete block: ' + err,
116116
Code: 0
117117
}).code(500)
118118
}
@@ -129,7 +129,7 @@ exports.stat = {
129129
// main route handler which is called after the above `parseArgs`, but only if the args were valid
130130
handler: (request, reply) => {
131131
const key = request.pre.args.key
132-
132+
console.log('fetching', key)
133133
request.server.app.ipfs.block.stat(key, (err, block) => {
134134
if (err) {
135135
log.error(err)
@@ -140,7 +140,7 @@ exports.stat = {
140140
}
141141

142142
return reply({
143-
Key: bs58.encode(block.key).toString(),
143+
Key: block.key,
144144
Size: block.size
145145
})
146146
})

src/http-api/resources/bootstrap.js

+47-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
'use strict'
22

33
const boom = require('boom')
4+
const multiaddr = require('multiaddr')
45

56
exports = module.exports
67

8+
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
9+
exports.parseKey = (request, reply) => {
10+
if (!request.query.arg) {
11+
return reply("Argument 'multiaddr' is required").code(400).takeover()
12+
}
13+
14+
try {
15+
return reply({
16+
addr: multiaddr(request.query.arg)
17+
})
18+
} catch (err) {
19+
return reply({
20+
Message: 'Not a valid multiaddr',
21+
Code: 0
22+
}).code(500).takeover()
23+
}
24+
}
25+
726
exports.list = (request, reply) => {
8-
request.server.app.ipfs.bootstrap.list((err, list) => {
27+
const ipfs = request.server.app.ipfs
28+
ipfs.bootstrap.list((err, list) => {
929
if (err) {
1030
return reply(boom.badRequest(err))
1131
}
1232
return reply(list)
1333
})
1434
}
1535

16-
exports.add = (request, reply) => {
17-
// request.server.app.ipfs.id((err, id) => {
18-
// if (err) { return reply(boom.badRequest(err)) }
19-
// return reply(id)
20-
// })
36+
exports.add = {
37+
parseArgs: exports.parseKey,
38+
handler (request, reply) {
39+
const ipfs = request.server.app.ipfs
40+
const addr = request.pre.args.addr
41+
42+
ipfs.bootstrap.add(addr.toString(), (err, list) => {
43+
if (err) {
44+
return reply(boom.badRequest(err))
45+
}
46+
return reply()
47+
})
48+
}
2149
}
2250

23-
exports.rm = (request, reply) => {
24-
// request.server.app.ipfs.id((err, id) => {
25-
// if (err) { return reply(boom.badRequest(err)) }
26-
// return reply(id)
27-
// })
51+
exports.rm = {
52+
parseArgs: exports.parseKey,
53+
handler (request, reply) {
54+
const ipfs = request.server.app.ipfs
55+
const addr = request.pre.args.addr
56+
57+
ipfs.bootstrap.rm(addr.toString(), (err, list) => {
58+
if (err) {
59+
return reply(boom.badRequest(err))
60+
}
61+
return reply()
62+
})
63+
}
2864
}

0 commit comments

Comments
 (0)