forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.js
176 lines (164 loc) · 4.74 KB
/
add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict'
const Command = require('ronin').Command
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:version')
log.error = debug('cli:version:error')
const bs58 = require('bs58')
const fs = require('fs')
const parallelLimit = require('run-parallel-limit')
const async = require('async')
const path = require('path')
const glob = require('glob')
function checkPath (inPath, recursive) {
// This function is to check for the following possible inputs
// 1) "." add the cwd but throw error for no recursion flag
// 2) "." -r return the cwd
// 3) "/some/path" but throw error for no recursion
// 4) "/some/path" -r
// 5) No path, throw err
// 6) filename.type return the cwd + filename
if (!inPath) {
throw new Error('Error: Argument \'path\' is required')
}
if (inPath === '.') {
inPath = process.cwd()
}
if (fs.statSync(inPath).isDirectory() && recursive === false) {
throw new Error(`Error: ${inPath} is a directory, use the '-r' flag to specify directories`)
}
return inPath
}
function daemonOn (res, inPath, ipfs) {
const files = []
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
if (fs.statSync(element).isDirectory()) {
callback()
} else {
const filePair = {
path: element.substring(index + 1, element.length),
content: fs.createReadStream(element)
}
files.push(filePair)
callback()
}
}, (err) => {
if (err) {
throw err
}
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
res.forEach((goRes) => {
console.log('added', goRes.Hash, goRes.Name)
})
})
})
} else {
const filePair = {
path: inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length),
content: fs.createReadStream(inPath)
}
files.push(filePair)
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
console.log('added', res[0].Hash, res[0].Name)
})
}
return
}
module.exports = Command.extend({
desc: 'Add a file to IPFS using the UnixFS data format',
options: {
recursive: {
alias: 'r',
type: 'boolean',
default: false
}
},
run: (recursive, inPath) => {
let rs
inPath = checkPath(inPath, recursive)
glob(path.join(inPath, '/**/*'), (err, res) => {
if (err) {
throw err
}
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
if (utils.isDaemonOn()) {
daemonOn(res, inPath, ipfs)
} else {
ipfs.files.add((err, i) => {
if (err) {
throw err
}
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
element.substring(index + 1, element.length)
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
} else {
fs.readdir(element, (err, files) => {
if (err) {
throw err
}
if (files.length === 0) {
i.write({
path: element.substring(index + 1, element.length),
stream: null
})
}
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
})
} else {
if (!fs.statSync(inPath).isDirectory()) {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
} else {
fs.readdir(inPath, (err, files) => {
if (err) {
throw err
}
if (files.length === 0) {
i.write({
path: inPath,
stream: null
})
}
})
}
}
})
}
})
})
}
})