Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

add path support #34

Merged
merged 3 commits into from
Jul 18, 2015
Merged
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
4 changes: 2 additions & 2 deletions examples/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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/'
Expand All @@ -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('/')

Expand Down Expand Up @@ -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']

Expand Down Expand Up @@ -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: '/',
Expand Down
4 changes: 4 additions & 0 deletions multipartdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 14 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down