2
2
3
3
const mh = require ( 'multihashes' )
4
4
const debug = require ( 'debug' )
5
- const log = debug ( 'jsipfs:http-api:files' )
6
- log . error = debug ( 'jsipfs:http-api:files:error' )
5
+ const log = debug ( 'jsipfs:http-api:file' )
6
+ log . error = debug ( 'jsipfs:http-api:file:error' )
7
+ const unixfsEngine = require ( 'ipfs-unixfs-engine' )
8
+ const exporter = unixfsEngine . exporter
9
+ const pull = require ( 'pull-stream' )
10
+ const toB58String = require ( 'multihashes' ) . toB58String
7
11
8
12
exports = module . exports
9
13
@@ -12,6 +16,18 @@ const fileTypeMap = {
12
16
dir : 'Directory'
13
17
}
14
18
19
+ function toFileObject ( file ) {
20
+ const fo = {
21
+ Hash : toB58String ( file . hash ) ,
22
+ Size : file . size ,
23
+ Type : fileTypeMap [ file . type ] || file . type
24
+ }
25
+ if ( fo . Hash !== file . name ) {
26
+ fo . Name = file . name
27
+ }
28
+ return fo ;
29
+ }
30
+
15
31
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
16
32
exports . parseKey = ( request , reply ) => {
17
33
if ( ! request . query . arg ) {
@@ -42,8 +58,11 @@ exports.parseKey = (request, reply) => {
42
58
} ) . code ( 500 ) . takeover ( )
43
59
}
44
60
61
+ const subpaths = key . split ( '/' )
62
+ subpaths . shift ( )
45
63
reply ( {
46
64
path : request . query . arg ,
65
+ subpaths : subpaths ,
47
66
key : key ,
48
67
hash : hash
49
68
} )
@@ -55,36 +74,38 @@ exports.ls = {
55
74
56
75
// main route handler which is called after the above `parseArgs`, but only if the args were valid
57
76
handler : ( request , reply ) => {
58
- const key = request . pre . args . key
59
77
const path = request . pre . args . path
60
- const hash = request . pre . args . hash
61
78
const ipfs = request . server . app . ipfs
79
+ const subpaths = request . pre . args . subpaths
80
+ const rootDepth = subpaths . length
62
81
63
- ipfs . ls ( key , ( err , files ) => {
64
- if ( err ) {
65
- return reply ( {
66
- Message : 'Failed to list dir: ' + err . message ,
67
- Code : 0
68
- } ) . code ( 500 )
69
- }
82
+ pull (
83
+ exporter ( path , ipfs . _ipldResolver , { maxDepth : rootDepth + 1 } ) ,
84
+ pull . collect ( ( err , files ) => {
85
+ if ( err ) {
86
+ return reply ( {
87
+ Message : 'Failed to list dir: ' + err . message ,
88
+ Code : 0
89
+ } ) . code ( 500 )
90
+ }
70
91
71
- let res = {
72
- Arguments : { } ,
73
- Objects : { }
74
- }
75
- res . Arguments [ path ] = key
76
- res . Objects [ key ] = {
77
- Hash : hash ,
78
- Size : 0 ,
79
- Type : 'Directory' ,
80
- Links : files . map ( ( file ) => ( {
81
- Name : file . name ,
82
- Hash : file . hash ,
83
- Size : file . size ,
84
- Type : fileTypeMap [ file . type ] || file . type
85
- } ) )
86
- }
87
- reply ( res )
88
- } )
92
+ let res = {
93
+ Arguments : { } ,
94
+ Objects : { }
95
+ }
96
+ const links = [ ]
97
+ files . forEach ( ( file ) => {
98
+ if ( file . depth === rootDepth ) {
99
+ let id = toB58String ( file . hash )
100
+ res . Arguments [ path ] = id
101
+ res . Objects [ id ] = toFileObject ( file )
102
+ res . Objects [ id ] . Links = file . type === 'file' ? null : links ;
103
+ } else {
104
+ links . push ( toFileObject ( file ) )
105
+ }
106
+ } )
107
+ return reply ( res )
108
+ } )
109
+ )
89
110
}
90
111
}
0 commit comments