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

Commit 4e5df90

Browse files
committed
new add, still needs work
1 parent e768675 commit 4e5df90

File tree

3 files changed

+113
-27
lines changed

3 files changed

+113
-27
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565
"hapi": "^13.3.0",
6666
"ipfs-api": "^3.0.1",
6767
"ipfs-blocks": "^0.2.0",
68-
"ipfs-unixfs-engine": "^0.4.2",
6968
"ipfs-merkle-dag": "^0.4.0",
7069
"ipfs-multipart": "^0.1.0",
7170
"ipfs-repo": "^0.6.1",
71+
"ipfs-unixfs-engine": "git://github.com/ipfs/js-ipfs-unixfs-engine.git#fix/race",
7272
"joi": "^8.0.2",
7373
"libp2p-ipfs": "^0.3.1",
7474
"lodash.get": "^4.2.1",
@@ -77,6 +77,7 @@
7777
"peer-id": "^0.6.6",
7878
"peer-info": "^0.6.2",
7979
"ronin": "^0.3.11",
80+
"streamifier": "^0.1.1",
8081
"temp": "^0.8.3"
8182
},
8283
"aegir": {
@@ -111,4 +112,4 @@
111112
"kumavis <[email protected]>",
112113
"nginnever <[email protected]>"
113114
]
114-
}
115+
}

src/cli/commands/files/add.js

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,87 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../core')
4+
const utils = require('../../utils')
55
const debug = require('debug')
66
const log = debug('cli:version')
77
log.error = debug('cli:version:error')
88
const bs58 = require('bs58')
9+
const streamifier = require('streamifier')
10+
const fs = require('fs')
11+
const async = require('async')
12+
const pathj = require('path')
13+
14+
function addStream (pair) {
15+
utils.getIPFS((err, ipfs) => {
16+
if (err) {
17+
throw err
18+
}
19+
if (utils.isDaemonOn()) {
20+
throw new Error('daemon running is not supported yet')
21+
/*return ipfs.add(pair.stream, (err, res) => {
22+
if (err) {
23+
log.error(err)
24+
throw err
25+
}
26+
console.log('added', res[0].Hash)
27+
})*/
28+
}
29+
console.log(pair.path)
30+
ipfs.files.add(pair, (err, res) => {
31+
if (err) {
32+
throw err
33+
}
34+
res.on('file', (file) => {
35+
console.log('added', bs58.encode(file.multihash).toString(), file.path)
36+
})
37+
res.finish()
38+
})
39+
})
40+
}
41+
42+
43+
function addDir (path) {
44+
const files = fs.readdirSync(path)
45+
//console.log(path)
46+
async.forEachSeries(files, (res, callback) => {
47+
var nestedPath = pathj.join(path, res)
48+
const l = process.cwd().length
49+
const filepath = nestedPath.substring(l + 1, nestedPath.length)
50+
//console.log(filepath)
51+
const stat = fs.statSync(nestedPath)
52+
if (stat.isFile()) {
53+
const buffered = fs.readFileSync(nestedPath)
54+
const r = streamifier.createReadStream(buffered)
55+
const filePair = {path: filepath, stream: r}
56+
addStream(filePair)
57+
}
58+
if (stat.isDirectory()) {
59+
addDir(nestedPath)
60+
}
61+
callback()
62+
}, (err) => {
63+
if (err) {
64+
throw err
65+
}
66+
console.log('done')
67+
return
68+
})
69+
}
70+
71+
function readPath (recursive, path) {
72+
console.log(utils.isDaemonOn())
73+
//console.log(path)
74+
const stats = fs.statSync(path)
75+
if (stats.isFile()) {
76+
const buffered = fs.readFileSync(path)
77+
const r = streamifier.createReadStream(buffered)
78+
path = path.substring(path.lastIndexOf('/') + 1, path.length)
79+
const filePair = {path: path, stream: r}
80+
addStream(filePair)
81+
} else if (stats.isDirectory() && recursive) {
82+
addDir(path)
83+
}
84+
}
985

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

2197
run: (recursive, path) => {
22-
var node = new IPFS()
23-
if (path.charAt(0) !== '/') {
24-
path = process.cwd() + '/' + path
98+
if (!path) {
99+
throw new Error('Error: Argument \'path\' is required')
25100
}
26-
node.files.add(path, {
27-
recursive: recursive
28-
}, (err, stats) => {
29-
if (err) {
30-
return console.log(err)
31-
}
32-
if (stats) {
33-
console.log('added', bs58.encode(stats.Hash).toString(), stats.Name)
34-
}
35-
})
101+
if (path === '.' && recursive === false) {
102+
console.log('Error: ' + path + ' is a directory, use the \'-r\' flag to specify directories')
103+
} else if (path === '.' && recursive === true) {
104+
path = process.cwd()
105+
}
106+
readPath(recursive, path)
107+
108+
// console.log(utils.isDaemonOn())
109+
// utils.getIPFS((err, ipfs) => {
110+
// if (err) {
111+
// throw err
112+
// }
113+
// //console.log(ipfs)
114+
// if (path.charAt(0) !== '/') {
115+
// path = process.cwd() + '/' + path
116+
// }
117+
// ipfs.files.add(path, {
118+
// recursive: recursive
119+
// }, (err, stats) => {
120+
// if (err) {
121+
// return console.log(err)
122+
// }
123+
// if (stats) {
124+
// console.log('added', bs58.encode(stats.Hash).toString(), stats.Name)
125+
// }
126+
// })
127+
// })
36128
}
37129
})

src/core/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,10 @@ function IPFS (repo) {
389389
}
390390

391391
this.files = {
392-
add: (path, options, callback) => {
393-
options.path = path
394-
options.dagService = dagS
395-
options.recursive = options
396-
397-
importer.import(path, options.dagService, options, function (err, stat) {
398-
if (err) {
399-
callback(err, null)
400-
}
401-
callback(null, stat)
402-
})
392+
add: (pair, callback) => {
393+
const i = new importer(dagS)
394+
i.add(pair)
395+
callback(null, i)
403396
},
404397
cat: (hash, callback) => {
405398
dagS.get(hash, (err, fetchedNode) => {

0 commit comments

Comments
 (0)