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

Commit

Permalink
new add, still needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Apr 24, 2016
1 parent e768675 commit 4e5df90
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 27 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
"hapi": "^13.3.0",
"ipfs-api": "^3.0.1",
"ipfs-blocks": "^0.2.0",
"ipfs-unixfs-engine": "^0.4.2",
"ipfs-merkle-dag": "^0.4.0",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.6.1",
"ipfs-unixfs-engine": "git://github.com/ipfs/js-ipfs-unixfs-engine.git#fix/race",
"joi": "^8.0.2",
"libp2p-ipfs": "^0.3.1",
"lodash.get": "^4.2.1",
Expand All @@ -77,6 +77,7 @@
"peer-id": "^0.6.6",
"peer-info": "^0.6.2",
"ronin": "^0.3.11",
"streamifier": "^0.1.1",
"temp": "^0.8.3"
},
"aegir": {
Expand Down Expand Up @@ -111,4 +112,4 @@
"kumavis <kumavis@users.noreply.github.com>",
"nginnever <ginneversource@gmail.com>"
]
}
}
120 changes: 106 additions & 14 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,87 @@
'use strict'

const Command = require('ronin').Command
const IPFS = require('../../../core')
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:version')
log.error = debug('cli:version:error')
const bs58 = require('bs58')
const streamifier = require('streamifier')
const fs = require('fs')
const async = require('async')
const pathj = require('path')

function addStream (pair) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
if (utils.isDaemonOn()) {
throw new Error('daemon running is not supported yet')
/*return ipfs.add(pair.stream, (err, res) => {
if (err) {
log.error(err)
throw err
}
console.log('added', res[0].Hash)
})*/
}
console.log(pair.path)
ipfs.files.add(pair, (err, res) => {
if (err) {
throw err
}
res.on('file', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
res.finish()
})
})
}


function addDir (path) {
const files = fs.readdirSync(path)
//console.log(path)
async.forEachSeries(files, (res, callback) => {
var nestedPath = pathj.join(path, res)
const l = process.cwd().length
const filepath = nestedPath.substring(l + 1, nestedPath.length)
//console.log(filepath)
const stat = fs.statSync(nestedPath)
if (stat.isFile()) {
const buffered = fs.readFileSync(nestedPath)
const r = streamifier.createReadStream(buffered)
const filePair = {path: filepath, stream: r}
addStream(filePair)
}
if (stat.isDirectory()) {
addDir(nestedPath)
}
callback()
}, (err) => {
if (err) {
throw err
}
console.log('done')
return
})
}

function readPath (recursive, path) {
console.log(utils.isDaemonOn())
//console.log(path)
const stats = fs.statSync(path)
if (stats.isFile()) {
const buffered = fs.readFileSync(path)
const r = streamifier.createReadStream(buffered)
path = path.substring(path.lastIndexOf('/') + 1, path.length)
const filePair = {path: path, stream: r}
addStream(filePair)
} else if (stats.isDirectory() && recursive) {
addDir(path)
}
}

module.exports = Command.extend({
desc: 'Add a file to IPFS using the UnixFS data format',
Expand All @@ -19,19 +95,35 @@ module.exports = Command.extend({
},

run: (recursive, path) => {
var node = new IPFS()
if (path.charAt(0) !== '/') {
path = process.cwd() + '/' + path
if (!path) {
throw new Error('Error: Argument \'path\' is required')
}
node.files.add(path, {
recursive: recursive
}, (err, stats) => {
if (err) {
return console.log(err)
}
if (stats) {
console.log('added', bs58.encode(stats.Hash).toString(), stats.Name)
}
})
if (path === '.' && recursive === false) {
console.log('Error: ' + path + ' is a directory, use the \'-r\' flag to specify directories')
} else if (path === '.' && recursive === true) {
path = process.cwd()
}
readPath(recursive, path)

// console.log(utils.isDaemonOn())
// utils.getIPFS((err, ipfs) => {
// if (err) {
// throw err
// }
// //console.log(ipfs)
// if (path.charAt(0) !== '/') {
// path = process.cwd() + '/' + path
// }
// ipfs.files.add(path, {
// recursive: recursive
// }, (err, stats) => {
// if (err) {
// return console.log(err)
// }
// if (stats) {
// console.log('added', bs58.encode(stats.Hash).toString(), stats.Name)
// }
// })
// })
}
})
15 changes: 4 additions & 11 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,10 @@ function IPFS (repo) {
}

this.files = {
add: (path, options, callback) => {
options.path = path
options.dagService = dagS
options.recursive = options

importer.import(path, options.dagService, options, function (err, stat) {
if (err) {
callback(err, null)
}
callback(null, stat)
})
add: (pair, callback) => {
const i = new importer(dagS)
i.add(pair)
callback(null, i)
},
cat: (hash, callback) => {
dagS.get(hash, (err, fetchedNode) => {
Expand Down

0 comments on commit 4e5df90

Please sign in to comment.