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

Commit 36ef0a9

Browse files
committed
feat: add wrapWithDirectory flag to files.add et al
1 parent 8e3ea44 commit 36ef0a9

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
"form-data": "^2.3.2",
7575
"go-ipfs-dep": "^0.4.13",
7676
"hat": "0.0.3",
77+
"interface-ipfs-core": "^0.60.1",
7778
"ipfsd-ctl": "~0.30.1",
78-
"interface-ipfs-core": "~0.58.0",
7979
"lodash": "^4.17.5",
8080
"mocha": "^5.0.4",
8181
"ncp": "^2.0.0",
@@ -107,7 +107,7 @@
107107
"hapi-set-header": "^1.0.2",
108108
"hoek": "^5.0.3",
109109
"human-to-milliseconds": "^1.0.0",
110-
"ipfs-api": "^18.2.1",
110+
"ipfs-api": "^20.0.0",
111111
"ipfs-bitswap": "~0.19.0",
112112
"ipfs-block": "~0.6.1",
113113
"ipfs-block-service": "~0.13.0",

src/cli/commands/files/add.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const utils = require('../../utils')
1414
const print = require('../../utils').print
1515
const createProgressBar = require('../../utils').createProgressBar
1616

17-
const WRAPPER = 'wrapper/'
18-
1917
function checkPath (inPath, recursive) {
2018
// This function is to check for the following possible inputs
2119
// 1) "." add the cwd but throw error for no recursion flag
@@ -58,7 +56,6 @@ function getTotalBytes (path, recursive, cb) {
5856

5957
function addPipeline (index, addStream, list, argv) {
6058
const {
61-
wrapWithDirectory,
6259
quiet,
6360
quieter,
6461
silent
@@ -78,17 +75,9 @@ function addPipeline (index, addStream, list, argv) {
7875
pull.filter((file) => !file.isDirectory),
7976
pull.map((file) => ({
8077
path: file.path.substring(index, file.path.length),
81-
originalPath: file.path
82-
})),
83-
pull.map((file) => ({
84-
path: wrapWithDirectory ? WRAPPER + file.path : file.path,
85-
content: fs.createReadStream(file.originalPath)
78+
content: fs.createReadStream(file.path)
8679
})),
8780
addStream,
88-
pull.map((file) => ({
89-
hash: file.hash,
90-
path: wrapWithDirectory ? file.path.substring(WRAPPER.length) : file.path
91-
})),
9281
pull.collect((err, added) => {
9382
if (err) {
9483
throw err
@@ -191,7 +180,8 @@ module.exports = {
191180
: Infinity,
192181
cidVersion: argv.cidVersion,
193182
rawLeaves: argv.rawLeaves,
194-
onlyHash: argv.onlyHash
183+
onlyHash: argv.onlyHash,
184+
wrapWithDirectory: argv.wrapWithDirectory
195185
}
196186

197187
// Temporary restriction on raw-leaves:

src/core/components/files.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const OtherBuffer = require('buffer').Buffer
1717
const CID = require('cids')
1818
const toB58String = require('multihashes').toB58String
1919

20+
const WRAPPER = 'wrapper/'
21+
2022
function noop () {}
2123

2224
function prepareFile (self, opts, file, callback) {
@@ -33,16 +35,17 @@ function prepareFile (self, opts, file, callback) {
3335

3436
const b58Hash = cid.toBaseEncodedString()
3537

38+
3639
cb(null, {
37-
path: file.path || b58Hash,
40+
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
3841
hash: b58Hash,
3942
size: node.size
4043
})
4144
}
4245
], callback)
4346
}
4447

45-
function normalizeContent (content) {
48+
function normalizeContent (opts, content) {
4649
if (!Array.isArray(content)) {
4750
content = [content]
4851
}
@@ -68,6 +71,14 @@ function normalizeContent (content) {
6871
}
6972
}
7073

74+
if (opts.wrapWithDirectory && !data.path) {
75+
throw new Error('Must provide a path when wrapping with a directory')
76+
}
77+
78+
if (opts.wrapWithDirectory) {
79+
data.path = WRAPPER + data.path
80+
}
81+
7182
return data
7283
})
7384
}
@@ -119,7 +130,7 @@ module.exports = function files (self) {
119130

120131
opts.progress = progress
121132
return pull(
122-
pull.map(normalizeContent),
133+
pull.map(normalizeContent.bind(null, opts)),
123134
pull.flatten(),
124135
importer(self._ipld, opts),
125136
pull.asyncMap(prepareFile.bind(null, self, opts))

src/http/api/resources/files.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ exports.add = {
148148
then: Joi.boolean().valid(false).required(),
149149
otherwise: Joi.boolean().valid(false)
150150
}),
151-
'only-hash': Joi.boolean()
151+
'only-hash': Joi.boolean(),
152+
'wrap-with-directory': Joi.boolean()
152153
})
153154
// TODO: Necessary until validate "recursive", "stream-channels" etc.
154155
.options({ allowUnknown: true })
@@ -207,7 +208,8 @@ exports.add = {
207208
cidVersion: request.query['cid-version'],
208209
rawLeaves: request.query['raw-leaves'],
209210
progress: request.query.progress ? progressHandler : null,
210-
onlyHash: request.query['only-hash']
211+
onlyHash: request.query['only-hash'],
212+
wrapWithDirectory: request.query['wrap-with-directory']
211213
}
212214

213215
const aborter = abortable()
@@ -245,7 +247,7 @@ exports.add = {
245247
ipfs.files.addPullStream(options),
246248
pull.map((file) => {
247249
return {
248-
Name: file.path ? file.path : file.hash,
250+
Name: file.path, //addPullStream already turned this into a hash if it wanted to
249251
Hash: file.hash,
250252
Size: file.size
251253
}

0 commit comments

Comments
 (0)