diff --git a/examples/add.js b/examples/add.js index 3a449e712..f34d87fee 100644 --- a/examples/add.js +++ b/examples/add.js @@ -2,8 +2,8 @@ var ipfs = require('../')('localhost', 5001) -var f1 = 'Hello', - f2 = 'World' +var f1 = 'Hello' +var f2 = 'World' ipfs.add([new Buffer(f1), new Buffer(f2)], function (err, res) { if (err || !res) return console.log(err) diff --git a/index.js b/index.js index 7999a0415..91e87ad28 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict' var fs = require('fs') +var path = require('path') var http = require('http') var qs = require('querystring') var multiaddr = require('multiaddr') @@ -9,10 +10,11 @@ var MultipartDir = require('./multipartdir.js') var stream = require('stream') var streamifier = require('streamifier') +var pkg try { - var pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json')) + pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json')) } catch(e) { - var pkg = { name: 'ipfs-api-browserify', version: '?' } + pkg = { name: 'ipfs-api-browserify', version: '?' } } var API_PATH = '/api/v0/' @@ -31,7 +33,8 @@ module.exports = function (host_or_multiaddr, port) { if (!port) port = 5001 function send (path, args, opts, files, buffer, cb) { - var query, stream, contentType = 'application/json' + var query, stream, contentType + contentType = 'application/json' if (Array.isArray(path)) path = path.join('/') @@ -64,7 +67,8 @@ module.exports = function (host_or_multiaddr, port) { }, withCredentials: false }, function (res) { - var data = '', objects = [] + var data = '' + var objects = [] var stream = !!res.headers['x-stream-output'] var chunkedObjects = !!res.headers['x-chunked-output'] @@ -127,7 +131,14 @@ module.exports = function (host_or_multiaddr, port) { file = files[i] - if (Buffer.isBuffer(file)) { + if (typeof file === 'string') { + file = new File({ + cwd: path.dirname(file), + base: path.dirname(file), + path: file, + contents: fs.createReadStream(file) + }) + } else if (Buffer.isBuffer(file)) { file = new File({ cwd: '/', base: '/', diff --git a/multipartdir.js b/multipartdir.js index aa852a54f..941c71718 100644 --- a/multipartdir.js +++ b/multipartdir.js @@ -53,6 +53,10 @@ function constructPath (curr, path) { } function addFile (root, file) { + if (!file.base || !file.path) { + throw new Error('addFile takes vinyl files') + } + var relative = Path.relative(file.base, file.path) var relative_dir = Path.dirname(relative) var folder, info diff --git a/package.json b/package.json index 7f9effb0d..8ac5d144e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "scripts": { "test": "node_modules/.bin/mocha", - "lint": "git diff --name-only --cached --relative | egrep .js$ | xargs --no-run-if-empty node_modules/.bin/standard" + "lint": "git diff --name-only --cached --relative | egrep .js$ | xargs node_modules/.bin/standard" }, "pre-commit": [ "lint" diff --git a/test/test.js b/test/test.js index d2f45e370..ce6cce9ae 100644 --- a/test/test.js +++ b/test/test.js @@ -34,7 +34,7 @@ describe('ipfs node api', function () { cwd: path.dirname(fileName), base: path.dirname(fileName), path: fileName, - contents: fs.createReadStream(fileName) + contents: fs.createReadStream(fileName) }) ipfs.add(file, function (err, res) { if (err) throw err @@ -62,6 +62,19 @@ describe('ipfs node api', function () { assert.equal(bufferAdded[0].Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') }) + var filePathAdded + before(function (done) { + ipfs.add(fileName, function (err, res) { + if (err) throw err + filePathAdded = res + done() + }) + }) + + it('add path', function () { + assert.equal(filePathAdded[0].Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + }) + var catted before(function (done) { ipfs.cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, stream) {