From 8583416951a8c6957ec4513426a0d3c8d4a0b799 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 13 Feb 2016 01:57:09 +0000 Subject: [PATCH] jsipfs files add feature --- karma.conf.js | 3 ++- package.json | 1 + src/cli/commands/add.js | 10 ---------- src/cli/commands/files/add.js | 33 ++++++++++++++++++++++++++++++++ src/http-api/resources/config.js | 2 +- src/http-api/resources/object.js | 2 +- src/ipfs-core/index.js | 9 +++++++++ 7 files changed, 47 insertions(+), 13 deletions(-) delete mode 100644 src/cli/commands/add.js create mode 100644 src/cli/commands/files/add.js diff --git a/karma.conf.js b/karma.conf.js index cfd13a8e7d..5ced2208cd 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -21,7 +21,8 @@ module.exports = function (config) { }, externals: { fs: '{}', - 'node-forge': 'forge' + 'node-forge': 'forge', + 'ipfs-data-importing': '{ import: {} }' }, node: { Buffer: true diff --git a/package.json b/package.json index 0a5ce3e833..46985cece3 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "hapi": "^12.0.0", "ipfs-api": "^2.13.1", "ipfs-blocks": "^0.1.0", + "ipfs-data-importing": "^0.3.0", "ipfs-merkle-dag": "^0.2.1", "ipfs-multipart": "^0.1.0", "ipfs-repo": "^0.5.0", diff --git a/src/cli/commands/add.js b/src/cli/commands/add.js deleted file mode 100644 index af4020b384..0000000000 --- a/src/cli/commands/add.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const Command = require('ronin').Command - -module.exports = Command.extend({ - desc: '', - - run: (name) => { - } -}) diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js new file mode 100644 index 0000000000..35ca7b4a0e --- /dev/null +++ b/src/cli/commands/files/add.js @@ -0,0 +1,33 @@ +'use strict' + +const Command = require('ronin').Command +const IPFS = require('../../../ipfs-core') +const debug = require('debug') +const log = debug('cli:version') +log.error = debug('cli:version:error') +const bs58 = require('bs58') + +module.exports = Command.extend({ + desc: 'Add a file to IPFS using the UnixFS data format', + + options: { + recursive: { + alias: 'r', + type: 'boolean', + default: false + } + }, + + run: (recursive, path) => { + var node = new IPFS() + path = process.cwd() + '/' + path + node.files.add(path, { + recursive: recursive + }, (err, stats) => { + if (err) { + return console.log(err) + } + console.log('added', bs58.encode(stats.Hash).toString(), stats.Name) + }) + } +}) diff --git a/src/http-api/resources/config.js b/src/http-api/resources/config.js index 4fd922e688..0c0d9e1789 100644 --- a/src/http-api/resources/config.js +++ b/src/http-api/resources/config.js @@ -140,7 +140,7 @@ exports.replace = { } const parser = multipart.reqParser(request.payload) - let file + var file parser.on('file', (fileName, fileStream) => { fileStream.on('data', (data) => { diff --git a/src/http-api/resources/object.js b/src/http-api/resources/object.js index cc15e1cb69..4f8318a61f 100644 --- a/src/http-api/resources/object.js +++ b/src/http-api/resources/object.js @@ -104,7 +104,7 @@ exports.put = { } const parser = multipart.reqParser(request.payload) - let file + var file parser.on('file', (fileName, fileStream) => { fileStream.on('data', (data) => { diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index a969a23854..990d75101c 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -11,6 +11,7 @@ const DAGService = mDAG.DAGService const Id = require('peer-id') const Info = require('peer-info') const multiaddr = require('multiaddr') +const importer = require('ipfs-data-importing').import exports = module.exports = IPFS @@ -320,6 +321,14 @@ function IPFS (repo) { records: {}, ping: notImpl } + + this.files = { + add: (path, options, callback) => { + options.path = path + options.dagService = dagS + importer(options, callback) + } + } } function notImpl () {