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

update/files add interface #306

Merged
merged 7 commits into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"form-data": "^1.0.0-rc3",
"gulp": "^3.9.1",
"idb-plus-blob-store": "^1.1.2",
"interface-ipfs-core": "^0.2.0",
"interface-ipfs-core": "^0.2.2",
"left-pad": "^1.1.0",
"lodash": "^4.11.2",
"mocha": "^2.5.1",
Expand All @@ -67,7 +67,7 @@
"glob": "^7.0.3",
"hapi": "^13.4.1",
"ipfs-bitswap": "^0.4.1",
"ipfs-api": "^5.0.0",
"ipfs-api": "^5.0.1",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.4.0",
"ipfs-merkle-dag": "^0.6.0",
Expand Down
59 changes: 47 additions & 12 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
'use strict'

const Importer = require('ipfs-unixfs-engine').importer
const Exporter = require('ipfs-unixfs-engine').exporter
const unixfsEngine = require('ipfs-unixfs-engine')
const Importer = unixfsEngine.Importer
const Exporter = unixfsEngine.Exporter
const UnixFS = require('ipfs-unixfs')
const bs58 = require('bs58')
const through = require('through2')
const isStream = require('isstream')
const promisify = require('promisify-es6')
const Duplex = require('stream').Duplex
const multihashes = require('multihashes')

module.exports = function files (self) {
return {
createAddStream: promisify((callback) => {
// TODO: wip
if (data === undefined) {
return new Importer(self._dagS)
createAddStream: (callback) => {
const i = new Importer(self._dagS)
const ds = new Duplex({ objectMode: true })

ds._read = (n) => {}
ds._write = (file, enc, next) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this passthrough needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a transform, on the way up

i.write(file)
next()
}
}),

ds.end = () => {
i.end()
}

let counter = 0

i.on('data', (file) => {
counter++
self.object.get(file.multihash, (err, node) => {
if (err) {
return ds.emit('error', err)
}
ds.push({path: file.path, node: node})
counter--
})
})

i.on('end', () => {
function canFinish () {
if (counter === 0) {
ds.push(null)
} else {
setTimeout(canFinish, 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this 100ms timeout loop?

Copy link
Member Author

Choose a reason for hiding this comment

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

Cause getting the object is an Async operation and the underlying stream from the importer won't care to wait that we have pushed all the objects to the upper stream and emit them all and then the 'end' event. This avoid a racing condition.

}
}
canFinish()
})

callback(null, ds)
},
add: promisify((data, callback) => {
// Buffer input
if (Buffer.isBuffer(data)) {
Expand All @@ -33,7 +68,7 @@ module.exports = function files (self) {
}]
}
if (!callback || typeof callback !== 'function') {
callback = function oop () {}
callback = function noop () {}
}
if (!Array.isArray(data)) {
return callback(new Error('"data" must be an array of { path: string, content: Buffer|Readable } or Buffer or Readable'))
Expand All @@ -43,8 +78,8 @@ module.exports = function files (self) {
const res = []

// Transform file info tuples to DAGNodes
i.pipe(through.obj(function transform (info, enc, next) {
const mh = bs58.encode(info.multihash).toString()
i.pipe(through.obj((info, enc, next) => {
const mh = multihashes.toB58String(info.multihash)
self._dagS.get(mh, (err, node) => {
if (err) return callback(err)
var obj = {
Expand All @@ -54,7 +89,7 @@ module.exports = function files (self) {
res.push(obj)
next()
})
}, function end (done) {
}, (done) => {
callback(null, res)
}))

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

before((done) => {
before(() => {
api = httpAPI.server.select('API')
done()
})
Copy link
Member

Choose a reason for hiding this comment

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

no need for done


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