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

Commit 51899e1

Browse files
committed
add, cat, get core + cli offline
1 parent 036eaf6 commit 51899e1

File tree

10 files changed

+401
-27
lines changed

10 files changed

+401
-27
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565
"ipfs-api": "^3.0.1",
6666
"ipfs-block": "^0.3.0",
6767
"ipfs-block-service": "^0.3.0",
68-
"ipfs-data-importing": "^0.3.3",
6968
"ipfs-merkle-dag": "^0.5.0",
7069
"ipfs-multipart": "^0.1.0",
7170
"ipfs-repo": "^0.7.1",
71+
"ipfs-unixfs-engine": "^0.5.0",
7272
"joi": "^8.0.2",
7373
"libp2p-ipfs": "^0.3.3",
7474
"lodash.get": "^4.2.1",
@@ -77,6 +77,7 @@
7777
"peer-book": "0.1.0",
7878
"peer-id": "^0.6.6",
7979
"peer-info": "^0.6.2",
80+
"readable-stream": "^1.1.13",
8081
"ronin": "^0.3.11",
8182
"temp": "^0.8.3"
8283
},

src/cli/commands/files/add.js

+118-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
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 Readable = require('stream').Readable
10+
const fs = require('fs')
11+
const async = require('async')
12+
const pathj = require('path')
13+
const glob = require('glob')
914

1015
module.exports = Command.extend({
1116
desc: 'Add a file to IPFS using the UnixFS data format',
@@ -19,15 +24,120 @@ module.exports = Command.extend({
1924
},
2025

2126
run: (recursive, path) => {
22-
var node = new IPFS()
23-
path = process.cwd() + '/' + path
24-
node.files.add(path, {
25-
recursive: recursive
26-
}, (err, stats) => {
27+
let s
28+
let rs
29+
30+
if (!path) {
31+
throw new Error('Error: Argument \'path\' is required')
32+
}
33+
34+
s = fs.statSync(path)
35+
36+
if (s.isDirectory() && recursive === false) {
37+
throw new Error('Error: ' + process.cwd() + ' is a directory, use the \'-r\' flag to specify directories')
38+
}
39+
if (path === '.' && recursive === true) {
40+
path = process.cwd()
41+
s = fs.statSync(process.cwd())
42+
} else if (path === '.' && recursive === false) {
43+
s = fs.statSync(process.cwd())
44+
if (s.isDirectory()) {
45+
throw new Error('Error: ' + process.cwd() + ' is a directory, use the \'-r\' flag to specify directories')
46+
}
47+
}
48+
49+
glob(pathj.join(path, '/**/*'), (err, res) => {
2750
if (err) {
28-
return console.log(err)
51+
throw err
2952
}
30-
console.log('added', bs58.encode(stats.Hash).toString(), stats.Name)
53+
utils.getIPFS((err, ipfs) => {
54+
if (err) {
55+
throw err
56+
}
57+
// var files = []
58+
if (utils.isDaemonOn()) {
59+
throw new Error('on-line mode is not supported yet')
60+
/* if (res.length !== 0) {
61+
const index = path.lastIndexOf('/')
62+
async.eachLimit(res, 10, (element, callback) => {
63+
rs = new Readable()
64+
const addPath = element.substring(index + 1, element.length)
65+
if (fs.statSync(element).isDirectory()) {
66+
callback()
67+
} else {
68+
const buffered = fs.readFileSync(element)
69+
rs.push(buffered)
70+
rs.push(null)
71+
const filePair = {path: addPath, content: rs}
72+
files.push(filePair)
73+
callback()
74+
}
75+
}, (err) => {
76+
if (err) {
77+
throw err
78+
}
79+
ipfs.add(files, (err, res) => {
80+
if (err) {
81+
throw err
82+
}
83+
res.forEach((goRes) => {
84+
console.log('added', goRes.Hash, goRes.Name)
85+
})
86+
})
87+
})
88+
} else {
89+
rs = new Readable()
90+
const buffered = fs.readFileSync(path)
91+
rs.push(buffered)
92+
rs.push(null)
93+
path = path.substring(path.lastIndexOf('/') + 1, path.length)
94+
const filePair = {path: path, content: rs}
95+
files.push(filePair)
96+
ipfs.add(files, (err, res) => {
97+
if (err) {
98+
throw err
99+
}
100+
console.log('added', res[0].Hash, res[0].Name)
101+
})
102+
}
103+
return */
104+
}
105+
const i = ipfs.files.add()
106+
i.on('data', (file) => {
107+
console.log('added', bs58.encode(file.multihash).toString(), file.path)
108+
})
109+
if (res.length !== 0) {
110+
const index = path.lastIndexOf('/')
111+
async.eachLimit(res, 10, (element, callback) => {
112+
rs = new Readable()
113+
const addPath = element.substring(index + 1, element.length)
114+
if (fs.statSync(element).isDirectory()) {
115+
callback()
116+
} else {
117+
const buffered = fs.readFileSync(element)
118+
rs.push(buffered)
119+
rs.push(null)
120+
const filePair = {path: addPath, stream: rs}
121+
i.write(filePair)
122+
callback()
123+
}
124+
}, (err) => {
125+
if (err) {
126+
throw err
127+
}
128+
i.end()
129+
})
130+
} else {
131+
rs = new Readable()
132+
const buffered = fs.readFileSync(path)
133+
path = path.substring(path.lastIndexOf('/') + 1, path.length)
134+
rs.push(buffered)
135+
rs.push(null)
136+
const filePair = {path: path, stream: rs}
137+
i.write(filePair)
138+
i.end()
139+
}
140+
})
31141
})
32142
}
33143
})

src/cli/commands/files/cat.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const debug = require('debug')
5+
const utils = require('../../utils')
6+
const log = debug('cli:files')
7+
log.error = debug('cli:files:error')
8+
9+
module.exports = Command.extend({
10+
desc: 'Download IPFS objects',
11+
12+
options: {},
13+
14+
run: (path, options) => {
15+
if (!path) {
16+
throw new Error("Argument 'path' is required")
17+
}
18+
if (!options) {
19+
options = {}
20+
}
21+
utils.getIPFS((err, ipfs) => {
22+
if (err) {
23+
throw err
24+
}
25+
if (utils.isDaemonOn()) {
26+
throw new Error('on-line mode is not supported yet')
27+
/* ipfs.cat(path, (err, res) => {
28+
if (err) {
29+
throw (err)
30+
}
31+
console.log(res.toString())
32+
})
33+
return */
34+
}
35+
36+
ipfs.files.cat(path, (err, res) => {
37+
if (err) {
38+
throw (err)
39+
}
40+
if (res) {
41+
res.on('file', (data) => {
42+
data.stream.pipe(process.stdout)
43+
})
44+
}
45+
})
46+
})
47+
}
48+
})

src/cli/commands/files/get.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const debug = require('debug')
5+
const utils = require('../../utils')
6+
const log = debug('cli:files')
7+
log.error = debug('cli:files:error')
8+
var fs = require('fs')
9+
const pathj = require('path')
10+
11+
module.exports = Command.extend({
12+
desc: 'Download IPFS objects',
13+
14+
options: {},
15+
16+
run: (path, options) => {
17+
let dir
18+
let filepath
19+
let ws
20+
21+
if (!path) {
22+
throw new Error("Argument 'path' is required")
23+
}
24+
if (!options) {
25+
options = {}
26+
dir = process.cwd()
27+
} else {
28+
if (options.slice(-1) !== '/') {
29+
options += '/'
30+
}
31+
dir = options
32+
}
33+
34+
utils.getIPFS((err, ipfs) => {
35+
if (err) {
36+
throw err
37+
}
38+
ipfs.files.get(path, (err, data) => {
39+
if (err) {
40+
throw err
41+
}
42+
data.on('file', (data) => {
43+
if (data.path.lastIndexOf('/') === -1) {
44+
filepath = data.path
45+
if (data.dir === false) {
46+
ws = fs.createWriteStream(pathj.join(dir, data.path))
47+
data.stream.pipe(ws)
48+
} else {
49+
try {
50+
fs.mkdirSync(pathj.join(dir, data.path))
51+
} catch (err) {
52+
throw err
53+
}
54+
}
55+
} else {
56+
filepath = data.path.substring(0, data.path.lastIndexOf('/') + 1)
57+
try {
58+
fs.mkdirSync(pathj.join(dir, filepath))
59+
} catch (err) {
60+
throw err
61+
}
62+
ws = fs.createWriteStream(pathj.join(dir, data.path))
63+
data.stream.pipe(ws)
64+
}
65+
})
66+
})
67+
})
68+
}
69+
})

src/core/index.js

+49-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ const DAGService = mDAG.DAGService
99
const peerId = require('peer-id')
1010
const PeerInfo = require('peer-info')
1111
const multiaddr = require('multiaddr')
12-
const importer = require('ipfs-data-importing').import
12+
const Importer = require('ipfs-unixfs-engine').importer
13+
const Exporter = require('ipfs-unixfs-engine').exporter
1314
const libp2p = require('libp2p-ipfs')
1415
const init = require('./init')
1516
const IPFSRepo = require('ipfs-repo')
1617
const PeerBook = require('peer-book')
18+
const UnixFS = require('ipfs-unixfs')
1719

1820
exports = module.exports = IPFS
1921

@@ -393,10 +395,52 @@ function IPFS (repo) {
393395
}
394396

395397
this.files = {
396-
add: (path, options, callback) => {
397-
options.path = path
398-
options.dagService = dagS
399-
importer(options, callback)
398+
add: (arr, callback) => {
399+
if (typeof arr === 'function') {
400+
callback = arr
401+
arr = undefined
402+
}
403+
if (callback === undefined) {
404+
callback = function noop () {}
405+
}
406+
if (arr === undefined) {
407+
return new Importer(dagS)
408+
}
409+
410+
const i = new Importer(dagS)
411+
const res = []
412+
413+
i.on('data', (info) => {
414+
res.push(info)
415+
})
416+
417+
i.on('end', () => {
418+
callback(null, res)
419+
})
420+
421+
arr.forEach((tuple) => {
422+
i.write(tuple)
423+
})
424+
425+
i.end()
426+
},
427+
cat: (hash, callback) => {
428+
dagS.get(hash, (err, fetchedNode) => {
429+
if (err) {
430+
return callback(err, null)
431+
}
432+
const data = UnixFS.unmarshal(fetchedNode.data)
433+
if (data.type === 'directory') {
434+
callback('This dag node is a directory', null)
435+
} else {
436+
const exportEvent = Exporter(hash, dagS)
437+
callback(null, exportEvent)
438+
}
439+
})
440+
},
441+
get: (hash, callback) => {
442+
var exportFile = Exporter(hash, dagS)
443+
callback(null, exportFile)
400444
}
401445
}
402446
}

0 commit comments

Comments
 (0)