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

updated cat core to return just file stream #253

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,41 @@ module.exports = Command.extend({
if (err) {
throw err
}
const i = ipfs.files.add()
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
ipfs.files.add((err, i) => {
if (err) {
throw err
}
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
}
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
})
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
}
})
})
})
}
Expand Down
7 changes: 3 additions & 4 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ module.exports = Command.extend({
})
return
}
ipfs.files.cat(path, (err, res) => {
ipfs.files.cat(path, (err, file) => {
if (err) {
throw (err)
}
res.on('data', (data) => {
data.stream.pipe(process.stdout)
})
console.log(file)
file.pipe(process.stdout)
Copy link
Member

@daviddias daviddias May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why console.log and file.pipe(process.stdout?

The test for cat is not checking the output, and that is why this got missed, please update the test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whooops, this is using the interface-core tests now :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on the CLI, which is not covered by the interface-ipfs-core tests. If you check the cli-test, you are not checking the output of the CLI.

})
})
}
Expand Down
35 changes: 20 additions & 15 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
const Importer = require('ipfs-unixfs-engine').importer
const Exporter = require('ipfs-unixfs-engine').exporter
const UnixFS = require('ipfs-unixfs')
const promisify = require('promisify-es6')

module.exports = function files (self) {
return {
add: (arr, callback) => {
add: promisify((arr, cb) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our pattern is to use callback as the most top level callback, so that it is easy distinguishable when we jump out the func

Copy link
Member

@dignifiedquire dignifiedquire May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's just @diasdavid s pattern 😛

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha to callback or cb, i'll switch back to callback here

if (typeof arr === 'function') {
callback = arr
cb = arr
arr = undefined
}
if (callback === undefined) {
callback = function noop () {}
if (cb === undefined) {
cb = function noop () {}
}
if (arr === undefined) {
return new Importer(self._dagS)
cb(null, new Importer(self._dagS))
}

const i = new Importer(self._dagS)
Expand All @@ -26,32 +27,36 @@ module.exports = function files (self) {
})

i.once('end', () => {
callback(null, res)
cb(null, res)
})

arr.forEach((tuple) => {
i.write(tuple)
})

i.end()
},
cat: (hash, callback) => {
}),

cat: promisify((hash, cb) => {
self._dagS.get(hash, (err, fetchedNode) => {
if (err) {
return callback(err, null)
return cb(err, null)
}
const data = UnixFS.unmarshal(fetchedNode.data)
if (data.type === 'directory') {
callback('This dag node is a directory', null)
cb('This dag node is a directory', null)
} else {
const exportStream = Exporter(hash, self._dagS)
callback(null, exportStream)
exportStream.once('data', (object) => {
cb(null, object.stream)
})
}
})
},
get: (hash, callback) => {
}),

get: promisify((hash, cb) => {
var exportFile = Exporter(hash, self._dagS)
callback(null, exportFile)
}
cb(null, exportFile)
})
}
}
4 changes: 1 addition & 3 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ exports.cat = {
Code: 0
}).code(500)
}
stream.on('data', (data) => {
return reply(data.stream)
})
return reply(stream)
})
}
}
68 changes: 12 additions & 56 deletions test/core-tests/test-files.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,20 @@
/* eslint-env mocha */
'use strict'

const bl = require('bl')
const expect = require('chai').expect
const Readable = require('stream').Readable
const bs58 = require('bs58')
const test = require('interface-ipfs-core')

const IPFS = require('../../src/core')

describe('files', () => {
let ipfs

before((done) => {
ipfs = new IPFS(require('./repo-path'))
ipfs.load(done)
})

it('add', (done) => {
const buffered = new Buffer('some data')
const rs = new Readable()
rs.push(buffered)
rs.push(null)
const arr = []
const filePair = {path: 'data.txt', stream: rs}
arr.push(filePair)
ipfs.files.add(arr, (err, res) => {
expect(err).to.not.exist
expect(res[0].path).to.equal('data.txt')
expect(res[0].size).to.equal(17)
expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS')
done()
const common = {
setup: function (cb) {
const ipfs = new IPFS(require('./repo-path'))
ipfs.load(() => {
cb(null, ipfs)
})
})
},
teardown: function (cb) {
cb()
}
}

it('cat', (done) => {
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
ipfs.files.cat(hash, (err, res) => {
expect(err).to.not.exist
res.on('data', (data) => {
data.stream.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.equal('hello world\n')
done()
}))
})
})
})

it('get', (done) => {
// TODO create non-trival get test
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
ipfs.files.get(hash, (err, res) => {
expect(err).to.not.exist
res.on('data', (data) => {
data.stream.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.equal('hello world\n')
done()
}))
})
})
})
})
test.files(common)