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

Commit

Permalink
fix(files.get): fix the command
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Sep 30, 2016
1 parent 9afd69b commit 7015586
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
52 changes: 37 additions & 15 deletions src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ log.error = debug('cli:files:error')
var fs = require('fs')
const path = require('path')
const pathExists = require('path-exists')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')

function checkArgs (hash, outPath) {
// format the output directory
Expand All @@ -33,30 +35,38 @@ function ensureDir (dir, cb) {
.catch(cb)
}

function fileHandler (result, dir) {
return function onFile (file) {
function fileHandler (dir) {
return function onFile (file, cb) {
const lastSlash = file.path.lastIndexOf('/')
// Check to see if the result is in a directory
if (file.path.lastIndexOf('/') === -1) {
if (lastSlash === -1) {
const dirPath = path.join(dir, file.path)
// Check to see if the result is a directory
if (file.dir === false) {
if (file.content) {
file.content.pipe(fs.createWriteStream(dirPath))
.once('error', cb)
.once('end', cb)
} else {
ensureDir(dirPath, (err) => {
if (err) {
throw err
}
})
ensureDir(dirPath, cb)
}
} else {
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
const filePath = file.path.substring(0, lastSlash + 1)
const dirPath = path.join(dir, filePath)

ensureDir(dirPath, (err) => {
if (err) {
throw err
return cb(err)
}

file.content.pipe(fs.createWriteStream(dirPath))
if (file.content) {
const filename = file.path.substring(lastSlash)
const target = path.join(dirPath, filename)

file.content.pipe(fs.createWriteStream(target))
.once('error', cb)
.once('end', cb)
}
cb()
})
}
}
Expand All @@ -76,17 +86,29 @@ module.exports = {
},

handler (argv) {
const dir = checkArgs(argv.ipfsPath, argv.output)
const ipfsPath = argv['ipfs-path']
const dir = checkArgs(ipfsPath, argv.output)

utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.files.get(argv.ipfsPath, (err, result) => {

ipfs.files.get(ipfsPath, (err, stream) => {
if (err) {
throw err
}
result.on('data', fileHandler(result, dir))

pull(
toPull.source(stream),
pull.asyncMap(fileHandler(dir)),
pull.onEnd((err) => {
console.log('finished writing')
if (err) {
throw err
}
})
)
})
})
}
Expand Down
56 changes: 27 additions & 29 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const tar = require('tar-stream')
const log = debug('http-api:files')
log.error = debug('http-api:files:error')
const pull = require('pull-stream')
const toStream = require('pull-stream-to-stream')
const toPull = require('stream-to-pull-stream')
const pushable = require('pull-pushable')
const EOL = require('os').EOL
const through = require('through2')

exports = module.exports

Expand Down Expand Up @@ -72,7 +72,7 @@ exports.get = {
const ipfs = request.server.app.ipfs
const pack = tar.pack()

ipfs.files.getPull(key, (err, stream) => {
ipfs.files.get(key, (err, stream) => {
if (err) {
log.error(err)

Expand All @@ -83,35 +83,33 @@ exports.get = {
return
}

pull(
stream,
pull.asyncMap((file, cb) => {
const header = {name: file.path}

if (!file.content) {
header.type = 'directory'
pack.entry(header)
cb()
} else {
header.size = file.size
toStream.source(file.content)
.pipe(pack.entry(header, cb))
}
}),
pull.onEnd((err) => {
if (err) {
log.error(err)

reply({
Message: 'Failed to get file: ' + err,
Code: 0
}).code(500)
return
stream.pipe(through.obj((file, enc, cb) => {
const header = {name: file.path}

if (!file.content) {
header.type = 'directory'
pack.entry(header)
cb()
} else {
header.size = file.size
const packStream = pack.entry(header, cb)
if (!packStream) {
// this happens if the request is aborted
// we just skip things then
return cb()
}
file.content.pipe(packStream)
}
}), () => {
if (err) {
log.error(err)
pack.emit('error', err)
pack.destroy()
return
}

pack.finalize()
})
)
pack.finalize()
})

// the reply must read the tar stream,
// to pull values through
Expand Down

0 comments on commit 7015586

Please sign in to comment.