This repository was archived by the owner on Aug 12, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +34
-8
lines changed Expand file tree Collapse file tree 2 files changed +34
-8
lines changed Original file line number Diff line number Diff line change 3
3
const UnixFS = require ( 'ipfs-unixfs' )
4
4
const pull = require ( 'pull-stream' )
5
5
6
- function extractContent ( node ) {
7
- return UnixFS . unmarshal ( node . data ) . data
8
- }
9
-
10
6
// Logic to export a single (possibly chunked) unixfs file.
11
7
module . exports = ( node , name , ds ) => {
8
+ const file = UnixFS . unmarshal ( node . data )
12
9
let content
13
10
14
11
if ( node . links . length === 0 ) {
15
- const c = extractContent ( node )
16
- content = pull . values ( [ c ] )
12
+ content = pull . values ( [ file . data ] )
17
13
} else {
18
14
content = pull (
19
15
pull . values ( node . links ) ,
20
16
pull . map ( ( link ) => ds . getStream ( link . hash ) ) ,
21
17
pull . flatten ( ) ,
22
- pull . map ( extractContent )
18
+ pull . map ( ( node ) => {
19
+ try {
20
+ const ex = UnixFS . unmarshal ( node . data )
21
+ return ex . data
22
+ } catch ( err ) {
23
+ console . error ( node )
24
+ throw new Error ( 'Failed to unmarshal node' )
25
+ }
26
+ } )
23
27
)
24
28
}
25
29
26
30
return pull . values ( [ {
27
31
content : content ,
28
- path : name
32
+ path : name ,
33
+ size : file . fileSize ( )
29
34
} ] )
30
35
}
Original file line number Diff line number Diff line change @@ -24,6 +24,27 @@ module.exports = (repo) => {
24
24
ds = new DAGService ( bs )
25
25
} )
26
26
27
+ it ( 'import and export' , ( done ) => {
28
+ pull (
29
+ pull . values ( [ {
30
+ path : '1.2MiB.txt' ,
31
+ content : pull . values ( [ bigFile , Buffer ( 'hello world' ) ] )
32
+ } ] ) ,
33
+ unixFSEngine . importer ( ds ) ,
34
+ pull . map ( ( file ) => {
35
+ expect ( file . path ) . to . be . eql ( '1.2MiB.txt' )
36
+
37
+ return exporter ( file . multihash , ds )
38
+ } ) ,
39
+ pull . flatten ( ) ,
40
+ pull . collect ( ( err , files ) => {
41
+ expect ( err ) . to . not . exist
42
+ expect ( files [ 0 ] . size ) . to . be . eql ( bigFile . length + 11 )
43
+ fileEql ( files [ 0 ] , Buffer . concat ( [ bigFile , Buffer ( 'hello world' ) ] ) , done )
44
+ } )
45
+ )
46
+ } )
47
+
27
48
it ( 'ensure hash inputs are sanitized' , ( done ) => {
28
49
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
29
50
const mhBuf = new Buffer ( bs58 . decode ( hash ) )
You can’t perform that action at this time.
0 commit comments