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

Make ipfs.files.add return DAGNodes #261

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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"ipfs-merkle-dag": "^0.6.0",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.8.0",
"ipfs-unixfs-engine": "^0.8.0",
"ipfs-unixfs-engine": "^0.9.0",
"isstream": "^0.1.2",
"joi": "^8.0.5",
"libp2p-ipfs": "^0.10.1",
"libp2p-ipfs-browser": "^0.9.1",
Expand All @@ -92,7 +93,8 @@
"run-parallel-limit": "^1.0.3",
"run-series": "^1.1.4",
"run-waterfall": "^1.1.3",
"temp": "^0.8.3"
"temp": "^0.8.3",
"through2": "^2.0.1"
},
"contributors": [
"Andrew de Andrade <[email protected]>",
Expand All @@ -111,4 +113,4 @@
"kumavis <[email protected]>",
"nginnever <[email protected]>"
]
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = Command.extend({
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
content: fs.createReadStream(element)
})
}
callback()
Expand All @@ -86,7 +86,7 @@ module.exports = Command.extend({
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
filePair = {path: inPath, content: rs}
i.write(filePair)
i.end()
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = Command.extend({
throw (err)
}
res.on('data', (data) => {
data.stream.pipe(process.stdout)
data.content.pipe(process.stdout)
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function fileHandler (result, dir) {
const dirPath = path.join(dir, file.path)
// Check to see if the result is a directory
if (file.dir === false) {
file.stream.pipe(fs.createWriteStream(dirPath))
file.content.pipe(fs.createWriteStream(dirPath))
} else {
ensureDir(dirPath, (err) => {
if (err) {
Expand All @@ -64,7 +64,7 @@ function fileHandler (result, dir) {
throw err
}

file.stream.pipe(fs.createWriteStream(dirPath))
file.content.pipe(fs.createWriteStream(dirPath))
})
}
}
Expand Down
62 changes: 46 additions & 16 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,68 @@
const Importer = require('ipfs-unixfs-engine').importer
const Exporter = require('ipfs-unixfs-engine').exporter
const UnixFS = require('ipfs-unixfs')
const bs58 = require('bs58')
const through = require('through2')
const isStream = require('isstream')
const promisify = require('promisify-es6')

module.exports = function files (self) {
return {
add: (arr, callback) => {
if (typeof arr === 'function') {
callback = arr
arr = undefined
createAddStream: promisify((callback) => {
// TODO: wip
if (data === undefined) {
return new Importer(self._dagS)
}
if (callback === undefined) {
callback = function noop () {}
}),

add: promisify((data, callback) => {
// Buffer input
if (Buffer.isBuffer(data)) {
data = [{
path: '',
content: data
}]
}
if (arr === undefined) {
return new Importer(self._dagS)
// Readable stream input
if (isStream.isReadable(data)) {
data = [{
path: '',
content: data
}]
}
if (!callback || typeof callback !== 'function') {
callback = function oop () {}
}
if (!Array.isArray(data)) {
return callback(new Error('"data" must be an array of { path: string, content: Buffer|Readable } or Buffer or Readable'))
}

const i = new Importer(self._dagS)
const res = []

i.on('data', (info) => {
res.push(info)
})

i.once('end', () => {
// Transform file info tuples to DAGNodes
i.pipe(through.obj(function transform (info, enc, next) {
const mh = bs58.encode(info.multihash).toString()
self._dagS.get(mh, (err, node) => {
if (err) return callback(err)
var obj = {
path: info.path || mh,
node: node
}
res.push(obj)
next()
})
}, function end (done) {
callback(null, res)
})
}))

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

i.end()
},
}),

cat: (hash, callback) => {
self._dagS.get(hash, (err, fetchedNode) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/ipfs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = function init (self) {
const rs = new Readable()
rs.push(fs.readFileSync(element))
rs.push(null)
const filePair = {path: addPath, stream: rs}
const filePair = {path: addPath, content: rs}
i.write(filePair)
}
callback()
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ exports.cat = {
}).code(500)
}
stream.on('data', (data) => {
return reply(data.stream)
return reply(data.content)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const createTempNode = require('../utils/temp-node')
const repoPath = require('./index').repoPath

describe('bitswap', function () {
this.timeout(20000)
this.timeout(40000)
const env = _.clone(process.env)
env.IPFS_PATH = repoPath

Expand Down
5 changes: 3 additions & 2 deletions test/core/both/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ describe('bitswap', () => {
})

afterEach((done) => {
setTimeout(() => ipfs.goOffline(done), 500)
// ipfs.goOffline(done)
setTimeout(() => ipfs.goOffline(done), 1500)
})

it('2 peers', (done) => {
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('bitswap', () => {
ipfs.files.cat(hash, (err, res) => {
expect(err).to.not.exist
res.on('file', (data) => {
data.stream.pipe(bl((err, bldata) => {
data.content.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.equal('I love IPFS <3')
cb()
Expand Down
69 changes: 12 additions & 57 deletions test/core/both/test-files.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,19 @@
/* 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('../../utils/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('../../utils/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)
2 changes: 1 addition & 1 deletion test/core/node-only/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const parallel = require('run-parallel')
const createTempNode = require('../../utils/temp-node')

describe('swarm', function () {
this.timeout(20 * 1000)
this.timeout(40 * 1000)

let nodeA
let nodeB
Expand Down
3 changes: 2 additions & 1 deletion test/http-api/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module.exports = (httpAPI) => {
describe('api', () => {
let api

it('api', () => {
before((done) => {
api = httpAPI.server.select('API')
done()
})

describe('/files/cat', () => {
Expand Down