This repository was archived by the owner on Mar 10, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 11 files changed +110
-19
lines changed Expand file tree Collapse file tree 11 files changed +110
-19
lines changed Original file line number Diff line number Diff line change @@ -20,19 +20,26 @@ module.exports = {
20
20
default : false ,
21
21
coerce : asBoolean ,
22
22
describe : 'Use long listing format.'
23
+ } ,
24
+ cidBase : {
25
+ alias : 'cid-base' ,
26
+ default : 'base58btc' ,
27
+ describe : 'CID base to use.'
23
28
}
24
29
} ,
25
30
26
31
handler ( argv ) {
27
32
let {
28
33
path,
29
34
ipfs,
30
- long
35
+ long,
36
+ cidBase
31
37
} = argv
32
38
33
39
argv . resolve (
34
40
ipfs . files . ls ( path || FILE_SEPARATOR , {
35
- long
41
+ long,
42
+ cidBase
36
43
} )
37
44
. then ( files => {
38
45
if ( long ) {
Original file line number Diff line number Diff line change @@ -41,6 +41,11 @@ Type: <type>`,
41
41
default : false ,
42
42
coerce : asBoolean ,
43
43
describe : 'Compute the amount of the dag that is local, and if possible the total size'
44
+ } ,
45
+ cidBase : {
46
+ alias : 'cid-base' ,
47
+ default : 'base58btc' ,
48
+ describe : 'CID base to use.'
44
49
}
45
50
} ,
46
51
Original file line number Diff line number Diff line change 2
2
3
3
const waterfall = require ( 'async/waterfall' )
4
4
const map = require ( 'async/map' )
5
- const bs58 = require ( 'bs58' )
6
5
const UnixFs = require ( 'ipfs-unixfs' )
7
6
const {
8
7
traverseTo,
9
8
loadNode,
9
+ formatCid,
10
10
FILE_SEPARATOR ,
11
11
FILE_TYPES
12
12
} = require ( './utils' )
13
13
14
14
const defaultOptions = {
15
- long : false
15
+ long : false ,
16
+ cidBase : 'base58btc'
16
17
}
17
18
18
19
module . exports = ( ipfs ) => {
@@ -47,7 +48,7 @@ module.exports = (ipfs) => {
47
48
done ( null , {
48
49
name : link . name ,
49
50
type : meta . type ,
50
- hash : bs58 . encode ( node . multihash ) ,
51
+ hash : formatCid ( node . multihash , options . cidBase ) ,
51
52
size : meta . fileSize ( ) || 0
52
53
} )
53
54
}
@@ -57,7 +58,7 @@ module.exports = (ipfs) => {
57
58
cb ( null , [ {
58
59
name : result . name ,
59
60
type : meta . type ,
60
- hash : bs58 . encode ( result . node . multihash ) ,
61
+ hash : formatCid ( result . node . multihash , options . cidBase ) ,
61
62
size : meta . fileSize ( ) || 0
62
63
} ] )
63
64
}
Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
3
const unmarshal = require ( 'ipfs-unixfs' ) . unmarshal
4
- const bs58 = require ( 'bs58' )
5
4
const {
6
- traverseTo
5
+ traverseTo,
6
+ formatCid
7
7
} = require ( './utils' )
8
8
const waterfall = require ( 'async/waterfall' )
9
9
const log = require ( 'debug' ) ( 'ipfs:mfs:stat' )
10
10
11
11
const defaultOptions = {
12
12
hash : false ,
13
13
size : false ,
14
- withLocal : false
14
+ withLocal : false ,
15
+ cidBase : 'base58btc'
15
16
}
16
17
17
18
module . exports = ( ipfs ) => {
@@ -32,7 +33,7 @@ module.exports = (ipfs) => {
32
33
( { node } , done ) => {
33
34
if ( options . hash ) {
34
35
return done ( null , {
35
- hash : bs58 . encode ( node . multihash )
36
+ hash : formatCid ( node . multihash , options . cidBase )
36
37
} )
37
38
} else if ( options . size ) {
38
39
return done ( null , {
@@ -49,7 +50,7 @@ module.exports = (ipfs) => {
49
50
}
50
51
51
52
done ( null , {
52
- hash : bs58 . encode ( node . multihash ) ,
53
+ hash : formatCid ( node . multihash , options . cidBase ) ,
53
54
size : meta . fileSize ( ) || 0 ,
54
55
cumulativeSize : node . size ,
55
56
blocks : blocks ,
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const CID = require ( 'cids' )
4
+
5
+ module . exports = ( cid , base ) => {
6
+ if ( Buffer . isBuffer ( cid ) ) {
7
+ cid = new CID ( cid )
8
+ }
9
+
10
+ if ( base === 'base58btc' ) {
11
+ return cid . toBaseEncodedString ( )
12
+ }
13
+
14
+ return cid . toV1 ( ) . toBaseEncodedString ( base )
15
+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ module.exports = {
9
9
createLock : require ( './create-lock' ) ,
10
10
createNode : require ( './create-node' ) ,
11
11
endPullStream : require ( './end-pull-stream' ) ,
12
+ formatCid : require ( './format-cid' ) ,
12
13
limitStreamBytes : require ( './limit-stream-bytes' ) ,
13
14
loadNode : require ( './load-node' ) ,
14
15
toSourcesAndDestination : require ( './to-sources-and-destination' ) ,
Original file line number Diff line number Diff line change @@ -13,11 +13,13 @@ const mfsLs = (api) => {
13
13
} = request . server . app
14
14
const {
15
15
arg,
16
- long
16
+ long,
17
+ cidBase
17
18
} = request . query
18
19
19
20
return ipfs . files . ls ( arg , {
20
- long
21
+ long,
22
+ cidBase
21
23
} )
22
24
. then ( files => {
23
25
reply ( {
@@ -44,7 +46,8 @@ const mfsLs = (api) => {
44
46
} ,
45
47
query : Joi . object ( ) . keys ( {
46
48
arg : Joi . string ( ) . default ( '/' ) ,
47
- long : Joi . boolean ( ) . default ( false )
49
+ long : Joi . boolean ( ) . default ( false ) ,
50
+ cidBase : Joi . string ( ) . default ( 'base58btc' )
48
51
} )
49
52
. rename ( 'l' , 'long' , {
50
53
override : true ,
Original file line number Diff line number Diff line change @@ -15,13 +15,15 @@ const mfsStat = (api) => {
15
15
arg,
16
16
hash,
17
17
size,
18
- withLocal
18
+ withLocal,
19
+ cidBase
19
20
} = request . query
20
21
21
22
return ipfs . files . stat ( arg , {
22
23
hash,
23
24
size,
24
- withLocal
25
+ withLocal,
26
+ cidBase
25
27
} )
26
28
. then ( stats => {
27
29
reply ( {
@@ -52,7 +54,8 @@ const mfsStat = (api) => {
52
54
arg : Joi . string ( ) . default ( '/' ) ,
53
55
hash : Joi . boolean ( ) . default ( false ) ,
54
56
size : Joi . boolean ( ) . default ( false ) ,
55
- withLocal : Joi . boolean ( ) . default ( false )
57
+ withLocal : Joi . boolean ( ) . default ( false ) ,
58
+ cidBase : Joi . string ( ) . default ( 'base58btc' )
56
59
} )
57
60
}
58
61
}
Original file line number Diff line number Diff line change @@ -40,5 +40,6 @@ module.exports = {
40
40
createMfs,
41
41
bufferStream : require ( './buffer-stream' ) ,
42
42
collectLeafCids : require ( './collect-leaf-cids' ) ,
43
- EMPTY_DIRECTORY_HASH : 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
43
+ EMPTY_DIRECTORY_HASH : 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' ,
44
+ EMPTY_DIRECTORY_HASH_BASE32 : 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
44
45
}
Original file line number Diff line number Diff line change @@ -139,6 +139,26 @@ describe('ls', function () {
139
139
} )
140
140
} )
141
141
142
+ it ( 'lists a file with a base32 hash' , ( ) => {
143
+ const fileName = `small-file-${ Math . random ( ) } .txt`
144
+ const content = Buffer . from ( 'Hello world' )
145
+
146
+ return mfs . write ( `/${ fileName } ` , content , {
147
+ create : true
148
+ } )
149
+ . then ( ( ) => mfs . ls ( `/${ fileName } ` , {
150
+ long : true ,
151
+ cidBase : 'base32'
152
+ } ) )
153
+ . then ( files => {
154
+ expect ( files . length ) . to . equal ( 1 )
155
+ expect ( files [ 0 ] . name ) . to . equal ( fileName )
156
+ expect ( files [ 0 ] . type ) . to . equal ( FILE_TYPES . file )
157
+ expect ( files [ 0 ] . size ) . to . equal ( content . length )
158
+ expect ( files [ 0 ] . hash . startsWith ( 'b' ) ) . to . equal ( true )
159
+ } )
160
+ } )
161
+
142
162
it ( 'fails to list non-existent file' , ( ) => {
143
163
return mfs . ls ( '/i-do-not-exist' )
144
164
. then ( ( ) => {
You can’t perform that action at this time.
0 commit comments