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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 9 deletions
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

Lines changed: 37 additions & 15 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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) {

0 commit comments

Comments
 (0)