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

Commit 47285a7

Browse files
victorbdaviddias
authored andcommitted
feat: wrap with directory (#1329)
* feat: add wrapWithDirectory flag to files.add et al * chore: Fix linting
1 parent e7b91ff commit 47285a7

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/cli/commands/files/add.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const utils = require('../../utils')
1515
const print = require('../../utils').print
1616
const createProgressBar = require('../../utils').createProgressBar
1717

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

6058
function addPipeline (index, addStream, list, argv) {
6159
const {
62-
wrapWithDirectory,
6360
quiet,
6461
quieter,
6562
silent
@@ -79,17 +76,9 @@ function addPipeline (index, addStream, list, argv) {
7976
pull.filter((file) => !file.isDirectory),
8077
pull.map((file) => ({
8178
path: file.path.substring(index, file.path.length),
82-
originalPath: file.path
83-
})),
84-
pull.map((file) => ({
85-
path: wrapWithDirectory ? WRAPPER + file.path : file.path,
86-
content: fs.createReadStream(file.originalPath)
79+
content: fs.createReadStream(file.path)
8780
})),
8881
addStream,
89-
pull.map((file) => ({
90-
hash: file.hash,
91-
path: wrapWithDirectory ? file.path.substring(WRAPPER.length) : file.path
92-
})),
9382
pull.collect((err, added) => {
9483
if (err) {
9584
throw err
@@ -198,7 +187,8 @@ module.exports = {
198187
cidVersion: argv.cidVersion,
199188
rawLeaves: argv.rawLeaves,
200189
onlyHash: argv.onlyHash,
201-
hashAlg: argv.hash
190+
hashAlg: argv.hash,
191+
wrapWithDirectory: argv.wrapWithDirectory
202192
}
203193

204194
// Temporary restriction on raw-leaves:

src/core/components/files.js

+13-3
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) {
@@ -34,15 +36,15 @@ function prepareFile (self, opts, file, callback) {
3436
const b58Hash = cid.toBaseEncodedString()
3537

3638
cb(null, {
37-
path: file.path || b58Hash,
39+
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
3840
hash: b58Hash,
3941
size: node.size
4042
})
4143
}
4244
], callback)
4345
}
4446

45-
function normalizeContent (content) {
47+
function normalizeContent (opts, content) {
4648
if (!Array.isArray(content)) {
4749
content = [content]
4850
}
@@ -68,6 +70,14 @@ function normalizeContent (content) {
6870
}
6971
}
7072

73+
if (opts.wrapWithDirectory && !data.path) {
74+
throw new Error('Must provide a path when wrapping with a directory')
75+
}
76+
77+
if (opts.wrapWithDirectory) {
78+
data.path = WRAPPER + data.path
79+
}
80+
7181
return data
7282
})
7383
}
@@ -123,7 +133,7 @@ module.exports = function files (self) {
123133

124134
opts.progress = progress
125135
return pull(
126-
pull.map(normalizeContent),
136+
pull.map(normalizeContent.bind(null, opts)),
127137
pull.flatten(),
128138
importer(self._ipld, opts),
129139
pull.asyncMap(prepareFile.bind(null, self, opts))

src/http/api/resources/files.js

+5-3
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 })
@@ -208,7 +209,8 @@ exports.add = {
208209
rawLeaves: request.query['raw-leaves'],
209210
progress: request.query.progress ? progressHandler : null,
210211
onlyHash: request.query['only-hash'],
211-
hashAlg: request.query['hash']
212+
hashAlg: request.query['hash'],
213+
wrapWithDirectory: request.query['wrap-with-directory']
212214
}
213215

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

0 commit comments

Comments
 (0)